feat(repos): tags, polymorphic entity_links, attachments
This commit is contained in:
26
tests/repos/attachments.test.js
Normal file
26
tests/repos/attachments.test.js
Normal file
@@ -0,0 +1,26 @@
|
||||
import { describe, it, expect, beforeEach } from 'vitest';
|
||||
import { resetDb } from '../helpers/db.js';
|
||||
import { migrateUp } from '../../lib/db/migrate.js';
|
||||
import * as attachments from '../../lib/db/repos/attachments.js';
|
||||
|
||||
beforeEach(async () => { await resetDb(); await migrateUp(); });
|
||||
|
||||
describe('attachments repo', () => {
|
||||
it('records an attachment', async () => {
|
||||
const a = await attachments.create({
|
||||
entity_type: 'page',
|
||||
entity_id: '11111111-1111-1111-1111-111111111111',
|
||||
filename: 'spec.pdf', mime_type: 'application/pdf',
|
||||
size_bytes: 1024, blob_path: 'aa/abc', checksum: 'abc'
|
||||
});
|
||||
expect(a.filename).toBe('spec.pdf');
|
||||
});
|
||||
|
||||
it('listForEntity returns descending by uploaded_at', async () => {
|
||||
const eid = '11111111-1111-1111-1111-111111111111';
|
||||
await attachments.create({ entity_type: 'page', entity_id: eid, filename: 'a', blob_path: 'a' });
|
||||
await attachments.create({ entity_type: 'page', entity_id: eid, filename: 'b', blob_path: 'b' });
|
||||
const list = await attachments.listForEntity('page', eid);
|
||||
expect(list).toHaveLength(2);
|
||||
});
|
||||
});
|
||||
27
tests/repos/links.test.js
Normal file
27
tests/repos/links.test.js
Normal file
@@ -0,0 +1,27 @@
|
||||
import { describe, it, expect, beforeEach } from 'vitest';
|
||||
import { resetDb } from '../helpers/db.js';
|
||||
import { migrateUp } from '../../lib/db/migrate.js';
|
||||
import * as links from '../../lib/db/repos/links.js';
|
||||
|
||||
beforeEach(async () => { await resetDb(); await migrateUp(); });
|
||||
|
||||
describe('links repo', () => {
|
||||
it('create + listFrom + listTo + remove', async () => {
|
||||
const a = '11111111-1111-1111-1111-111111111111';
|
||||
const b = '22222222-2222-2222-2222-222222222222';
|
||||
const link = await links.create('project', a, 'page', b, 'mentions');
|
||||
expect(link.relation).toBe('mentions');
|
||||
expect(await links.listFrom('project', a)).toHaveLength(1);
|
||||
expect(await links.listTo('page', b)).toHaveLength(1);
|
||||
await links.remove(link.id);
|
||||
expect(await links.listFrom('project', a)).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('idempotent on the unique tuple', async () => {
|
||||
const a = '11111111-1111-1111-1111-111111111111';
|
||||
const b = '22222222-2222-2222-2222-222222222222';
|
||||
const l1 = await links.create('project', a, 'page', b, 'mentions');
|
||||
const l2 = await links.create('project', a, 'page', b, 'mentions');
|
||||
expect(l2.id).toBe(l1.id);
|
||||
});
|
||||
});
|
||||
34
tests/repos/tags.test.js
Normal file
34
tests/repos/tags.test.js
Normal file
@@ -0,0 +1,34 @@
|
||||
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 tags from '../../lib/db/repos/tags.js';
|
||||
|
||||
const owner = { kind: 'user', id: null };
|
||||
beforeEach(async () => { await resetDb(); await migrateUp(); });
|
||||
|
||||
describe('tags repo', () => {
|
||||
it('upserts a tag by name', async () => {
|
||||
const t1 = await tags.upsert('homelab');
|
||||
const t2 = await tags.upsert('homelab');
|
||||
expect(t2.id).toBe(t1.id);
|
||||
});
|
||||
|
||||
it('attach + detach + listForEntity', async () => {
|
||||
const s = await spaces.create({ slug: 'h', name: 'H' }, owner);
|
||||
const t = await tags.upsert('urgent');
|
||||
await tags.attach('space', s.id, t.id);
|
||||
const list = await tags.listForEntity('space', s.id);
|
||||
expect(list).toHaveLength(1);
|
||||
await tags.detach('space', s.id, t.id);
|
||||
expect(await tags.listForEntity('space', s.id)).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('attach is idempotent', async () => {
|
||||
const s = await spaces.create({ slug: 'h', name: 'H' }, owner);
|
||||
const t = await tags.upsert('urgent');
|
||||
await tags.attach('space', s.id, t.id);
|
||||
await tags.attach('space', s.id, t.id);
|
||||
expect(await tags.listForEntity('space', s.id)).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user