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

5.3 KB
Raw Blame
import { describe, it, expect, vi, beforeEach } from 'vitest';

vi.mock('./apiCore.js', () => ({
  request: vi.fn(),
}));

let request;
let listCatalogIngredientsByIds;

beforeEach(async () => {
  vi.resetModules();
  ({ request } = await import('./apiCore.js'));
  ({ listCatalogIngredientsByIds } = await import('./apiCatalog.js'));
  request.mockReset();
});

describe('listCatalogIngredientsByIds', () => {
  it('unwraps the { items } envelope and re-orders to the requested ids', async () => {
    // Server returns created_at DESC (NOT request order) inside the paged envelope.
    request.mockResolvedValue({
      items: [
        { id: 'cat-b', name: 'B' },
        { id: 'cat-a', name: 'A' },
        { id: 'cat-c', name: 'C' },
      ],
      nextOffset: 3,
    });

    const out = await listCatalogIngredientsByIds(['cat-a', 'cat-b', 'cat-c']);
    // Restored to the user's pick order, as a plain array.
    expect(out.map((i) => i.id)).toEqual(['cat-a', 'cat-b', 'cat-c']);

    // The ids ride the normal list endpoint as a CSV query.
    const [path] = request.mock.calls[0];
    expect(path).toContain('/catalog/ingredients?');
    expect(decodeURIComponent(path)).toContain('ids=cat-a,cat-b,cat-c');
  });

  it('drops falsy ids and ids the server omits (missing / soft-deleted)', async () => {
    request.mockResolvedValue({ items: [{ id: 'cat-a', name: 'A' }], nextOffset: 1 });
    const out = await listCatalogIngredientsByIds(['cat-a', '', 'cat-missing', null]);
    expect(out.map((i) => i.id)).toEqual(['cat-a']);
  });

  it('tolerates a bare-array response shape', async () => {
    request.mockResolvedValue([{ id: 'cat-a', name: 'A' }]);
    const out = await listCatalogIngredientsByIds(['cat-a']);
    expect(out.map((i) => i.id)).toEqual(['cat-a']);
  });
});

describe('listCatalogIngredients filter passthrough (#1762)', () => {
  let listCatalogIngredients;
  beforeEach(async () => {
    ({ listCatalogIngredients } = await import('./apiCatalog.js'));
    request.mockResolvedValue({ items: [], nextOffset: 0 });
  });

  it('encodes a universe/series ref filter as refKind + refId', async () => {
    await listCatalogIngredients({ refKind: 'universe', refId: 'u-1', type: 'character' });
    const [path] = request.mock.calls[0];
    expect(decodeURIComponent(path)).toContain('refKind=universe');
    expect(decodeURIComponent(path)).toContain('refId=u-1');
    expect(decodeURIComponent(path)).toContain('type=character');
  });

  it('sends unlinked=true for the Raw album view', async () => {
    await listCatalogIngredients({ unlinked: true });
    const [path] = request.mock.calls[0];
    expect(path).toContain('unlinked=true');
  });

  it('sends orphaned=true for the Orphaned album view', async () => {
    await listCatalogIngredients({ orphaned: true });
    const [path] = request.mock.calls[0];
    expect(path).toContain('orphaned=true');
  });

  it('prefers the ref filter over unlinked/orphaned when both are passed', async () => {
    await listCatalogIngredients({ refKind: 'series', refId: 's-1', unlinked: true, orphaned: true });
    const [path] = request.mock.calls[0];
    expect(decodeURIComponent(path)).toContain('refKind=series');
    expect(path).not.toContain('unlinked');
    expect(path).not.toContain('orphaned');
  });

  it('sends scrapId for the source-scrap filter, composed with a ref filter (#7617)', async () => {
    await listCatalogIngredients({ scrapId: 'cat-scrap-1', refKind: 'universe', refId: 'u-1' });
    const [path] = request.mock.calls[0];
    expect(decodeURIComponent(path)).toContain('scrapId=cat-scrap-1');
    expect(decodeURIComponent(path)).toContain('refKind=universe');
  });
});

describe('getCatalogFacets (#1762)', () => {
  it('GETs the facets endpoint', async () => {
    const { getCatalogFacets } = await import('./apiCatalog.js');
    request.mockResolvedValue({ types: [], universes: [], series: [], tags: [], total: 0 });
    await getCatalogFacets();
    expect(request).toHaveBeenCalledWith('/catalog/facets', undefined);
  });
});
// @vitest-environment node

it('serializes the commit operation key into the HTTP body while retaining request options', async () => {
  const { commitCatalogScrapDraft } = await import('./apiCatalog.js');
  const operationKey = '11111111-1111-4111-8111-111111111111';
  await commitCatalogScrapDraft('example-scrap', [], { operationKey, relationships: [], silent: true });
  const [path, options] = request.mock.lastCall;
  expect(path).toBe('/catalog/scraps/example-scrap/commit');
  expect(JSON.parse(options.body)).toEqual({ accepted: [], operationKey, relationships: [] });
  expect(options.silent).toBe(true);
  expect(options).not.toHaveProperty('operationKey');
});

it('serializes the source note ids so the server can consume them in the commit request', async () => {
  const { commitCatalogScrapDraft } = await import('./apiCatalog.js');
  const creativeNoteIds = ['3f1c0c52-8d5e-4b8e-9d3c-111111111111'];
  await commitCatalogScrapDraft('example-scrap', [], { creativeNoteIds, silent: true });
  const [, options] = request.mock.lastCall;
  expect(JSON.parse(options.body)).toEqual({ accepted: [], creativeNoteIds });
  expect(options).not.toHaveProperty('creativeNoteIds');
  await commitCatalogScrapDraft('example-scrap', [], { creativeNoteIds: [], silent: true });
  expect(JSON.parse(request.mock.lastCall[1].body)).toEqual({ accepted: [] });
});