feat(repos): pages with auto-revisions, refs with upsertByExternal

This commit is contained in:
root
2026-05-31 02:17:01 +10:00
parent 652f7c3894
commit c891c495bb
4 changed files with 244 additions and 0 deletions

33
tests/repos/pages.test.js Normal file
View File

@@ -0,0 +1,33 @@
import { describe, it, expect, beforeEach } from 'vitest';
import { resetDb } from '../helpers/db.js';
import { migrateUp } from '../../lib/db/migrate.js';
import * as spaces from '../../lib/db/repos/spaces.js';
import * as pages from '../../lib/db/repos/pages.js';
const owner = { kind: 'user', id: null };
beforeEach(async () => { await resetDb(); await migrateUp(); });
describe('pages repo', () => {
it('creates a page and auto-snapshots a revision', async () => {
const s = await spaces.create({ slug: 'h', name: 'H' }, owner);
const p = await pages.create(
{ space_id: s.id, slug: 'a', title: 'A', body_md: 'hello' }, owner
);
expect(p.body_md).toBe('hello');
const revs = await pages.listRevisions(p.id);
expect(revs).toHaveLength(1);
expect(revs[0].body_md).toBe('hello');
});
it('updating body adds a revision', async () => {
const s = await spaces.create({ slug: 'h', name: 'H' }, owner);
const p = await pages.create(
{ space_id: s.id, slug: 'a', title: 'A', body_md: 'v1' }, owner
);
await pages.update(p.id, { body_md: 'v2' }, owner);
const revs = await pages.listRevisions(p.id);
expect(revs).toHaveLength(2);
expect(revs[0].body_md).toBe('v2'); // newest first
});
});

35
tests/repos/refs.test.js Normal file
View File

@@ -0,0 +1,35 @@
import { describe, it, expect, beforeEach } from 'vitest';
import { resetDb } from '../helpers/db.js';
import { migrateUp } from '../../lib/db/migrate.js';
import * as spaces from '../../lib/db/repos/spaces.js';
import * as refs from '../../lib/db/repos/refs.js';
const owner = { kind: 'user', id: null };
beforeEach(async () => { await resetDb(); await migrateUp(); });
describe('refs repo', () => {
it('creates a url ref', async () => {
const s = await spaces.create({ slug: 'h', name: 'H' }, owner);
const r = await refs.create({
space_id: s.id, kind: 'url',
source_url: 'https://example.com',
title: 'Ex', source_kind: 'manual'
}, owner);
expect(r.kind).toBe('url');
expect(r.status).toBe('ingested');
});
it('idempotent upsert by (source_kind, external_id)', async () => {
const s = await spaces.create({ slug: 'h', name: 'H' }, owner);
const r1 = await refs.upsertByExternal({
space_id: s.id, kind: 'url', source_url: 'https://e.com',
source_kind: 'karakeep', external_id: 'kk-123', title: 'v1'
}, owner);
const r2 = await refs.upsertByExternal({
space_id: s.id, kind: 'url', source_url: 'https://e.com',
source_kind: 'karakeep', external_id: 'kk-123', title: 'v2'
}, owner);
expect(r2.id).toBe(r1.id);
expect(r2.title).toBe('v2');
});
});