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

7.2 KB
Raw Blame
import { describe, it as vitestIt, expect, vi, beforeEach, afterEach, afterAll } from 'vitest';
import { mkdtempSync, rmSync } from 'fs';
import { tmpdir } from 'os';
import { join } from 'path';
import { mockNoPeerSync, mockNoPeers, makePathsProxy, ownTestBodies } from '../lib/mockPathsDataRoot.js';

// Each test gets its own root, allocated only when the test starts — never at
// module collection, so a run that bails before this file executes a test
// leaves nothing behind. Teardown drains every owned test body first: a body
// cancelled by another worker's fail-fast keeps writing, and removing the root
// under it lets the next store write recreate it (#9635).
let tempRoot;
const roots = new Set();
const owned = ownTestBodies(vitestIt);
const it = owned.it;

async function cleanupFixtures() {
  try {
    await owned.drain();
  } finally {
    for (const root of roots) rmSync(root, { recursive: true, force: true });
    roots.clear();
  }
}

vi.mock('../lib/fileUtils.js', async (importOriginal) =>
  makePathsProxy(await importOriginal(), { dataRoot: () => tempRoot }));
vi.mock('./instances.js', () => mockNoPeers());
vi.mock('./sharing/peerSync.js', () => mockNoPeerSync());

let uuidCounter = 0;
vi.mock('crypto', async () => {
  const actual = await vi.importActual('crypto');
  return { ...actual, randomUUID: () => `uuid-${String(++uuidCounter).padStart(8, '0')}` };
});

const universeSvc = await import('./universeBuilder.js');
const seriesSvc = await import('./pipeline/series.js');
const mediaCollections = await import('./mediaCollections.js');
const dup = await import('./duplicateDetection.js');

afterEach(cleanupFixtures);
afterAll(cleanupFixtures);

describe('duplicateDetection', () => {
  beforeEach(() => {
    tempRoot = mkdtempSync(join(tmpdir(), 'dup-detect-test-'));
    roots.add(tempRoot);
    uuidCounter = 0;
  });

  describe('findDuplicateUniverseGroups', () => {
    it('groups universes by normalized (case/whitespace-insensitive) name', async () => {
      await universeSvc.createUniverse({ name: 'Clandestiny' });
      await universeSvc.createUniverse({ name: '  clandestiny ' });
      await universeSvc.createUniverse({ name: 'Echoes of the Choir' });

      const groups = await dup.findDuplicateUniverseGroups();
      expect(groups).toHaveLength(1);
      expect(groups[0].normalizedName).toBe('clandestiny');
      expect(groups[0].records).toHaveLength(2);
      // Each record carries counts for the survivor-suggestion UI.
      expect(groups[0].records[0].counts).toHaveProperty('characters');
    });

    it('excludes deleted and ephemeral universes', async () => {
      const a = await universeSvc.createUniverse({ name: 'Dup' });
      await universeSvc.createUniverse({ name: 'Dup' });
      const eph = await universeSvc.createUniverse({ name: 'Dup', ephemeral: true });
      // Three "Dup" exist, but deleting one and ephemeral-marking another leaves
      // only a single live non-ephemeral → no duplicate group.
      await universeSvc.deleteUniverse(a.id);
      expect(eph.ephemeral).toBe(true);
      const groups = await dup.findDuplicateUniverseGroups();
      expect(groups).toEqual([]);
    });

    it('reports linkedSeriesCount per universe', async () => {
      const u1 = await universeSvc.createUniverse({ name: 'WithSeries' });
      await universeSvc.createUniverse({ name: 'WithSeries' });
      await seriesSvc.createSeries({ name: 'A', universeId: u1.id });
      await seriesSvc.createSeries({ name: 'B', universeId: u1.id });
      const groups = await dup.findDuplicateUniverseGroups();
      const rec = groups[0].records.find((r) => r.id === u1.id);
      expect(rec.linkedSeriesCount).toBe(2);
    });

    it('sums linkedCollectionItemCount across all collections sharing a universeId', async () => {
      // Two universes share a name → a duplicate group. The first owns TWO
      // collections (the deterministic `uc-` bucket plus a stray same-universeId
      // record that a sync merge could leave behind). The count must add both
      // collections' items, not last-wins on whichever the scan reads last.
      const u1 = await universeSvc.createUniverse({ name: 'WithCollections' });
      await universeSvc.createUniverse({ name: 'WithCollections' });
      const now = new Date().toISOString();
      const seed = (id, itemCount) => mediaCollections.mediaCollectionStore().saveOneNow(id, {
        id,
        name: `${id}-name`,
        description: '',
        coverKey: null,
        universeId: u1.id,
        items: Array.from({ length: itemCount }, (_, i) => ({
          kind: 'image', ref: `${id}-item-${i}`, key: `${id}-item-${i}`,
        })),
        createdAt: now,
        updatedAt: now,
      });
      await seed(`uc-${u1.id}`, 3);
      await seed('stray-merged-bucket', 2);

      const groups = await dup.findDuplicateUniverseGroups();
      const rec = groups[0].records.find((r) => r.id === u1.id);
      expect(rec.linkedCollectionItemCount).toBe(5);
    });
  });

  describe('findDuplicateSeriesGroups', () => {
    it('groups same-name series WITHIN a universe, not across universes', async () => {
      const u1 = await universeSvc.createUniverse({ name: 'U1' });
      const u2 = await universeSvc.createUniverse({ name: 'U2' });
      await seriesSvc.createSeries({ name: 'Salt Run', universeId: u1.id });
      await seriesSvc.createSeries({ name: 'Salt Run', universeId: u1.id }); // dup within u1
      await seriesSvc.createSeries({ name: 'Salt Run', universeId: u2.id }); // NOT a dup (diff universe)

      const { series } = await dup.findDuplicateSeriesGroups();
      expect(series).toHaveLength(1);
      expect(series[0].universeId).toBe(u1.id);
      expect(series[0].records).toHaveLength(2);
    });

    it('surfaces orphan series in a separate marked bucket (never merged across null)', async () => {
      // Orphans can only be created via the service (route forbids it).
      await seriesSvc.createSeries({ name: 'Lonely', universeId: null });
      await seriesSvc.createSeries({ name: 'Lonely', universeId: null });
      const { series, orphans, orphanCount } = await dup.findDuplicateSeriesGroups();
      expect(series).toEqual([]);
      expect(orphanCount).toBe(2);
      expect(orphans).toHaveLength(1);
      expect(orphans[0].normalizedName).toBe('lonely');
    });
  });

  describe('same-name create warning helpers', () => {
    it('findSameNameUniverses excludes the record itself', async () => {
      const a = await universeSvc.createUniverse({ name: 'Solo' });
      expect(await dup.findSameNameUniverses('Solo', { excludeId: a.id })).toEqual([]);
      await universeSvc.createUniverse({ name: 'Solo' });
      const hits = await dup.findSameNameUniverses('Solo', { excludeId: a.id });
      expect(hits).toHaveLength(1);
    });

    it('findSameNameSeries scopes within a universe and ignores orphans', async () => {
      const u1 = await universeSvc.createUniverse({ name: 'U' });
      const s1 = await seriesSvc.createSeries({ name: 'Twin', universeId: u1.id });
      await seriesSvc.createSeries({ name: 'Twin', universeId: u1.id });
      const hits = await dup.findSameNameSeries('Twin', u1.id, { excludeId: s1.id });
      expect(hits).toHaveLength(1);
      // No universe → never warns (orphans aren't duplicate-checked).
      expect(await dup.findSameNameSeries('Twin', null, {})).toEqual([]);
    });
  });
});