Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

feat(catalog): persist scraps on bulk-import + route-level DB tests

[._.]/ Adam Eivyc0bfe483d78c
diff --git a/server/routes/catalog.js b/server/routes/catalog.js
index 62891161b7..0c5aaabcec 100644
--- a/server/routes/catalog.js
+++ b/server/routes/catalog.js
@@ -420,6 +420,10 @@ router.post('/bulk-import', asyncHandler(async (req, res) => {
// Zod-validated shape so we can stamp each ref link with its original role.
const entries = [];
const perRowRoles = [];
+ // Round-tripped scraps (markdown `### Scraps`) ride as a non-enumerable
+ // `entry.scraps` sibling — captured here alongside the Zod-validated shape so
+ // they can be persisted as catalog_scraps rows in the same transaction below.
+ const perRowScraps = [];
for (let i = 0; i < parsed.length; i++) {
const entry = parsed[i];
// Merge default tags onto each row (dedup), then validate the full
@@ -432,6 +436,7 @@ router.post('/bulk-import', asyncHandler(async (req, res) => {
}
entries.push(result.data);
perRowRoles.push(typeof entry.roleForExportedRef === 'string' ? entry.roleForExportedRef : null);
+ perRowScraps.push(Array.isArray(entry.scraps) ? entry.scraps : []);
}
// Optional ref-links: same shape as the /link route, applied once per
@@ -464,6 +469,7 @@ router.post('/bulk-import', asyncHandler(async (req, res) => {
const seeds = entries.map((e) => ingredientEmbedSeed(e));
const embeds = await embedBatch(seeds);
+ let scrapsCreated = 0;
const created = await withTransaction(async (client) => {
const out = [];
for (let i = 0; i < entries.length; i++) {
@@ -495,6 +501,22 @@ router.post('/bulk-import', asyncHandler(async (req, res) => {
[ing.id, target.refKind, target.refId, role],
);
}
+ // Persist any round-tripped scraps as catalog_scraps rows + source links
+ // in the same transaction, so they roll back with the ingredient on a
+ // mid-batch failure. Only markdown imports carry scraps today; JSON/CSV
+ // rows have an empty list. No embedding is generated for an imported scrap
+ // (the ingredient already carries the searchable embedding) — the scrap is
+ // provenance, not a separate search target.
+ for (const s of perRowScraps[i]) {
+ if (!s || typeof s.rawText !== 'string' || !s.rawText.trim()) continue;
+ const scrap = await catalogDB.createScrap({
+ rawText: s.rawText,
+ sourceKind: (typeof s.sourceKind === 'string' && s.sourceKind.trim() ? s.sourceKind.trim() : 'import').slice(0, 32),
+ metadata: { importedVia: 'bulk-import', format },
+ }, { client });
+ await catalogDB.linkIngredientToSource(ing.id, scrap.id, null, { client });
+ scrapsCreated++;
+ }
out.push({ id: ing.id, type: ing.type, name: ing.name });
}
return out;
@@ -503,8 +525,8 @@ router.post('/bulk-import', asyncHandler(async (req, res) => {
// Parser warnings (today: unrecognized markdown type headings) ride
// back on the response so the user notices typos like `## Plce: …`.
const warnings = Array.isArray(parsed.warnings) ? parsed.warnings : [];
- console.log(`📥 Catalog bulk import: ${format} → ${created.length} ingredient(s)${warnings.length ? ` (${warnings.length} warning(s))` : ''}`);
- res.status(201).json({ created, count: created.length, warnings });
+ console.log(`📥 Catalog bulk import: ${format} → ${created.length} ingredient(s)${scrapsCreated ? `, ${scrapsCreated} scrap(s)` : ''}${warnings.length ? ` (${warnings.length} warning(s))` : ''}`);
+ res.status(201).json({ created, count: created.length, scrapsCreated, warnings });
}));
// Export one ref slice (universe/series/issue/work) as a portable bundle.
diff --git a/server/routes/catalog.test.js b/server/routes/catalog.test.js
new file mode 100644
index 0000000000..16bf6eb748
--- /dev/null
+++ b/server/routes/catalog.test.js
@@ -0,0 +1,190 @@
+/**
+ * Route-level Postgres integration tests for the catalog HTTP contract.
+ *
+ * Covers the producer↔consumer seams that the parser/DB unit tests don't reach:
+ * - POST /bulk-import persists round-tripped `### Scraps` into catalog_scraps
+ * + catalog_ingredient_sources rows in the same transaction.
+ * - POST /bulk-import recreates an export bundle's ref link from `bundleRef`
+ * when no `defaults.*Ref` overrides it, honoring per-row `roleForExportedRef`.
+ * - POST /ingredients/:id/revisions/:revisionId/restore restores the revision's
+ * payload VERBATIM, preserving its captured `payload.schemaVersion`, and
+ * records the restore as a new (auditable) revision.
+ *
+ * Needs a live Postgres with the catalog schema (same probe as
+ * services/catalogDB.test.js); SKIPS cleanly when unreachable. Embeddings are
+ * mocked so the route never reaches an AI provider.
+ */
+
+import { describe, it, expect, afterAll, vi } from 'vitest';
+import express from 'express';
+import { request } from '../lib/testHelper.js';
+import { errorMiddleware } from '../lib/errorHandler.js';
+import { checkHealth, ensureSchema, close } from '../lib/db.js';
+
+// Mock embeddings — the bulk-import + restore routes call these; we don't want a
+// network round-trip and the assertions never inspect the vector.
+vi.mock('../services/embeddings.js', () => ({
+ embedBatch: vi.fn(async (seeds) => (seeds || []).map(() => ({ embedding: null, model: null }))),
+ ingredientEmbedSeed: vi.fn((e) => e),
+ embedIngredient: vi.fn(async () => ({})),
+}));
+
+const catalogDB = await import('../services/catalogDB.js');
+const router = (await import('./catalog.js')).default;
+
+// Probe the DB ONCE at module load (top-level await) so describe.skipIf reports
+// SKIPPED rather than zero-assertion green when Postgres is unreachable.
+let dbReady = false;
+let skipReason = '';
+{
+ const health = await checkHealth().catch((e) => ({ connected: false, error: e?.message }));
+ if (!health.connected) {
+ skipReason = `Postgres not reachable (${health.error || 'no connection'})`;
+ } else {
+ await ensureSchema().catch(() => {});
+ const recheck = await checkHealth().catch(() => ({ hasCatalogSchema: false }));
+ if (recheck.hasCatalogSchema) dbReady = true;
+ else skipReason = 'catalog schema not present';
+ }
+}
+if (!dbReady) console.log(`⏭️ routes/catalog.test: skipping suite — ${skipReason || 'no database'}`);
+
+function makeApp() {
+ const app = express();
+ app.use(express.json({ limit: '4mb' }));
+ app.use('/api/catalog', router);
+ app.use(errorMiddleware);
+ return app;
+}
+
+const createdIngredientIds = new Set();
+const createdScrapIds = new Set();
+
+afterAll(async () => {
+ if (!dbReady) return;
+ for (const id of createdIngredientIds) {
+ await catalogDB.deleteIngredient(id, { hard: true }).catch(() => {});
+ }
+ for (const id of createdScrapIds) {
+ await catalogDB.deleteScrap(id, { hard: true }).catch(() => {});
+ }
+ await close();
+});
+
+describe.skipIf(!dbReady)('POST /api/catalog/bulk-import — scrap persistence', () => {
+ it('persists a round-tripped `### Scraps` bullet as a catalog_scraps row + source link', async () => {
+ const markdown = [
+ '## Character: Scrap Persist Hero',
+ '',
+ 'A protagonist used to verify scrap persistence.',
+ '',
+ 'tags: test-bulk-scrap',
+ '',
+ '### Scraps',
+ '- (paste) Original notes captured for this hero.',
+ ].join('\n');
+
+ const r = await request(makeApp())
+ .post('/api/catalog/bulk-import')
+ .send({ format: 'markdown', payload: markdown });
+
+ expect(r.status).toBe(201);
+ expect(r.body.count).toBe(1);
+ expect(r.body.scrapsCreated).toBe(1);
+ const ing = r.body.created[0];
+ createdIngredientIds.add(ing.id);
+
+ const sources = await catalogDB.listSourcesForIngredient(ing.id);
+ expect(sources).toHaveLength(1);
+ const scrapId = sources[0].scrapId;
+ createdScrapIds.add(scrapId);
+ const scrap = await catalogDB.getScrap(scrapId);
+ expect(scrap.rawText).toBe('Original notes captured for this hero.');
+ expect(scrap.sourceKind).toBe('paste');
+ });
+
+ it('creates no scrap rows for a JSON import (no scraps carried)', async () => {
+ const r = await request(makeApp())
+ .post('/api/catalog/bulk-import')
+ .send({ format: 'json', payload: JSON.stringify([{ type: 'idea', name: 'Scrapless Idea', payload: { description: 'x' } }]) });
+
+ expect(r.status).toBe(201);
+ expect(r.body.scrapsCreated).toBe(0);
+ createdIngredientIds.add(r.body.created[0].id);
+ const sources = await catalogDB.listSourcesForIngredient(r.body.created[0].id);
+ expect(sources).toHaveLength(0);
+ });
+});
+
+describe.skipIf(!dbReady)('POST /api/catalog/bulk-import — export-bundle ref recreation', () => {
+ it('recreates the bundle ref link from `bundleRef` and honors per-row role', async () => {
+ const seriesId = `test-series-${Date.now()}`;
+ const bundle = {
+ version: 1,
+ ref: { kind: 'series', id: seriesId },
+ ingredients: [
+ { type: 'character', name: 'Bundle Cast A', payload: { physicalDescription: 'a' }, roleForExportedRef: 'lead' },
+ { type: 'character', name: 'Bundle Cast B', payload: { physicalDescription: 'b' } },
+ ],
+ };
+
+ const r = await request(makeApp())
+ .post('/api/catalog/bulk-import')
+ .send({ format: 'json', payload: JSON.stringify(bundle) });
+
+ expect(r.status).toBe(201);
+ expect(r.body.count).toBe(2);
+ for (const c of r.body.created) createdIngredientIds.add(c.id);
+
+ const linked = await catalogDB.listIngredientsForRef('series', seriesId);
+ expect(linked.map((x) => x.ingredient.name).sort()).toEqual(['Bundle Cast A', 'Bundle Cast B']);
+ // Per-row role precedence: row A carried `roleForExportedRef: 'lead'`, row B
+ // fell back to the `bulk-<kind>` default.
+ const roleByName = Object.fromEntries(linked.map((x) => [x.ingredient.name, x.role]));
+ expect(roleByName['Bundle Cast A']).toBe('lead');
+ expect(roleByName['Bundle Cast B']).toBe('bulk-series');
+ });
+});
+
+describe.skipIf(!dbReady)('POST /api/catalog/ingredients/:id/revisions/:revisionId/restore', () => {
+ it('restores the revision payload verbatim, preserving its schemaVersion, and records a new revision', async () => {
+ // Seed an ingredient, then write an "old shape" payload (schemaVersion 0) so
+ // a later restore can prove the marker is preserved, not re-stamped.
+ const ing = await catalogDB.createIngredient({ type: 'concept', name: 'Restore Probe', payload: { description: 'v-current' } });
+ createdIngredientIds.add(ing.id);
+
+ await catalogDB.updateIngredient(ing.id, { payload: { schemaVersion: 0, description: 'old-shape' } });
+ await catalogDB.updateIngredient(ing.id, { payload: { schemaVersion: 99, description: 'new-shape' } });
+
+ const { items: revisions } = await catalogDB.listIngredientRevisions(ing.id);
+ const oldRev = revisions.find((rev) => rev.payload?.description === 'old-shape');
+ expect(oldRev).toBeTruthy();
+ expect(oldRev.payload.schemaVersion).toBe(0);
+
+ const r = await request(makeApp())
+ .post(`/api/catalog/ingredients/${ing.id}/revisions/${oldRev.id}/restore`)
+ .send({});
+
+ expect(r.status).toBe(200);
+ expect(r.body.payload.description).toBe('old-shape');
+ expect(r.body.payload.schemaVersion).toBe(0); // preserved verbatim, NOT re-stamped
+
+ // The restore is itself recorded as a new revision (auditable/reversible).
+ const { items: after } = await catalogDB.listIngredientRevisions(ing.id);
+ expect(after.length).toBe(revisions.length + 1);
+ });
+
+ it('404s when the revision belongs to a different ingredient', async () => {
+ const a = await catalogDB.createIngredient({ type: 'concept', name: 'Restore Owner A', payload: { description: 'a' } });
+ const b = await catalogDB.createIngredient({ type: 'concept', name: 'Restore Owner B', payload: { description: 'b' } });
+ createdIngredientIds.add(a.id);
+ createdIngredientIds.add(b.id);
+ await catalogDB.updateIngredient(a.id, { payload: { description: 'a2' } });
+ const aRev = (await catalogDB.listIngredientRevisions(a.id)).items[0];
+
+ const r = await request(makeApp())
+ .post(`/api/catalog/ingredients/${b.id}/revisions/${aRev.id}/restore`)
+ .send({});
+ expect(r.status).toBe(404);
+ });
+});
diff --git a/server/services/catalogDB.js b/server/services/catalogDB.js
index 45b2085d5d..8560fcd092 100644
--- a/server/services/catalogDB.js
+++ b/server/services/catalogDB.js
@@ -169,11 +169,17 @@ function rowToRevision(row) {
}
-export async function createScrap({ title, rawText, sourceKind = 'paste', metadata = {}, embedding = null, embeddingModel = null } = {}) {
+// `{ client }` is optional — when supplied, the INSERT runs on the caller's
+// transaction client so a bulk-import batch can commit-or-rollback its scraps
+// alongside the ingredients + source links (see POST /api/catalog/bulk-import).
+// Absent, falls through to the pool-level `query` as before. Mirrors the same
+// option on `createIngredient` / `linkIngredientToSource`.
+export async function createScrap({ title, rawText, sourceKind = 'paste', metadata = {}, embedding = null, embeddingModel = null } = {}, { client } = {}) {
if (!rawText) throw new Error('rawText is required');
const id = newScrapId();
const originInstanceId = await getInstanceId();
- const result = await query(
+ const exec = client ? client.query.bind(client) : query;
+ const result = await exec(
`INSERT INTO catalog_scraps
(id, title, raw_text, source_kind, metadata, embedding, embedding_model, origin_instance_id)
VALUES ($1, $2, $3, $4, $5::jsonb, $6, $7, $8)