Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/workbench/extensions/agent/crdt/followerDoc.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { describe, expect, it, vi } from 'vitest'
import * as Y from 'yjs'

import { FollowerDoc } from './followerDoc'

describe('FollowerDoc.applyRemoteUpdate', () => {
it('applies a valid host update', () => {
const host = new Y.Doc()
host.getMap('nodes').set('1', { type: 'LoadImage' })
const update = Y.encodeStateAsUpdate(host)
const follower = new FollowerDoc()
const onUpdate = vi.fn()
follower.addEventListener('update', onUpdate)

follower.applyRemoteUpdate(update)

expect(follower.doc.getMap('nodes').toJSON()).toEqual({
'1': { type: 'LoadImage' }
})
expect(follower.updatesApplied).toBe(1)
expect(onUpdate).toHaveBeenCalledOnce()
})

it('throws for a truncated update without dispatching an event or incrementing updatesApplied', () => {
const follower = new FollowerDoc()
const onUpdate = vi.fn()
follower.addEventListener('update', onUpdate)

expect(() => follower.applyRemoteUpdate(Uint8Array.of(0))).toThrow(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium — This test pins propagation as the contract for malformed remote updates, but the production caller (LayoutFollowerBridge.onDocUpdate) advances lastSeq before calling applyRemoteUpdate and wraps it in no try/catch, so a single corrupt frame throws out of the socket listener while the gap detector now believes the follower is caught up — no reportError(), no resubscribe, and silent divergence for the rest of the session. Consider pinning containment plus recovery (catch, report, resubscribe) instead, or at minimum not baking the throwing behaviour in as intended. Raised by 4 of 8 reviewers (claude-opus-5-thinking-max adversarial, claude-opus-5-thinking-max edge-case, gemini-3.1-pro adversarial, gpt-5.6-sol-max adversarial).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deferred to #16869: recovery requires a production bridge change plus bridge-level coverage. This PR remains a characterization of the current FollowerDoc propagation contract.

'Unexpected end of array'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚪ Nit — Matching 'Unexpected end of array' couples the test to an internal lib0 decoder message via substring match: it will pass on any unrelated error containing that text and break on a yjs/lib0 bump with no behaviour change. Asserting that it throws at all, or on a type this codebase owns, expresses the intent more durably. Raised by 2 of 8 reviewers (claude-opus-5-thinking-max adversarial, claude-opus-5-thinking-max edge-case).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left as-is: the exact decoder error is part of this narrow characterization. Matching any thrown error would let an unrelated regression satisfy the test.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, but this is still a form of change-detector.

)

expect(onUpdate).not.toHaveBeenCalled()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Low — Uint8Array.of(0) fails at the first decode step, so the assertions only cover input that could never partially apply, and the test never asserts follower.doc is unchanged. Yjs integrates struct refs inside a transaction with no rollback, so an update truncated later (e.g. update.slice(0, -1)) can mutate the doc and advance stateVector() while updatesApplied stays 0 — add a doc-state assertion with a truncated real update to actually cover atomicity. Raised by 2 of 8 reviewers (claude-opus-5-thinking-max adversarial, claude-opus-5-thinking-max edge-case).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left as-is: this test makes no atomicity claim. It characterizes counters and events for a deterministic first-decode rejection; partial-apply behavior needs separate targeted coverage.

expect(follower.updatesApplied).toBe(0)
})
})
Loading