Skip to content

Commit 696aa22

Browse files
test(webview): pin the unset viewStates write the Anything matcher cannot see
The review row on src/core/webview/__tests__/webviewMessageHandler.spec.ts:2134 was right about the assertion, and the reason is worth keeping where the next person will read it. expect.anything() excludes undefined and null, so not.toHaveBeenCalledWith("viewStates", expect.anything()) still passes when the updateSettings loop writes the key with an undefined value - which is the shape that erases the registered map, since ContextProxy reads an undefined value as a delete (ContextProxy.ts:464-466 for secrets, and globalState.update(key, undefined) deletes, which this repo relies on at :173, :280-282, :305 and :346). The shipped guard is not wrong: webviewMessageHandler.ts:790 drops the key regardless of value, so the defect here was test strength rather than behaviour. It is worth strengthening anyway because the same file already pins that undefined shape for another key at :2109-2116, so the weaker form is one copy-paste away. The asymmetry between the two doubles is the general rule for this family, and it is why the new case asserts the call rather than a value-shaped matcher: the provider-side test is erase-safe for free because it asserts on the stored state (ClineProvider.spec.ts:9038-9040, where an unset write makes the equality fail), while the handler double is a bare contextProxy.setValue mock that has to pin the call and its value. Copy the stronger form, not the neighbour. Case: updateSettings with { viewStates: undefined, alwaysDenyUnapprovedCommands: true } must never write viewStates - neither as undefined nor through the Anything matcher - must still write the other key, and must log the ignore. Control: with the guard block removed from webviewMessageHandler.ts the file reports 2 failed | 174 passed, the new case failing with expected "vi.fn()" to not be called with arguments: [ 'viewStates', undefined ]; with the guard in place it is 176 passed and exit 0. Gates on the committed text: webviewMessageHandler.spec 176 passing, src tsc --noEmit exit 0, eslint clean with the file's single suppression entry unchanged at 35 and src/eslint-suppressions.json byte-identical to its blob at 3298. The compile-job command list was run in job order with the same results as the previous heads: lint, check-types, code-qa-ci, solheim-smoke check-types, agent-evals unit and check-types, lifecycle model-check, webview-recovery check-types and model-check and the MCP OAuth check green; format:check red locally on 2004 files, none of them the file touched here, which passes prettier --check after LF normalisation, and the two solheim record.sh suites need ffprobe and POSIX path separators that this box lacks. Test-only change: no behaviour touched, no changeset, no CHANGELOG, no suppression increase.
1 parent 138b147 commit 696aa22

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

‎src/core/webview/__tests__/webviewMessageHandler.spec.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2138,6 +2138,29 @@ it("ignores a webview-supplied viewStates map in the generic updateSettings loop
21382138
)
21392139
})
21402140

2141+
// An unset is the shape that erases the stored map: ContextProxy reads an undefined value as a delete
2142+
// (ContextProxy.ts:464-466 for secrets; globalState.update(key, undefined) deletes, which the repo relies
2143+
// on at :173, :280-282, :305 and :346), and expect.anything() cannot see it because it excludes
2144+
// undefined and null - so the matcher in the case above is not enough on its own, and the call itself
2145+
// has to be pinned here. The asymmetry to copy when extending this family: the provider-side test is
2146+
// erase-safe because it asserts on the stored state, while this double is a bare setValue mock and so
2147+
// must pin the call and its value, exactly as the case above pins the unset shape for another key.
2148+
it("ignores an unset viewStates map in the generic updateSettings loop", async () => {
2149+
vi.clearAllMocks()
2150+
2151+
await webviewMessageHandler(mockClineProvider, {
2152+
type: "updateSettings",
2153+
updatedSettings: { viewStates: undefined, alwaysDenyUnapprovedCommands: true },
2154+
})
2155+
2156+
expect(mockClineProvider.contextProxy.setValue).not.toHaveBeenCalledWith("viewStates", undefined)
2157+
expect(mockClineProvider.contextProxy.setValue).not.toHaveBeenCalledWith("viewStates", expect.anything())
2158+
expect(mockClineProvider.contextProxy.setValue).toHaveBeenCalledWith("alwaysDenyUnapprovedCommands", true)
2159+
expect(mockClineProvider.log).toHaveBeenCalledWith(
2160+
expect.stringContaining("Ignored a webview-supplied viewStates map"),
2161+
)
2162+
})
2163+
21412164
// Both allowlists are normalized by the same branch, so both are held to the
21422165
// same contract.
21432166
describe.each(["allowedReadFiles", "allowedWriteFiles"] as const)("webviewMessageHandler - %s", (key) => {

0 commit comments

Comments
 (0)