46 lines
2.5 KiB
JavaScript
46 lines
2.5 KiB
JavaScript
import { describe, it, expect, beforeAll } from 'vitest';
|
|
import { DatabaseSync } from 'node:sqlite';
|
|
import { tmpdir } from 'node:os';
|
|
import { join } from 'node:path';
|
|
import { randomUUID } from 'node:crypto';
|
|
import { pool } from '../../lib/db/pool.js';
|
|
import { resetDb } from '../helpers/db.js';
|
|
import { migrateUp } from '../../lib/db/migrate.js';
|
|
import { ensureSpace } from '../../migrate/spaces.js';
|
|
import { importVoid1 } from '../../migrate/sources/void1.js';
|
|
|
|
let dbPath, spaceId;
|
|
beforeAll(async () => {
|
|
await resetDb(); await migrateUp(); spaceId = await ensureSpace('void1', 'Void 1');
|
|
dbPath = join(tmpdir(), `void1-${randomUUID()}.db`);
|
|
const db = new DatabaseSync(dbPath);
|
|
db.exec(`CREATE TABLE wiki_pages(id TEXT,slug TEXT,title TEXT,content TEXT,tags TEXT,parent_id TEXT,created_at INT,updated_at INT);
|
|
CREATE TABLE projects(id TEXT,name TEXT,description TEXT,status TEXT);
|
|
CREATE TABLE project_tasks(id TEXT,project_id TEXT,text TEXT,done INT,position INT);
|
|
CREATE TABLE project_journal(id TEXT,project_id TEXT,content TEXT,created_at INT);
|
|
CREATE TABLE conversations(id TEXT,project_id TEXT,title TEXT,created_at INT);
|
|
CREATE TABLE messages(id TEXT,conversation_id TEXT,role TEXT,content TEXT,created_at INT);`);
|
|
db.prepare(`INSERT INTO wiki_pages VALUES('w1','home','Home','welcome','',NULL,0,0)`).run();
|
|
db.prepare(`INSERT INTO projects VALUES('p1','Homelab','my lab','active')`).run();
|
|
db.prepare(`INSERT INTO project_tasks VALUES('t1','p1','do a thing',0,1)`).run();
|
|
db.prepare(`INSERT INTO conversations VALUES('c1','p1','Chat 1',0)`).run();
|
|
db.prepare(`INSERT INTO messages VALUES('m1','c1','user','hi',0)`).run();
|
|
db.prepare(`INSERT INTO messages VALUES('m2','c1','assistant','hello',1)`).run();
|
|
db.close();
|
|
});
|
|
|
|
describe('void1 importer', () => {
|
|
it('maps wiki/projects/tasks/conversations/messages, idempotently', async () => {
|
|
const r1 = await importVoid1({ sqlitePath: dbPath, spaceId });
|
|
expect(r1.pages).toBeGreaterThanOrEqual(1);
|
|
expect(r1.projects).toBe(1);
|
|
expect(r1.tasks).toBe(1);
|
|
expect(r1.conversations).toBe(1);
|
|
expect(r1.messages).toBe(2);
|
|
const page = await pool.query(`SELECT title FROM pages WHERE space_id=$1 AND title='Home'`, [spaceId]);
|
|
expect(page.rows.length).toBe(1);
|
|
const r2 = await importVoid1({ sqlitePath: dbPath, spaceId }); // re-run
|
|
expect(r2.pages + r2.projects + r2.tasks + r2.conversations + r2.messages).toBe(0);
|
|
});
|
|
});
|