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

6.8 KB
Raw Blame
/**
 * Unit tests for the catalog DB layer's registry-driven behavior:
 *   - `createIngredient` stamps `payload.schemaVersion` from the registry;
 *   - it overwrites an incoming (stale/peer) schemaVersion with the LOCAL one;
 *   - the minted id uses the registry's per-type prefix;
 *   - an unknown type throws before touching the DB.
 *
 * Postgres is mocked — we capture the INSERT params and assert on them, so the
 * suite runs without a live database.
 */

import { describe, it, expect, vi, beforeEach } from 'vitest';

const captured = { sql: null, params: null };

vi.mock('../lib/db.js', () => ({
  query: vi.fn(async (sql, params) => {
    // createIngredient/reviveDeletedIngredient now also write + prune a
    // revision row, so the fake must dispatch on the statement: only capture
    // the catalog_ingredients write the assertions read, and no-op the
    // revision INSERT / prune DELETE.
    const s = sql.trim();
    if (s.includes('catalog_identity_operations')) return { rows: [] };
    if (/^INSERT INTO catalog_ingredient_revisions/i.test(s)) {
      const [id, ingredient_id, name] = params;
      return {
        rows: [{
          id, ingredient_id, name, payload: JSON.parse(params[3]),
          tags: params[4] || [], source: params[5], actor: params[6] ?? null,
          created_at: new Date(),
        }],
      };
    }
    if (/^DELETE FROM catalog_ingredient_revisions/i.test(s)) {
      return { rows: [] };
    }
    captured.sql = sql;
    captured.params = params;
    // Echo a row shaped like catalog_ingredients so rowToIngredient works.
    const [id, type, name] = params;
    const payload = JSON.parse(params[3]);
    return {
      rows: [{
        id, type, name, payload, tags: params[4] || [],
        embedding: null, embedding_model: null, origin_instance_id: params[7],
        created_at: new Date(), updated_at: new Date(),
        deleted: false, deleted_at: null, sync_sequence: 1,
      }],
    };
  }),
  withTransaction: vi.fn(async (fn) => {
    const { query } = await import('../lib/db.js');
    return fn({ query });
  }),
  pgvectorToArray: vi.fn(() => null),
  arrayToPgvector: vi.fn((a) => a),
}));

vi.mock('./instanceIdentity.js', () => ({
  getInstanceId: vi.fn(async () => 'inst-test'),
}));

import { createIngredient, reviveDeletedIngredient } from './catalogDB.js';
import { currentPayloadSchemaVersion, setUserCatalogTypes } from '../lib/catalogTypes.js';

beforeEach(() => {
  captured.sql = null;
  captured.params = null;
  // Reset the user-type registry between tests so a leftover definition can't
  // leak across cases.
  setUserCatalogTypes([]);
});

describe('createIngredient — payload schemaVersion stamping', () => {
  it('stamps the registry-current payload schemaVersion', async () => {
    const ing = await createIngredient({ type: 'character', name: 'Echo', payload: { physicalDescription: 'tall' } });
    const stored = JSON.parse(captured.params[3]);
    expect(stored.schemaVersion).toBe(currentPayloadSchemaVersion('character'));
    expect(stored.physicalDescription).toBe('tall');
    // The returned record carries the stamped payload too.
    expect(ing.payload.schemaVersion).toBe(currentPayloadSchemaVersion('character'));
  });

  it('overwrites an incoming (stale) schemaVersion with the local one', async () => {
    await createIngredient({ type: 'idea', name: 'Spark', payload: { summary: 'x', schemaVersion: 99 } });
    const stored = JSON.parse(captured.params[3]);
    expect(stored.schemaVersion).toBe(currentPayloadSchemaVersion('idea'));
    expect(stored.schemaVersion).not.toBe(99);
  });

  it('stamps even when no payload is supplied', async () => {
    await createIngredient({ type: 'scene', name: 'Open' });
    const stored = JSON.parse(captured.params[3]);
    expect(stored.schemaVersion).toBe(currentPayloadSchemaVersion('scene'));
  });
});

describe('createIngredient — registry-derived id prefix + type guard', () => {
  it('mints an id with the registry per-type prefix', async () => {
    const ing = await createIngredient({ type: 'place', name: 'Dock' });
    expect(ing.id).toMatch(/^cat-plc-/);
  });

  it('preserves an explicit id (peer backfill) without minting', async () => {
    const ing = await createIngredient({ id: 'cat-chr-bible-abc', type: 'character', name: 'Echo' });
    expect(ing.id).toBe('cat-chr-bible-abc');
  });

  it('throws on an unknown type before touching the DB', async () => {
    await expect(createIngredient({ type: 'wardrobe', name: 'Cape' })).rejects.toThrow(/Invalid ingredient type/);
    expect(captured.params).toBeNull();
  });
});

describe('createIngredient — user-defined types', () => {
  it('mints a cat-<prefix>-<uuid> id and round-trips a user-typed create', async () => {
    setUserCatalogTypes([{ id: 'faction', label: 'Faction', primaryContentKey: 'creed', fields: [{ key: 'creed', label: 'Creed', kind: 'longtext' }] }]);
    const ing = await createIngredient({ type: 'faction', name: 'The Choir', payload: { creed: 'unity' } });
    expect(ing.id).toMatch(/^cat-[a-z0-9]+-[0-9a-f-]{36}$/);
    expect(ing.type).toBe('faction');
    const stored = JSON.parse(captured.params[3]);
    expect(stored.creed).toBe('unity');
    // User types are payloadSchemaVersion 1.
    expect(stored.schemaVersion).toBe(1);
  });

  it('throws for an unregistered user type before touching the DB', async () => {
    // No setUserCatalogTypes call → 'faction' is not active.
    await expect(createIngredient({ type: 'faction', name: 'x' })).rejects.toThrow(/Invalid ingredient type/);
    expect(captured.params).toBeNull();
  });

  it('never mints a user prefix that collides with a system prefix', async () => {
    // id 'characters' would slug to 'char' which is NOT a system prefix ('chr'),
    // but 'cha' could; assert the minted prefix avoids all six system prefixes.
    setUserCatalogTypes([{ id: 'chronicle', label: 'Chronicle', primaryContentKey: 'body', fields: [{ key: 'body', label: 'Body', kind: 'longtext' }] }]);
    const ing = await createIngredient({ type: 'chronicle', name: 'Log' });
    const prefix = ing.id.split('-')[1];
    expect(['chr', 'plc', 'obj', 'idea', 'scn', 'cnc']).not.toContain(prefix);
  });
});

describe('reviveDeletedIngredient — registry guard + payload stamping', () => {
  it('validates the type against the registry (not a stale prefix map)', async () => {
    await expect(reviveDeletedIngredient('cat-x-1', { type: 'wardrobe', name: 'Cape' }))
      .rejects.toThrow(/invalid type/);
    expect(captured.params).toBeNull();
  });

  it('re-stamps the payload schemaVersion on revive', async () => {
    await reviveDeletedIngredient('cat-chr-1', { type: 'character', name: 'Echo', payload: { role: 'lead' } });
    const stored = JSON.parse(captured.params[3]);
    expect(stored.schemaVersion).toBe(currentPayloadSchemaVersion('character'));
    expect(stored.role).toBe('lead');
  });
});