89 lines
3.9 KiB
JavaScript
89 lines
3.9 KiB
JavaScript
'use strict';
|
|
const test = require('node:test'),
|
|
assert = require('node:assert/strict'),
|
|
fs = require('node:fs'),
|
|
os = require('node:os'),
|
|
path = require('node:path'),
|
|
crypto = require('node:crypto');
|
|
const { AssistantStore } = require('../../src/modules/assistant/store.cjs');
|
|
function fixture(t) {
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'conversation-lifecycle-')),
|
|
store = new AssistantStore(dir);
|
|
t.after(() => {
|
|
store.close();
|
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
});
|
|
return store;
|
|
}
|
|
test('archiving releases active conversation quota, preserves history and remains owner scoped', (t) => {
|
|
const store = fixture(t),
|
|
items = Array.from({ length: 50 }, () => store.create('owner'));
|
|
assert.equal(typeof store.archive, 'function');
|
|
assert.throws(() => store.create('owner'), { status: 409 });
|
|
assert.throws(() => store.archive(items[0].id, 'other', { version: 1 }), { status: 404 });
|
|
const archived = store.archive(items[0].id, 'owner', { version: 1 });
|
|
assert.equal(archived.archived, true);
|
|
store.create('owner');
|
|
assert.equal(store.conversations('owner').length, 50);
|
|
assert.equal(store.listConversations('owner', { scope: 'archived' }).total, 1);
|
|
assert.throws(() => store.restore(items[0].id, 'owner', { version: archived.version }), {
|
|
status: 409,
|
|
});
|
|
store.archive(items[1].id, 'owner', { version: 1 });
|
|
assert.equal(store.restore(items[0].id, 'owner', { version: archived.version }).archived, false);
|
|
});
|
|
test('running turns cannot be archived and archived conversations cannot execute or accept messages', (t) => {
|
|
const store = fixture(t),
|
|
conversation = store.create('owner');
|
|
assert.equal(typeof store.archive, 'function');
|
|
const turn = store.beginTurn(conversation.id, 'owner', 'synthetic message');
|
|
assert.throws(
|
|
() =>
|
|
store.archive(conversation.id, 'owner', {
|
|
version: store.conversation(conversation.id, 'owner').version,
|
|
}),
|
|
{ status: 409 },
|
|
);
|
|
store.updateTurn(turn.id, 'success', { reply: 'synthetic answer' });
|
|
const saved = store.archive(conversation.id, 'owner', {
|
|
version: store.conversation(conversation.id, 'owner').version,
|
|
});
|
|
assert.throws(() => store.beginTurn(conversation.id, 'owner', 'new'), { status: 409 });
|
|
assert.throws(() => store.action(conversation.id, 'owner', 'none', () => {}), { status: 409 });
|
|
assert.equal(store.conversation(conversation.id, 'owner').turns[0].reply, 'synthetic answer');
|
|
assert.equal(saved.archived, true);
|
|
});
|
|
test('a full conversation continues once with a bounded private history and retains action evidence', (t) => {
|
|
const store = fixture(t),
|
|
parent = store.create('owner');
|
|
assert.equal(typeof store.continueConversation, 'function');
|
|
for (let i = 0; i < 100; i++) {
|
|
const id = crypto.randomUUID();
|
|
store.db.prepare('INSERT INTO turns VALUES(?,?,?,?,?,?)').run(
|
|
id,
|
|
parent.id,
|
|
'success',
|
|
store.encode(id, {
|
|
text: 'line-' + i,
|
|
reply: 'answer-' + i,
|
|
actions: i === 0 ? [{ id: 'evidence', status: 'success', result: { verified: true } }] : [],
|
|
evidence: [],
|
|
}),
|
|
'2020-01-01',
|
|
'2020-01-01T00:00:00Z',
|
|
);
|
|
}
|
|
const next = store.continueConversation(parent.id, 'owner', { version: 1 });
|
|
assert.equal(next.continuationOf, parent.id);
|
|
assert.equal(next.turns.length, 0);
|
|
assert.equal(store.conversation(parent.id, 'owner').archived, true);
|
|
assert.equal(store.conversation(parent.id, 'owner').turns[0].actions[0].result.verified, true);
|
|
assert.equal(store.continueConversation(parent.id, 'owner', { version: 1 }).id, next.id);
|
|
const history = store.history(next.id, 'owner');
|
|
assert.equal(history.length, 3);
|
|
assert.equal(history[0].text, 'line-97');
|
|
store.beginTurn(next.id, 'owner', 'follow up');
|
|
assert.equal(store.conversation(next.id, 'owner').continuationOf, parent.id);
|
|
assert.throws(() => store.history(next.id, 'other'), { status: 404 });
|
|
});
|