test(server): cover background extraction, the legacy memory copy and run results

This commit is contained in:
shumeiko.it
2026-10-24 12:00:00 +02:00
parent 2d507dc457
commit 101523e463
3 changed files with 274 additions and 0 deletions

View File

@@ -0,0 +1,112 @@
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import Database from 'better-sqlite3';
import { afterEach, describe, expect, it } from 'vitest';
import { WaggleDB } from '@waggle/core';
import { createInMemoryMemoryStore, PERSONAL_SCOPE, workspaceScope } from '@waggle/memory-contract';
import { importLegacyMemories, mapFrame } from '../../src/local/legacy-memory-import.js';
const dirs: string[] = [];
function tempDir(): string {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-legacy-import-'));
dirs.push(dir);
return dir;
}
afterEach(() => {
for (const dir of dirs.splice(0)) fs.rmSync(dir, { recursive: true, force: true });
});
/** Writes a minimal legacy .mind file with the given frames. */
function legacyMind(file: string, rows: Array<{ content: string; importance?: string; source?: string }>): void {
const db = new Database(file);
db.exec(`CREATE TABLE memory_frames (
id INTEGER PRIMARY KEY AUTOINCREMENT, content TEXT NOT NULL,
importance TEXT NOT NULL DEFAULT 'normal', source TEXT, created_at TEXT
)`);
const insert = db.prepare('INSERT INTO memory_frames (content, importance, source, created_at) VALUES (?, ?, ?, ?)');
for (const row of rows) insert.run(row.content, row.importance ?? 'normal', row.source ?? 'user_stated', '2026-05-01 10:00:00');
db.close();
}
describe('mapFrame', () => {
it('turns content prefixes into kinds and strips lane headers', () => {
expect(mapFrame({ content: '[mind-fact]\nLives in Novi Sad', importance: 'normal', source: 'system' }))
.toEqual({ text: 'Lives in Novi Sad', kind: 'fact', importance: 'normal', source: 'system' });
expect(mapFrame({ content: '[mind-profile Mila]\nDesigner', importance: 'normal', source: null })?.kind).toBe('profile');
expect(mapFrame({ content: '[mind-event]\n[2026-05-01] Moved', importance: 'normal', source: null })?.kind).toBe('event');
expect(mapFrame({ content: 'Decision: use Postgres', importance: 'important', source: 'user_stated' })?.kind).toBe('decision');
expect(mapFrame({ content: 'User preference: short answers', importance: 'normal', source: 'user_stated' }))
.toMatchObject({ text: 'User preference: short answers', kind: 'preference' });
expect(mapFrame({ content: 'Plain fact', importance: 'normal', source: 'weird' })?.source).toBe('import');
});
it('drops deprecated frames, raw dialogue turns and empty text', () => {
expect(mapFrame({ content: 'Old', importance: 'deprecated', source: null })).toBeNull();
expect(mapFrame({ content: '[mind-rawturn conv=1 turn=2 speaker=user]\nhello', importance: 'normal', source: null })).toBeNull();
expect(mapFrame({ content: ' ', importance: 'normal', source: null })).toBeNull();
});
});
describe('importLegacyMemories', () => {
it('copies each scope once and leaves the legacy files unchanged', async () => {
const dir = tempDir();
const personal = path.join(dir, 'personal.mind');
const workspace = path.join(dir, 'workspace.mind');
legacyMind(personal, [
{ content: 'User preference: answers in Russian' },
{ content: 'Obsolete', importance: 'deprecated' },
]);
legacyMind(workspace, [{ content: 'Decision: ship on Friday', importance: 'important' }]);
const before = [fs.readFileSync(personal), fs.readFileSync(workspace)];
const memory = createInMemoryMemoryStore();
const waggleDb = new WaggleDB(path.join(dir, 'waggle.db'));
const sources = [
{ path: personal, scope: PERSONAL_SCOPE },
{ path: workspace, scope: workspaceScope('ws1') },
{ path: path.join(dir, 'missing.mind'), scope: workspaceScope('ws2') },
];
const reports = await importLegacyMemories(memory, waggleDb, sources);
expect(reports.map((r) => [r.scope, r.status, r.copied, r.skipped])).toEqual([
['personal', 'imported', 1, 1],
['workspace:ws1', 'imported', 1, 0],
['workspace:ws2', 'missing', 0, 0],
]);
const personalMemories = await memory.list({ scopes: [PERSONAL_SCOPE] });
expect(personalMemories).toMatchObject([
{ text: 'User preference: answers in Russian', kind: 'preference', origin: 'legacy-import' },
]);
expect(personalMemories[0]!.metadata).toEqual({ legacy_created_at: '2026-05-01 10:00:00' });
expect(await memory.list({ scopes: [workspaceScope('ws1')] })).toMatchObject([
{ text: 'Decision: ship on Friday', kind: 'decision', importance: 'important' },
]);
const again = await importLegacyMemories(memory, waggleDb, sources);
expect(again.map((r) => r.status)).toEqual(['already-imported', 'already-imported', 'already-imported']);
expect(await memory.count({ scopes: [PERSONAL_SCOPE, workspaceScope('ws1')] })).toBe(2);
expect([fs.readFileSync(personal), fs.readFileSync(workspace)]).toEqual(before);
waggleDb.close();
});
it('resumes after a cancelled copy', async () => {
const dir = tempDir();
const personal = path.join(dir, 'personal.mind');
legacyMind(personal, [{ content: 'First fact' }, { content: 'Second fact' }, { content: 'Third fact' }]);
const memory = createInMemoryMemoryStore();
const waggleDb = new WaggleDB(path.join(dir, 'waggle.db'));
let calls = 0;
const cancelled = await importLegacyMemories(
memory, waggleDb, [{ path: personal, scope: PERSONAL_SCOPE }], undefined, () => ++calls > 2,
);
expect(cancelled[0]!.copied).toBe(1);
const resumed = await importLegacyMemories(memory, waggleDb, [{ path: personal, scope: PERSONAL_SCOPE }]);
expect(resumed[0]).toMatchObject({ status: 'imported', copied: 2 });
expect((await memory.list({ scopes: [PERSONAL_SCOPE] })).map((r) => r.text).sort())
.toEqual(['First fact', 'Second fact', 'Third fact']);
waggleDb.close();
});
});

View File

@@ -0,0 +1,113 @@
import { afterEach, describe, expect, it, vi } from 'vitest';
import {
createInMemoryMemoryStore,
PERSONAL_SCOPE,
type MemoryStore,
type RememberInput,
} from '@waggle/memory-contract';
import { MemoryExtractionQueue } from '../../src/local/memory-extraction.js';
/** An in-memory store that claims extraction and records the conversations it gets. */
function extractingStore(): { store: MemoryStore; calls: RememberInput[] } {
const inner = createInMemoryMemoryStore();
const calls: RememberInput[] = [];
const store: MemoryStore = {
...inner,
capabilities: { ...inner.capabilities, extraction: true },
remember: async (input) => {
calls.push(input);
return [];
},
};
return { store, calls };
}
afterEach(() => {
vi.useRealTimers();
});
describe('MemoryExtractionQueue', () => {
it('extracts a batch once enough messages are queued', async () => {
const { store, calls } = extractingStore();
const queue = new MemoryExtractionQueue({ memory: store, isEnabled: () => true, batchMessages: 4 });
queue.record('s1', 'I moved to Novi Sad.', 'Noted.');
expect(calls).toHaveLength(0);
queue.record('s1', 'I work as a designer.', 'Got it.');
await queue.flush('s1');
expect(calls).toHaveLength(1);
expect(calls[0]).toMatchObject({
scope: PERSONAL_SCOPE,
kind: 'profile',
origin: 'extraction',
sessionId: 's1',
conversation: [
{ role: 'user', content: 'I moved to Novi Sad.' },
{ role: 'assistant', content: 'Noted.' },
{ role: 'user', content: 'I work as a designer.' },
{ role: 'assistant', content: 'Got it.' },
],
});
queue.stop();
});
it('extracts pending messages after a pause', async () => {
vi.useFakeTimers();
const { store, calls } = extractingStore();
const queue = new MemoryExtractionQueue({ memory: store, isEnabled: () => true, batchMessages: 10, idleMs: 1000 });
queue.record('s1', 'My daughter is called Ana.', 'Lovely name.');
await vi.advanceTimersByTimeAsync(999);
expect(calls).toHaveLength(0);
await vi.advanceTimersByTimeAsync(1);
await queue.flush();
expect(calls).toHaveLength(1);
queue.stop();
});
it('keeps sessions apart', async () => {
const { store, calls } = extractingStore();
const queue = new MemoryExtractionQueue({ memory: store, isEnabled: () => true, batchMessages: 10 });
queue.record('a', 'I like tea.', 'OK.');
queue.record('b', 'I like coffee.', 'OK.');
await queue.flush();
expect(calls.map((c) => c.sessionId).sort()).toEqual(['a', 'b']);
queue.stop();
});
it('collects nothing when the setting is off or the store cannot extract', async () => {
const { store, calls } = extractingStore();
let enabled = false;
const queue = new MemoryExtractionQueue({ memory: store, isEnabled: () => enabled, batchMessages: 2 });
queue.record('s1', 'I live in Belgrade.', 'OK.');
await queue.flush();
expect(calls).toHaveLength(0);
enabled = true;
const plain = new MemoryExtractionQueue({ memory: createInMemoryMemoryStore(), isEnabled: () => true, batchMessages: 2 });
expect(plain.active).toBe(false);
queue.stop();
plain.stop();
});
it('reports a failed extraction without throwing', async () => {
const { store } = extractingStore();
const failing: MemoryStore = { ...store, remember: async () => { throw new Error('model unreachable'); } };
const onError = vi.fn();
const queue = new MemoryExtractionQueue({ memory: failing, isEnabled: () => true, batchMessages: 2, onError });
queue.record('s1', 'I live in Belgrade.', 'OK.');
await queue.flush();
expect(onError).toHaveBeenCalledWith(expect.objectContaining({ message: 'model unreachable' }));
queue.stop();
});
it('skips batches without a user message and trims long replies', async () => {
const { store, calls } = extractingStore();
const queue = new MemoryExtractionQueue({ memory: store, isEnabled: () => true, batchMessages: 2 });
queue.record('s1', ' ', 'x'.repeat(10_000));
await queue.flush();
expect(calls).toHaveLength(0);
queue.record('s2', 'Hi, I am Mila.', 'y'.repeat(10_000));
await queue.flush();
expect(calls[0]?.conversation?.[1]?.content).toHaveLength(4000);
queue.stop();
});
});

View File

@@ -0,0 +1,49 @@
import { describe, expect, it } from 'vitest';
import {
createInMemoryMemoryStore,
PERSONAL_SCOPE,
workspaceScope,
type MemoryStore,
} from '@waggle/memory-contract';
import { recordRunResult } from '../../src/local/run-memory.js';
const input = {
workspaceId: 'ws1',
personalText: '[Agent run]\nRun: r1\nSummary: done',
workspaceText: '[Agent run result]\nRun: r1\nResult: done',
metadata: { runId: 'r1', source: 'fleet', kind: 'worker' },
};
describe('recordRunResult', () => {
it('writes an index entry to personal memory and the result to the workspace', async () => {
const memory = createInMemoryMemoryStore();
const refs = await recordRunResult(memory, input);
expect(refs.status).toBe('complete');
const [personal] = await memory.list({ scopes: [PERSONAL_SCOPE] });
const [workspace] = await memory.list({ scopes: [workspaceScope('ws1')] });
expect(refs.personalFrameIds).toEqual([personal!.id]);
expect(refs.workspaceFrameIds).toEqual({ ws1: [workspace!.id] });
expect(workspace).toMatchObject({ kind: 'note', origin: 'agent-runs', source: 'agent_inferred' });
});
it('renames metadata keys the contract reserves', async () => {
const memory = createInMemoryMemoryStore();
await recordRunResult(memory, input);
const [record] = await memory.list({ scopes: [PERSONAL_SCOPE] });
expect(record!.metadata).toEqual({ runId: 'r1', run_source: 'fleet', run_kind: 'worker' });
});
it('reports a partial result when one write fails', async () => {
const inner = createInMemoryMemoryStore();
const memory: MemoryStore = {
...inner,
remember: async (args) => {
if (args.scope.kind === 'workspace') throw new Error('disk full');
return inner.remember(args);
},
};
const refs = await recordRunResult(memory, input);
expect(refs.status).toBe('partial');
expect(refs.workspaceFrameIds).toEqual({});
});
});