113 lines
5.7 KiB
TypeScript
113 lines
5.7 KiB
TypeScript
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();
|
|
});
|
|
});
|