Skip to content

Commit 999c651

Browse files
author
easonLiangWorldedtech
committed
test(types): cover the persisted view-state record at the schema boundary
The pre-merge review asked for focused coverage of the new runtime schema behaviour: viewStateSchema and the viewStates record on globalSettingsSchema were only exercised through the webview layer. Six tests, one per behaviour the record has to guarantee: - a valid record preserves mode, currentApiConfigName and updatedAt for every instance, because a selection the schema drops is written back as absent and the next window opens in the default mode; - the record is parsed standalone through the exported schema as well; - a numeric mode, a non-string config name and a non-numeric timestamp are each rejected, and each negative test parses the same record without the bad field first, so a failure can only mean the schema rejected that value - not that the record shape is unsupported or the assertion never ran; - a view state that is not an object is rejected, which is what a half-migrated store looks like. Negative controls, mutant restored from a byte snapshot (sha256 869345626629): mode accepts anything -> 1 red; config name accepts anything -> 1 red; timestamp accepts anything -> 1 red; record value becomes any -> 4 red; the viewStates key removed -> 5 red; the view state gains a required field -> 6 red. The single-test controls are the three "accepts anything" mutants. Verification: vitest on this spec 12 passed; tsc --noEmit -p packages/types run as an A/B on the same tree (with the file and at HEAD) - both 0 errors, error sets identical by name. Prettier was checked through --stdin-filepath with LF normalisation because this worktree has core.autocrlf=true, where a direct local prettier verdict is unreliable in both directions; the added lines are clean and the diff is purely additive (67 added, 0 deleted, no end-of-line flip).
1 parent 0861907 commit 999c651

1 file changed

Lines changed: 67 additions & 0 deletions

File tree

‎packages/types/src/__tests__/global-settings.test.ts‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
DEFAULT_DESTRUCTIVE_COMMAND_GUARD_ENABLED,
44
GLOBAL_SETTINGS_KEYS,
55
globalSettingsSchema,
6+
viewStateSchema,
67
} from "../global-settings.js"
78

89
describe("destructive command guard global setting", () => {
@@ -38,3 +39,69 @@ describe("alwaysDenyUnapprovedCommands global setting", () => {
3839
expect(() => globalSettingsSchema.parse({ alwaysDenyUnapprovedCommands: "true" })).toThrow()
3940
})
4041
})
42+
43+
describe("viewStates global setting", () => {
44+
it("preserves each webview instance selection through the persisted schema", () => {
45+
const parsed = globalSettingsSchema.parse({
46+
viewStates: {
47+
"session-1": { mode: "code", currentApiConfigName: "default", updatedAt: 1700000000000 },
48+
"session-2": { mode: "architect" },
49+
},
50+
})
51+
// Every field has to survive the round trip: a selection the schema drops is
52+
// written back as absent, so the next window opens in the default mode.
53+
expect(parsed.viewStates).toEqual({
54+
"session-1": { mode: "code", currentApiConfigName: "default", updatedAt: 1700000000000 },
55+
"session-2": { mode: "architect" },
56+
})
57+
})
58+
59+
it("parses a single view state on its own", () => {
60+
expect(viewStateSchema.parse({ mode: "ask", currentApiConfigName: "x", updatedAt: 5 })).toEqual({
61+
mode: "ask",
62+
currentApiConfigName: "x",
63+
updatedAt: 5,
64+
})
65+
})
66+
67+
it("rejects a numeric mode and names the offending path", () => {
68+
// The control first: the same record without the bad field must parse, so a
69+
// failure here can only mean the schema rejected that value - not that the record
70+
// shape is unsupported or the assertion never ran.
71+
expect(globalSettingsSchema.safeParse({ viewStates: { a: { mode: "code" } } }).success).toBe(true)
72+
73+
const result = globalSettingsSchema.safeParse({ viewStates: { a: { mode: 42 } } })
74+
75+
expect(result.success).toBe(false)
76+
expect(result.error?.issues[0]?.path).toEqual(["viewStates", "a", "mode"])
77+
})
78+
79+
it("rejects a non-string currentApiConfigName and names the offending path", () => {
80+
expect(globalSettingsSchema.safeParse({ viewStates: { a: { currentApiConfigName: "x" } } }).success).toBe(true)
81+
82+
const result = globalSettingsSchema.safeParse({ viewStates: { a: { currentApiConfigName: 7 } } })
83+
84+
expect(result.success).toBe(false)
85+
expect(result.error?.issues[0]?.path).toEqual(["viewStates", "a", "currentApiConfigName"])
86+
})
87+
88+
it("rejects a non-numeric updatedAt and names the offending path", () => {
89+
expect(globalSettingsSchema.safeParse({ viewStates: { a: { updatedAt: 1 } } }).success).toBe(true)
90+
91+
const result = globalSettingsSchema.safeParse({ viewStates: { a: { updatedAt: "1" } } })
92+
93+
expect(result.success).toBe(false)
94+
expect(result.error?.issues[0]?.path).toEqual(["viewStates", "a", "updatedAt"])
95+
})
96+
97+
it("rejects a view state that is not an object", () => {
98+
// A bare string under a session id is what a half-migrated store looks like;
99+
// accepting it defers the crash to whoever reads the selection back.
100+
expect(globalSettingsSchema.safeParse({ viewStates: { a: { mode: "code" } } }).success).toBe(true)
101+
102+
const result = globalSettingsSchema.safeParse({ viewStates: { a: "code" } })
103+
104+
expect(result.success).toBe(false)
105+
expect(result.error?.issues[0]?.path).toEqual(["viewStates", "a"])
106+
})
107+
})

0 commit comments

Comments
 (0)