79 lines
2.4 KiB
JavaScript
79 lines
2.4 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import test from 'node:test';
|
|
import {
|
|
formatSessionStreamSseChunk,
|
|
isSessionStreamReplayEnabled,
|
|
isTerminalSessionEvent,
|
|
parseSessionSseBlock,
|
|
parseSessionStreamLastEventId,
|
|
resolveUpstreamResumeEventId,
|
|
shouldPersistSessionStreamEvent,
|
|
shouldSkipUpstreamAfterSessionReplay,
|
|
} from './session-stream.mjs';
|
|
|
|
test('formatSessionStreamSseChunk includes id for replay cursor', () => {
|
|
const chunk = formatSessionStreamSseChunk({
|
|
id: 'evt-1',
|
|
data: { type: 'Message', message: { role: 'assistant' } },
|
|
});
|
|
assert.match(chunk, /^id: evt-1\n/);
|
|
assert.match(chunk, /"type":"Message"/);
|
|
});
|
|
|
|
test('parseSessionSseBlock extracts id and data', () => {
|
|
const parsed = parseSessionSseBlock('id: upstream-1\ndata: {"type":"Finish"}\n\n');
|
|
assert.equal(parsed.id, 'upstream-1');
|
|
assert.equal(parsed.data, '{"type":"Finish"}');
|
|
});
|
|
|
|
test('terminal detection and upstream skip policy', () => {
|
|
assert.equal(isTerminalSessionEvent({ type: 'Finish' }), true);
|
|
assert.equal(isTerminalSessionEvent({ type: 'Message' }), false);
|
|
assert.equal(
|
|
shouldSkipUpstreamAfterSessionReplay([
|
|
{ payload: { type: 'Message' } },
|
|
{ payload: { type: 'Finish' } },
|
|
]),
|
|
true,
|
|
);
|
|
assert.equal(
|
|
shouldSkipUpstreamAfterSessionReplay([{ payload: { type: 'Message' } }]),
|
|
false,
|
|
);
|
|
assert.equal(shouldPersistSessionStreamEvent({ type: 'Ping' }), false);
|
|
assert.equal(shouldPersistSessionStreamEvent({ type: 'Message' }), true);
|
|
assert.equal(isSessionStreamReplayEnabled({ MEMIND_SESSION_STREAM_REPLAY: '1' }), true);
|
|
assert.equal(parseSessionStreamLastEventId(' abc '), 'abc');
|
|
});
|
|
|
|
test('upstream resume cursor never falls back to a Portal replay id', () => {
|
|
assert.equal(
|
|
resolveUpstreamResumeEventId([], {
|
|
id: 'portal-uuid',
|
|
upstreamEventId: null,
|
|
}),
|
|
null,
|
|
);
|
|
assert.equal(
|
|
resolveUpstreamResumeEventId([], {
|
|
id: 'portal-uuid',
|
|
upstreamEventId: 'upstream-7',
|
|
}),
|
|
'upstream-7',
|
|
);
|
|
});
|
|
|
|
test('upstream resume cursor uses the newest mappable replay event across turns', () => {
|
|
assert.equal(
|
|
resolveUpstreamResumeEventId(
|
|
[
|
|
{ upstreamEventId: 'upstream-old-finish' },
|
|
{ upstreamEventId: 'upstream-new-message' },
|
|
{ upstreamEventId: null },
|
|
],
|
|
{ upstreamEventId: 'upstream-cursor' },
|
|
),
|
|
'upstream-new-message',
|
|
);
|
|
});
|