Files
dsh-context/tests/client/assemble.spec.ts
T
bowenliang123 87a2893190 refactor(comments): clean up the test-suite comments to the guideline
Apply the AGENTS.md comments guideline to every .ts under tests/: remove
what the code already expresses, keep only what it cannot.

Removed: spec-header coverage inventories the describe/test tree already
names; comments restating the next line's assertion or its message; helper
JSDoc whose body already says it; within-file duplicates; five stale
claims (trendChart's "six aggregated turns" against agg.length === 8,
agentGraph's "six categories" against CATS' seven, fold-timing's "cheapest
call LAST" against a loop that feeds it first, fold-surface's unreachable
plain-JSON note, contextView's "two turns" for a one-turn fixture).

Kept and condensed: the projection-freeze class, strict wire/state-schema
hazards, retention and pricing windows, pairing rules, hostile-input
rationale, fixture and expectation-array ground truth, jsdom mechanics,
and issue-numbered regressions.

Comment lines 2604 -> 2014 (-22%); 85 module headers kept, 30 coverage
inventories dropped; every surviving comment line is within the 140-column
limit, with 36 wrapped lines joined onto fuller ones.

No logic, string, identifier, import or directive changed: a comment-strip
gate proves each file's comment-free code is byte-identical to HEAD, and a
line-level proof pairs all 845 changed lines with an unchanged code part.
pnpm run lint && pnpm run test (106 files / 2061 tests, per-file 100%
coverage) && pnpm run build all pass.
2026-10-10 00:50:55 +08:00

170 lines
6.4 KiB
TypeScript

// Context assembly (src/client/assemble.ts): the header epoch lookup and the per-step surface reconstruction with its coverage flags.
import assert from 'node:assert/strict'
import { describe, test } from 'vitest'
import { assemble, headerAt, systemAt } from '../../src/client/assemble'
import type { ContextHeaders, ContextTimeline, HeaderRecord } from '../../src/shared/types'
import { surfaceNodeAt } from './helpers/kit'
function epoch(seq: number): HeaderRecord {
return { seq, time: 0, tools: [] }
}
function headers(...seqs: number[]): ContextHeaders {
return { headers: seqs.map(epoch) }
}
function timeline(over: Partial<ContextTimeline> = {}): ContextTimeline {
return {
ok: true,
current: { system: 0, tools: 0, user: 0, inject: 0, skill: 0, assistant: 0, tool: 0, total: 0 },
requests: [],
events: [],
nodes: [],
droppedNodes: 0,
archive: [],
...over,
}
}
describe('headerAt', () => {
test('absent or empty headers yield null', () => {
assert.equal(headerAt(null, 10), null)
assert.equal(headerAt({ headers: [] }, 10), null)
})
test('a null seq picks the newest epoch', () => {
const h = headers(10, 20)
assert.equal(headerAt(h, null), h.headers[1])
})
test('a seq at or before every epoch yields null', () => {
assert.equal(headerAt(headers(10, 20), 5), null)
assert.equal(headerAt(headers(10, 20), 10), null)
})
test('the latest epoch before the seq is in force', () => {
const h = headers(10, 20)
assert.equal(headerAt(h, 15), h.headers[0])
assert.equal(headerAt(h, 25), h.headers[1])
})
})
describe('assemble live view', () => {
test('a null seq returns all live nodes for the next request', () => {
const data = timeline({ nodes: [surfaceNodeAt(1), surfaceNodeAt(5)] })
const h = headers(10)
const out = assemble(data, h, null)
assert.equal(out.live, true)
assert.deepEqual(out.nodes.map(n => n.seq), [1, 5])
assert.notEqual(out.nodes, data.nodes)
assert.equal(out.header, h.headers[0])
assert.equal(out.missingLive, 0)
assert.equal(out.approximate, false)
})
test('dropped live nodes are all part of the live context', () => {
const data = timeline({ nodes: [surfaceNodeAt(5)], droppedNodes: 3, surfaceFloor: 4 })
assert.equal(assemble(data, null, null).missingLive, 3)
})
})
describe('assemble past step', () => {
test('the surface is live nodes before the step plus still-alive archive nodes', () => {
const data = timeline({
nodes: [surfaceNodeAt(1), surfaceNodeAt(3), surfaceNodeAt(8)],
archive: [
surfaceNodeAt(2, { gone: 10 }),
surfaceNodeAt(4, { gone: 5 }),
surfaceNodeAt(6),
surfaceNodeAt(9, { gone: 12 }),
],
})
const out = assemble(data, null, 7)
assert.equal(out.live, false)
assert.deepEqual(out.nodes.map(n => n.seq), [1, 2, 3])
})
test('the assembled nodes come back in seq order', () => {
const data = timeline({ nodes: [surfaceNodeAt(3), surfaceNodeAt(1)], archive: [surfaceNodeAt(2, { gone: 10 })] })
assert.deepEqual(assemble(data, null, 7).nodes.map(n => n.seq), [1, 2, 3])
})
test('the header in force at the step rides along', () => {
const h = headers(2, 10)
assert.equal(assemble(timeline(), h, 7).header, h.headers[0])
assert.equal(assemble(timeline(), h, 1).header, null)
})
})
describe('assemble missingLive for past steps', () => {
test('a step after the surface floor contains every dropped live node', () => {
const data = timeline({ droppedNodes: 2, surfaceFloor: 5 })
assert.equal(assemble(data, null, 6).missingLive, 2)
})
test('a step at or below the surface floor flags nothing', () => {
const data = timeline({ droppedNodes: 2, surfaceFloor: 5 })
assert.equal(assemble(data, null, 5).missingLive, 0)
assert.equal(assemble(data, null, 3).missingLive, 0)
})
test('without a surface floor a past step flags nothing', () => {
const data = timeline({ droppedNodes: 2 })
assert.equal(assemble(data, null, 6).missingLive, 0)
})
})
describe('assemble approximate flag', () => {
test('a step older than the archive floor is approximate', () => {
const data = timeline({ archiveFloor: 10 })
assert.equal(assemble(data, null, 6).approximate, true)
})
test('a step at or after the archive floor is exact', () => {
const data = timeline({ archiveFloor: 10 })
assert.equal(assemble(data, null, 10).approximate, false)
assert.equal(assemble(data, null, 12).approximate, false)
})
test('without an archive floor no step is approximate', () => {
assert.equal(assemble(timeline(), null, 6).approximate, false)
})
})
describe('systemAt — the system prompt in force', () => {
const sys = (seq: number, tokens: number, time = seq * 100) => ({ seq, time, tokens })
test('the last nonempty live node wins, dormant empties are skipped', () => {
const data = timeline({ systems: [sys(3, 10), sys(8, 0), sys(12, 20)] })
assert.deepEqual(systemAt(data, null, null), sys(12, 20))
// Only a dormant node after the last live one: the earlier prompt stands.
assert.deepEqual(systemAt(timeline({ systems: [sys(3, 10), sys(8, 0)] }), null, null), sys(3, 10))
})
test('a step resolves the prompt logged before it', () => {
const data = timeline({ systems: [sys(3, 10), sys(12, 20)] })
assert.deepEqual(systemAt(data, null, 12), sys(3, 10))
assert.deepEqual(systemAt(data, null, 13), sys(12, 20))
assert.equal(systemAt(data, null, 3), null, 'at the node itself the prompt is not yet in force')
})
test('a list holding only dormant nodes yields no prompt', () => {
assert.equal(systemAt(timeline({ systems: [sys(3, 0)] }), null, null), null)
})
test('rows folded before `systems` existed fall back to the header epoch', () => {
const epoch: HeaderRecord = { seq: 15, time: 1500, systemTokens: 42, tools: [] }
assert.deepEqual(systemAt(timeline(), epoch, null), { seq: 15, time: 1500, tokens: 42 })
assert.deepEqual(systemAt(timeline({ systems: [] }), epoch, 20), { seq: 15, time: 1500, tokens: 42 })
assert.equal(systemAt(timeline(), { seq: 15, time: 1500, tools: [] }, null), null, 'an epoch without a price yields none')
assert.equal(systemAt(timeline(), null, null), null)
})
test('assemble exposes the resolved prompt', () => {
const data = timeline({ systems: [sys(3, 10)] })
assert.deepEqual(assemble(data, null, null).system, sys(3, 10))
assert.equal(assemble(timeline(), null, null).system, null)
})
})