From cd79a92653bd1930f83723d60d8cb3586b9e7268 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Mon, 26 Oct 2026 12:40:38 -0700 Subject: [PATCH 1/3] test(agent): share one record guard across the agent wire fixtures The agent follower fixtures each hand-rolled the same `typeof x === 'object' && x !== null` check before reading a parsed doc frame. This replaces the two remaining copies with one guard under `browser_tests/fixtures/utils/`, and adds its unit test. The guard EXCLUDES arrays, unlike the version this change was carried from. `value is Record` is false for an array, so admitting one lets a caller index it by string key with no compiler complaint, and three sibling guards in `browser_tests/fixtures/customNode/` already exclude them. Both call sites reject an array one line later anyway (`type`/`op` read as `undefined`), so no valid frame changes outcome: an array `data` now returns null instead of being handed back typed as a record. `agentGraphOpLostOnDisconnect.spec.ts` needed no change. Its local copy of the guard went away with the `subscriptionFrameOf` helper that used it, so the duplicate this would have removed is already gone from main. --- .../fixtures/agentFollowerHostSocket.ts | 9 ++++---- browser_tests/fixtures/agentWireFrame.ts | 6 +++-- browser_tests/fixtures/utils/isRecord.test.ts | 23 +++++++++++++++++++ browser_tests/fixtures/utils/isRecord.ts | 12 ++++++++++ 4 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 browser_tests/fixtures/utils/isRecord.test.ts create mode 100644 browser_tests/fixtures/utils/isRecord.ts diff --git a/browser_tests/fixtures/agentFollowerHostSocket.ts b/browser_tests/fixtures/agentFollowerHostSocket.ts index 86342b4a121..37f2d0d9715 100644 --- a/browser_tests/fixtures/agentFollowerHostSocket.ts +++ b/browser_tests/fixtures/agentFollowerHostSocket.ts @@ -27,6 +27,7 @@ import type { ParsedWireBatch, WireOpEnvelope } from '@e2e/fixtures/agentWireFrame' +import { isRecord } from '@e2e/fixtures/utils/isRecord' const SUBSCRIBE_TIMEOUT = 15_000 /** The sid the fake ComfyUI reports on its first `status` frame. */ @@ -86,11 +87,11 @@ function docFrameEnvelope( raw: string | Buffer ): { type: string; data: Record } | null { const frame: unknown = JSON.parse(raw.toString()) - if (typeof frame !== 'object' || frame === null) return null - const { type, data } = frame as { type?: unknown; data?: unknown } + if (!isRecord(frame)) return null + const { type, data } = frame if (typeof type !== 'string' || !type.startsWith('doc_')) return null - if (typeof data !== 'object' || data === null) return null - return { type, data: data as Record } + if (!isRecord(data)) return null + return { type, data } } function stringOrNull(value: unknown): string | null { diff --git a/browser_tests/fixtures/agentWireFrame.ts b/browser_tests/fixtures/agentWireFrame.ts index 569a8659dc5..b41297359d3 100644 --- a/browser_tests/fixtures/agentWireFrame.ts +++ b/browser_tests/fixtures/agentWireFrame.ts @@ -1,5 +1,7 @@ import { DEFERRED_OPS, FROZEN_OPS } from '@comfyorg/comfy-multi-player' +import { isRecord } from '@e2e/fixtures/utils/isRecord' + const WIRE_OP_KINDS: readonly string[] = [...FROZEN_OPS, ...DEFERRED_OPS] /** @@ -16,8 +18,8 @@ export interface WireOpEnvelope { } function isWireEnvelopeShaped(value: unknown): value is WireOpEnvelope { - if (typeof value !== 'object' || value === null) return false - const { op, op_id } = value as { op?: unknown; op_id?: unknown } + if (!isRecord(value)) return false + const { op, op_id } = value return ( typeof op === 'string' && WIRE_OP_KINDS.includes(op) && diff --git a/browser_tests/fixtures/utils/isRecord.test.ts b/browser_tests/fixtures/utils/isRecord.test.ts new file mode 100644 index 00000000000..a63fd4bf0cd --- /dev/null +++ b/browser_tests/fixtures/utils/isRecord.test.ts @@ -0,0 +1,23 @@ +import { describe, expect, it } from 'vitest' + +import { isRecord } from '@e2e/fixtures/utils/isRecord' + +describe('isRecord', () => { + it('accepts indexable objects', () => { + expect(isRecord({})).toBe(true) + expect(isRecord({ type: 'doc_ops' })).toBe(true) + }) + + it('rejects null and primitives', () => { + expect(isRecord(null)).toBe(false) + expect(isRecord(undefined)).toBe(false) + expect(isRecord('doc_ops')).toBe(false) + expect(isRecord(1)).toBe(false) + expect(isRecord(true)).toBe(false) + }) + + it('rejects arrays, which the Record predicate would otherwise mistype', () => { + expect(isRecord([])).toBe(false) + expect(isRecord([{ op: 'add_node' }])).toBe(false) + }) +}) diff --git a/browser_tests/fixtures/utils/isRecord.ts b/browser_tests/fixtures/utils/isRecord.ts new file mode 100644 index 00000000000..73ecdb0b1b1 --- /dev/null +++ b/browser_tests/fixtures/utils/isRecord.ts @@ -0,0 +1,12 @@ +/** + * Narrows an unknown parsed-JSON value to an indexable object. + * + * Arrays are excluded. The predicate claims `Record`, which an + * array is not, so admitting one lets a caller index it by string key with no + * complaint from the compiler. This also matches the sibling guards already in + * `browser_tests/fixtures/customNode/`, so the name means one thing across the + * fixtures. + */ +export function isRecord(value: unknown): value is Record { + return typeof value === 'object' && value !== null && !Array.isArray(value) +} From 9a7f2888569ae843aad1dae68eb81538d4c6f340 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Mon, 26 Oct 2026 13:18:08 -0700 Subject: [PATCH 2/3] test(agent): table-drive the record guard cases and cut its JSDoc to the contract The guard's JSDoc narrated why arrays are excluded and pointed at the sibling guards. AGENTS.md permits only a curt contract JSDoc on an exported API, so the array exclusion stays as the contract and the rationale moves here: the predicate claims `Record`, which an array is not, so admitting one would let a caller index it by string key with no compiler complaint, and the sibling guards in browser_tests/fixtures/customNode/ already exclude them. The three cases differed only in input and expected output, which docs/guidance/testing-principles.md makes a table-driven default. One `it.for` table now gives each value its own named row with the expected result in the row and no branching in the body. Both are CodeRabbit findings on cd79a92653. --- browser_tests/fixtures/utils/isRecord.test.ts | 28 ++++++++----------- browser_tests/fixtures/utils/isRecord.ts | 10 +------ 2 files changed, 13 insertions(+), 25 deletions(-) diff --git a/browser_tests/fixtures/utils/isRecord.test.ts b/browser_tests/fixtures/utils/isRecord.test.ts index a63fd4bf0cd..44e3c2ea216 100644 --- a/browser_tests/fixtures/utils/isRecord.test.ts +++ b/browser_tests/fixtures/utils/isRecord.test.ts @@ -3,21 +3,17 @@ import { describe, expect, it } from 'vitest' import { isRecord } from '@e2e/fixtures/utils/isRecord' describe('isRecord', () => { - it('accepts indexable objects', () => { - expect(isRecord({})).toBe(true) - expect(isRecord({ type: 'doc_ops' })).toBe(true) - }) - - it('rejects null and primitives', () => { - expect(isRecord(null)).toBe(false) - expect(isRecord(undefined)).toBe(false) - expect(isRecord('doc_ops')).toBe(false) - expect(isRecord(1)).toBe(false) - expect(isRecord(true)).toBe(false) - }) - - it('rejects arrays, which the Record predicate would otherwise mistype', () => { - expect(isRecord([])).toBe(false) - expect(isRecord([{ op: 'add_node' }])).toBe(false) + it.for<[string, unknown, boolean]>([ + ['accepts an empty object', {}, true], + ['accepts an object with keys', { type: 'doc_ops' }, true], + ['rejects null', null, false], + ['rejects undefined', undefined, false], + ['rejects a string', 'doc_ops', false], + ['rejects a number', 1, false], + ['rejects a boolean', true, false], + ['rejects an empty array', [], false], + ['rejects an array of objects', [{ op: 'add_node' }], false] + ])('%s', ([, value, expected]) => { + expect(isRecord(value)).toBe(expected) }) }) diff --git a/browser_tests/fixtures/utils/isRecord.ts b/browser_tests/fixtures/utils/isRecord.ts index 73ecdb0b1b1..6796811aea9 100644 --- a/browser_tests/fixtures/utils/isRecord.ts +++ b/browser_tests/fixtures/utils/isRecord.ts @@ -1,12 +1,4 @@ -/** - * Narrows an unknown parsed-JSON value to an indexable object. - * - * Arrays are excluded. The predicate claims `Record`, which an - * array is not, so admitting one lets a caller index it by string key with no - * complaint from the compiler. This also matches the sibling guards already in - * `browser_tests/fixtures/customNode/`, so the name means one thing across the - * fixtures. - */ +/** True when `value` is a non-null object that is not an array. */ export function isRecord(value: unknown): value is Record { return typeof value === 'object' && value !== null && !Array.isArray(value) } From d39d4082f9a8d8f8ca003dd9b4ca495aef967551 Mon Sep 17 00:00:00 2001 From: bymyself Date: Tue, 27 Oct 2026 09:16:05 +0000 Subject: [PATCH 3/3] fix: reuse es-toolkit plain object guard Addresses review feedback:\nhttps://github.com/Comfy-Org/ComfyUI_frontend/pull/21995#discussion_r4382148164 --- .../fixtures/agentFollowerHostSocket.ts | 6 +++--- browser_tests/fixtures/agentWireFrame.ts | 5 ++--- browser_tests/fixtures/utils/isRecord.test.ts | 19 ------------------- browser_tests/fixtures/utils/isRecord.ts | 4 ---- 4 files changed, 5 insertions(+), 29 deletions(-) delete mode 100644 browser_tests/fixtures/utils/isRecord.test.ts delete mode 100644 browser_tests/fixtures/utils/isRecord.ts diff --git a/browser_tests/fixtures/agentFollowerHostSocket.ts b/browser_tests/fixtures/agentFollowerHostSocket.ts index 37f2d0d9715..48fdb508696 100644 --- a/browser_tests/fixtures/agentFollowerHostSocket.ts +++ b/browser_tests/fixtures/agentFollowerHostSocket.ts @@ -27,7 +27,7 @@ import type { ParsedWireBatch, WireOpEnvelope } from '@e2e/fixtures/agentWireFrame' -import { isRecord } from '@e2e/fixtures/utils/isRecord' +import { isPlainObject } from 'es-toolkit' const SUBSCRIBE_TIMEOUT = 15_000 /** The sid the fake ComfyUI reports on its first `status` frame. */ @@ -87,10 +87,10 @@ function docFrameEnvelope( raw: string | Buffer ): { type: string; data: Record } | null { const frame: unknown = JSON.parse(raw.toString()) - if (!isRecord(frame)) return null + if (!isPlainObject(frame)) return null const { type, data } = frame if (typeof type !== 'string' || !type.startsWith('doc_')) return null - if (!isRecord(data)) return null + if (!isPlainObject(data)) return null return { type, data } } diff --git a/browser_tests/fixtures/agentWireFrame.ts b/browser_tests/fixtures/agentWireFrame.ts index b41297359d3..3b01167791e 100644 --- a/browser_tests/fixtures/agentWireFrame.ts +++ b/browser_tests/fixtures/agentWireFrame.ts @@ -1,6 +1,5 @@ import { DEFERRED_OPS, FROZEN_OPS } from '@comfyorg/comfy-multi-player' - -import { isRecord } from '@e2e/fixtures/utils/isRecord' +import { isPlainObject } from 'es-toolkit' const WIRE_OP_KINDS: readonly string[] = [...FROZEN_OPS, ...DEFERRED_OPS] @@ -18,7 +17,7 @@ export interface WireOpEnvelope { } function isWireEnvelopeShaped(value: unknown): value is WireOpEnvelope { - if (!isRecord(value)) return false + if (!isPlainObject(value)) return false const { op, op_id } = value return ( typeof op === 'string' && diff --git a/browser_tests/fixtures/utils/isRecord.test.ts b/browser_tests/fixtures/utils/isRecord.test.ts deleted file mode 100644 index 44e3c2ea216..00000000000 --- a/browser_tests/fixtures/utils/isRecord.test.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { describe, expect, it } from 'vitest' - -import { isRecord } from '@e2e/fixtures/utils/isRecord' - -describe('isRecord', () => { - it.for<[string, unknown, boolean]>([ - ['accepts an empty object', {}, true], - ['accepts an object with keys', { type: 'doc_ops' }, true], - ['rejects null', null, false], - ['rejects undefined', undefined, false], - ['rejects a string', 'doc_ops', false], - ['rejects a number', 1, false], - ['rejects a boolean', true, false], - ['rejects an empty array', [], false], - ['rejects an array of objects', [{ op: 'add_node' }], false] - ])('%s', ([, value, expected]) => { - expect(isRecord(value)).toBe(expected) - }) -}) diff --git a/browser_tests/fixtures/utils/isRecord.ts b/browser_tests/fixtures/utils/isRecord.ts deleted file mode 100644 index 6796811aea9..00000000000 --- a/browser_tests/fixtures/utils/isRecord.ts +++ /dev/null @@ -1,4 +0,0 @@ -/** True when `value` is a non-null object that is not an array. */ -export function isRecord(value: unknown): value is Record { - return typeof value === 'object' && value !== null && !Array.isArray(value) -}