fix(app): validate persisted client state (#3289)
* fix(app): validate persisted client state Persisted replica data could satisfy TypeScript through an assertion while missing fields required by renderers. Treat every AsyncStorage value as untrusted input, clear values that fail strict schemas, and version the replica DTO so incompatible caches cannot hydrate. * fix(app): preserve validated legacy state * fix(app): preserve validated preferences Preserve supported legacy location fields and every shipped theme while keeping unknown persisted fields rejected. Align browser test seeds with the current preference shape so strict validation exercises the intended provider. * fix(app): migrate legacy agent preferences Released clients stored host/location fields and per-provider thinkingOptionId together. Migrate those exact strict bytes into current preferences without admitting unknown fields. * fix(app): preserve drafts with legacy reviews Accept the exact strict shape released for workspace review attachments so migration can discard that obsolete attachment without clearing every persisted draft.
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import type { StateStorage } from "zustand/middleware";
|
||||
import type { ParsedDiffFile } from "@/git/use-diff-query";
|
||||
import { createValidatedPersistStorage } from "@/storage/validated-persist-storage";
|
||||
import {
|
||||
buildReviewAttachmentSnapshot,
|
||||
buildReviewDraftKey,
|
||||
@@ -16,6 +18,7 @@ import {
|
||||
type ReviewDraftComment,
|
||||
type ReviewDraftStoreState,
|
||||
serializeReviewDraftState,
|
||||
SerializedReviewDraftStateSchema,
|
||||
setDiffModeOverrideInState,
|
||||
updateCommentInState,
|
||||
} from "./state";
|
||||
@@ -69,6 +72,20 @@ function makeFile(): ParsedDiffFile {
|
||||
};
|
||||
}
|
||||
|
||||
function createMemoryStorage(): StateStorage & { values: Map<string, string> } {
|
||||
const values = new Map<string, string>();
|
||||
return {
|
||||
values,
|
||||
getItem: async (key) => values.get(key) ?? null,
|
||||
setItem: async (key, value) => {
|
||||
values.set(key, value);
|
||||
},
|
||||
removeItem: async (key) => {
|
||||
values.delete(key);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
describe("buildReviewDraftKey", () => {
|
||||
it("scopes by server, workspace-or-cwd, diff mode, base ref, and whitespace mode", () => {
|
||||
const base = buildReviewDraftKey({
|
||||
@@ -122,7 +139,27 @@ describe("buildReviewDraftKey", () => {
|
||||
});
|
||||
|
||||
describe("normalizePersistedState", () => {
|
||||
it("filters invalid draft comments and ignores activeModesByScope from old persisted JSON", () => {
|
||||
it("keeps v1 review comments for migration while dropping the legacy mode field", async () => {
|
||||
const backing = createMemoryStorage();
|
||||
const legacyState = {
|
||||
drafts: { "review:key": [makeComment()] },
|
||||
activeModesByScope: { "review:scope": "base" },
|
||||
};
|
||||
backing.values.set(
|
||||
"@paseo:review-draft-store",
|
||||
JSON.stringify({ state: legacyState, version: 1 }),
|
||||
);
|
||||
const storage = createValidatedPersistStorage(backing, SerializedReviewDraftStateSchema);
|
||||
|
||||
const stored = await storage.getItem("@paseo:review-draft-store");
|
||||
const normalized = normalizePersistedState(stored?.state);
|
||||
|
||||
expect(normalized.drafts["review:key"]).toEqual([makeComment()]);
|
||||
expect(normalized.diffModeOverrides).toEqual({});
|
||||
expect(backing.values.has("@paseo:review-draft-store")).toBe(true);
|
||||
});
|
||||
|
||||
it("rejects the complete payload when any draft comment or field is invalid", () => {
|
||||
const normalized = normalizePersistedState({
|
||||
drafts: {
|
||||
"review:key": [
|
||||
@@ -146,17 +183,7 @@ describe("normalizePersistedState", () => {
|
||||
});
|
||||
|
||||
expect(normalized.diffModeOverrides).toEqual({});
|
||||
expect(normalized.drafts["review:key"]).toEqual([
|
||||
{
|
||||
id: "comment-1",
|
||||
filePath: "src/example.ts",
|
||||
side: "new",
|
||||
lineNumber: 41,
|
||||
body: "Keep me.",
|
||||
createdAt: "2026-04-21T00:00:00.000Z",
|
||||
updatedAt: "2026-04-21T00:00:00.000Z",
|
||||
},
|
||||
]);
|
||||
expect(normalized.drafts).toEqual({});
|
||||
});
|
||||
|
||||
it("returns empty state for null, non-object, or malformed inputs", () => {
|
||||
|
||||
Reference in New Issue
Block a user