200 lines
7.5 KiB
TypeScript
200 lines
7.5 KiB
TypeScript
import assert from 'node:assert/strict';
|
|
import { test } from 'vitest';
|
|
import {
|
|
DEFAULT_RENDER_TURNS,
|
|
MAX_RENDER_TURNS,
|
|
batchSizeForLimit,
|
|
countTurnsInSlice,
|
|
expandNewerInWindow,
|
|
expandOlderInWindow,
|
|
initialTurnWindow,
|
|
jumpToTurnInWindow,
|
|
normalizeRenderTurnLimit,
|
|
renderTurnLimitOf,
|
|
sliceTurnWindow,
|
|
stabilizeGroups,
|
|
} from '../src/client/turn-window.js';
|
|
import type { ReaderGroup } from '../src/client/projection.js';
|
|
import { reconcileTurnSlots } from '../src/client/turn-slots.js';
|
|
|
|
function makeGroups(count: number): ReaderGroup[] {
|
|
return Array.from({ length: count }, (_, index) => ({
|
|
key: `turn:${index}`,
|
|
turn: index,
|
|
keys: [`user-${index}`, `assistant-${index}`],
|
|
}));
|
|
}
|
|
|
|
test('normalizeRenderTurnLimit defaults to 6, supports unlimited 0, and clamps bounds', () => {
|
|
assert.equal(DEFAULT_RENDER_TURNS, 6);
|
|
assert.equal(normalizeRenderTurnLimit(undefined), 6);
|
|
assert.equal(normalizeRenderTurnLimit(-3), 6);
|
|
assert.equal(normalizeRenderTurnLimit(Number.NaN), 6);
|
|
assert.equal(normalizeRenderTurnLimit('invalid'), 6);
|
|
assert.equal(normalizeRenderTurnLimit(0.5), 6);
|
|
|
|
assert.equal(normalizeRenderTurnLimit(0), 0);
|
|
assert.equal(normalizeRenderTurnLimit('0'), 0);
|
|
assert.equal(normalizeRenderTurnLimit(' all '), 0);
|
|
assert.equal(normalizeRenderTurnLimit(null), 0);
|
|
|
|
assert.equal(normalizeRenderTurnLimit(4), 4);
|
|
assert.equal(normalizeRenderTurnLimit(' 12 '), 12);
|
|
assert.equal(normalizeRenderTurnLimit(999), MAX_RENDER_TURNS);
|
|
|
|
assert.equal(renderTurnLimitOf(undefined), 6);
|
|
assert.equal(renderTurnLimitOf({ renderTurnLimit: 10 }), 10);
|
|
assert.equal(renderTurnLimitOf({ renderTurnLimit: 0 }), 0);
|
|
|
|
assert.equal(batchSizeForLimit(10), 10);
|
|
assert.equal(batchSizeForLimit(0), 6);
|
|
});
|
|
|
|
test('sliceTurnWindow keeps only the latest 6 turns by default and supports unlimited mode', () => {
|
|
const groups = makeGroups(20);
|
|
const initial = initialTurnWindow(6);
|
|
|
|
const sliced = sliceTurnWindow(groups, initial, 6);
|
|
assert.equal(sliced.visible.length, 6);
|
|
assert.deepEqual(sliced.visible.map(g => g.turn), [14, 15, 16, 17, 18, 19]);
|
|
assert.equal(sliced.start, 14);
|
|
assert.equal(sliced.end, 20);
|
|
assert.equal(sliced.hiddenOlder, 14);
|
|
assert.equal(sliced.hiddenNewer, 0);
|
|
|
|
const shortGroups = makeGroups(4);
|
|
const shortSlice = sliceTurnWindow(shortGroups, initial, 6);
|
|
assert.equal(shortSlice.visible, shortGroups);
|
|
assert.equal(shortSlice.hiddenOlder, 0);
|
|
|
|
const unlimited = sliceTurnWindow(groups, initialTurnWindow(0), 0);
|
|
assert.equal(unlimited.visible, groups);
|
|
assert.equal(unlimited.hiddenOlder, 0);
|
|
assert.equal(unlimited.hiddenNewer, 0);
|
|
});
|
|
|
|
test('expandOlderInWindow and expandNewerInWindow step smoothly and merge back into tail', () => {
|
|
const groups = makeGroups(20);
|
|
let state = initialTurnWindow(6);
|
|
|
|
// Expand older from tail (14..19 -> 8..19)
|
|
state = expandOlderInWindow(groups, state, 6);
|
|
let slice = sliceTurnWindow(groups, state, 6);
|
|
assert.deepEqual(slice.visible.map(g => g.turn), [8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]);
|
|
assert.equal(slice.hiddenOlder, 8);
|
|
assert.equal(slice.hiddenNewer, 0);
|
|
|
|
// Jump far into middle (turns 6..11), then expand older & newer within range
|
|
state = jumpToTurnInWindow(groups, initialTurnWindow(6), 6, 6);
|
|
assert.equal(state.kind, 'range');
|
|
slice = sliceTurnWindow(groups, state, 6);
|
|
assert.deepEqual(slice.visible.map(g => g.turn), [6, 7, 8, 9, 10, 11]);
|
|
assert.equal(slice.hiddenOlder, 6);
|
|
assert.equal(slice.hiddenNewer, 8);
|
|
|
|
state = expandOlderInWindow(groups, state, 6);
|
|
slice = sliceTurnWindow(groups, state, 6);
|
|
assert.deepEqual(slice.visible.map(g => g.turn), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);
|
|
assert.equal(slice.hiddenOlder, 0);
|
|
assert.equal(slice.hiddenNewer, 8);
|
|
|
|
// Expanding newer once -> 0..17, twice -> 0..19 merged back to tail
|
|
state = expandNewerInWindow(groups, state, 6);
|
|
assert.equal(state.kind, 'range');
|
|
state = expandNewerInWindow(groups, state, 6);
|
|
assert.equal(state.kind, 'tail');
|
|
slice = sliceTurnWindow(groups, state, 6);
|
|
assert.equal(slice.visible, groups);
|
|
assert.equal(slice.hiddenNewer, 0);
|
|
});
|
|
|
|
test('jumpToTurnInWindow opens nearby turns without splitting tail and isolates far turns', () => {
|
|
const groups = makeGroups(25);
|
|
const initial = initialTurnWindow(6); // shows 19..24
|
|
|
|
// Already visible -> no-op
|
|
assert.equal(jumpToTurnInWindow(groups, initial, 20, 6), initial);
|
|
|
|
// Nearby jump (distance 10 <= directExpandCeiling 12) -> stays in tail mode (15..24)
|
|
const near = jumpToTurnInWindow(groups, initial, 15, 6);
|
|
assert.equal(near.kind, 'tail');
|
|
const nearSlice = sliceTurnWindow(groups, near, 6);
|
|
assert.deepEqual(nearSlice.visible.map(g => g.turn), [15, 16, 17, 18, 19, 20, 21, 22, 23, 24]);
|
|
assert.equal(nearSlice.hiddenNewer, 0);
|
|
|
|
// Far jump -> range centered forward from target (2..7)
|
|
const far = jumpToTurnInWindow(groups, initial, 2, 6);
|
|
assert.equal(far.kind, 'range');
|
|
const farSlice = sliceTurnWindow(groups, far, 6);
|
|
assert.deepEqual(farSlice.visible.map(g => g.turn), [2, 3, 4, 5, 6, 7]);
|
|
assert.equal(farSlice.hiddenOlder, 2);
|
|
assert.equal(farSlice.hiddenNewer, 17);
|
|
|
|
// Jumping to tail turn resets cleanly to 6-turn tail (19..24)
|
|
const backToTail = jumpToTurnInWindow(groups, far, 23, 6);
|
|
assert.deepEqual(backToTail, { kind: 'tail', count: 6 });
|
|
|
|
// Stale rewound range key falls back safely to tail
|
|
const trimmedGroups = makeGroups(8);
|
|
const rewoundSlice = sliceTurnWindow(
|
|
trimmedGroups,
|
|
{ kind: 'range', startKey: 'turn:5', endKey: 'turn:10' },
|
|
6,
|
|
);
|
|
assert.deepEqual(rewoundSlice.visible.map(g => g.turn), [2, 3, 4, 5, 6, 7]);
|
|
});
|
|
|
|
test('countTurnsInSlice counts logical turns properly', () => {
|
|
const mixed: ReaderGroup[] = [
|
|
{ key: 'turn:0', turn: 0, keys: ['u0', 'a0'] },
|
|
{ key: 'node:misc', turn: null, keys: ['c0'] },
|
|
{ key: 'turn:1', turn: 1, keys: ['u1', 'a1'] },
|
|
];
|
|
assert.equal(countTurnsInSlice(mixed, 0, 3), 2);
|
|
assert.equal(countTurnsInSlice(mixed, 1, 2), 1);
|
|
assert.equal(countTurnsInSlice(mixed, 2, 2), 0);
|
|
});
|
|
|
|
test('stabilizeGroups keeps unchanged group references across tail updates', () => {
|
|
const prev: ReaderGroup[] = [
|
|
{ key: 'turn:0', turn: 0, keys: ['u0', 'a0'] },
|
|
{ key: 'turn:1', turn: 1, keys: ['u1', 'a1'] },
|
|
];
|
|
const identicalCopy: ReaderGroup[] = [
|
|
{ key: 'turn:0', turn: 0, keys: ['u0', 'a0'] },
|
|
{ key: 'turn:1', turn: 1, keys: ['u1', 'a1'] },
|
|
];
|
|
assert.equal(stabilizeGroups(prev, identicalCopy), prev);
|
|
|
|
const appended: ReaderGroup[] = [
|
|
{ key: 'turn:0', turn: 0, keys: ['u0', 'a0'] },
|
|
{ key: 'turn:1', turn: 1, keys: ['u1', 'a1', 'a1b'] },
|
|
{ key: 'turn:2', turn: 2, keys: ['u2'] },
|
|
];
|
|
const next = stabilizeGroups(prev, appended);
|
|
assert.equal(next[0], prev[0]);
|
|
assert.notEqual(next[1], prev[1]);
|
|
assert.equal(next[2], appended[2]);
|
|
});
|
|
|
|
test('reconcileTurnSlots skips retire animation when clipped by render window', () => {
|
|
const t0 = { key: 'turn:0' };
|
|
const t1 = { key: 'turn:1' };
|
|
const t2 = { key: 'turn:2' };
|
|
const initial = reconcileTurnSlots([], [t0, t1]);
|
|
|
|
// Simulate t0 clipped out of the render window while still in session history (`shouldRetire('turn:0') === false`),
|
|
// and later simulate t1 genuinely rewound (`shouldRetire('turn:1') === true`).
|
|
const clipped = reconcileTurnSlots(initial, [t1, t2], key => key !== 'turn:0');
|
|
assert.deepEqual(clipped.map(s => [s.group.key, s.live]), [
|
|
['turn:1', true],
|
|
['turn:2', true],
|
|
]);
|
|
|
|
const rewound = reconcileTurnSlots(clipped, [t2], key => key === 'turn:1');
|
|
assert.deepEqual(rewound.map(s => [s.group.key, s.live]), [
|
|
['turn:1', false],
|
|
['turn:2', true],
|
|
]);
|
|
});
|