Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

5.1 KB
Raw Blame
import { mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { dirname, join, sep } from 'node:path';
import { fileURLToPath } from 'node:url';
import { afterEach, describe, expect, it } from 'vitest';
import { isTestOnlySourceFile } from '../../../test-support/test-only-source-file.mjs';
import {
  PAIRED_INTAKE_DETECTION,
  pairedIntakeDetectionMode,
  RESERVED_PAIRED_INTAKE_DETECTION,
  shouldRunPairedIntakeDetection,
} from './bridge-loss-suppression.ts';

const SRC_DIR = dirname(fileURLToPath(import.meta.url));

function isProductionSource(name: string): boolean {
  return name.endsWith('.ts') && !isTestOnlySourceFile(name) && !name.endsWith('.d.ts');
}

function stripCommentLines(text: string): string {
  return text
    .split('\n')
    .filter((line) => {
      const t = line.trimStart();
      return !(t.startsWith('*') || t.startsWith('//') || t.startsWith('/*') || t.startsWith('*/'));
    })
    .join('\n');
}

function declaredPairedOrigins(root: string = SRC_DIR): Set<string> {
  const origins = new Set<string>();
  const objLiteral = /\{[^{}]*\}/g;
  for (const rel of readdirSync(root, { recursive: true, encoding: 'utf-8' })) {
    const base = rel.split(sep).pop() ?? rel;
    if (!isProductionSource(base)) continue;
    let text: string;
    try {
      text = stripCommentLines(readFileSync(join(root, rel), 'utf-8'));
    } catch {
      continue;
    }
    for (const m of text.matchAll(objLiteral)) {
      const obj = m[0];
      if (!obj.includes('paired: true')) continue;
      const originMatch = obj.match(/origin:\s*'([^']+)'/);
      if (originMatch?.[1]) origins.add(originMatch[1]);
    }
  }
  return origins;
}

const scratchDirs: string[] = [];
afterEach(() => {
  while (scratchDirs.length > 0) {
    const dir = scratchDirs.pop();
    if (dir) rmSync(dir, { recursive: true, force: true });
  }
});

function plantSyntheticTree(): string {
  const root = mkdtempSync(join(tmpdir(), 'ok-paired-scan-'));
  scratchDirs.push(root);
  writeFileSync(
    join(root, 'top-level-surface.ts'),
    "export const TOP = Object.freeze({ source: 'local', context: { origin: 'planted-top-level', paired: true } });\n",
    'utf-8',
  );
  mkdirSync(join(root, 'http', 'deeper'), { recursive: true });
  writeFileSync(
    join(root, 'http', 'nested-surface.ts'),
    "export const NESTED = Object.freeze({ source: 'local', context: { origin: 'planted-subdirectory', paired: true } });\n",
    'utf-8',
  );
  writeFileSync(
    join(root, 'http', 'deeper', 'deepest-surface.ts'),
    "export const DEEP = Object.freeze({ source: 'local', context: { origin: 'planted-two-deep', paired: true } });\n",
    'utf-8',
  );
  writeFileSync(
    join(root, 'http', 'nested-surface.test.ts'),
    "const FIXTURE = { origin: 'planted-fixture-not-production', paired: true };\n",
    'utf-8',
  );
  return root;
}

describe('paired-intake detection classification (fail-closed sweep)', () => {
  it('the scanner finds paired origins declared in subdirectories, not just top-level files', () => {
    const root = plantSyntheticTree();
    const found = declaredPairedOrigins(root);

    expect(found.has('planted-top-level')).toBe(true);
    expect(found.has('planted-subdirectory')).toBe(true);
    expect(found.has('planted-two-deep')).toBe(true);
    expect(found.has('planted-fixture-not-production')).toBe(false);
    expect([...found].sort()).toEqual([
      'planted-subdirectory',
      'planted-top-level',
      'planted-two-deep',
    ]);
  });

  it('classifies every paired-write origin declared in production source', () => {
    const declared = declaredPairedOrigins();
    expect(declared.size).toBeGreaterThanOrEqual(5);
    const unclassified = [...declared].filter((o) => pairedIntakeDetectionMode(o) === undefined);
    expect(unclassified).toEqual([]);
  });

  it('has no phantom classification without a source origin', () => {
    const declared = declaredPairedOrigins();
    const phantom = Object.keys(PAIRED_INTAKE_DETECTION).filter((o) => !declared.has(o));
    expect(phantom).toEqual([]);
  });

  it('keeps reserved classifications out of the live map until their constant lands', () => {
    const declared = declaredPairedOrigins();
    for (const reserved of Object.keys(RESERVED_PAIRED_INTAKE_DETECTION)) {
      expect(PAIRED_INTAKE_DETECTION[reserved]).toBeUndefined();
      expect(declared.has(reserved)).toBe(false);
    }
  });

  it('flags a synthetic unclassified origin', () => {
    expect(pairedIntakeDetectionMode('brand-new-write-surface')).toBeUndefined();
    expect(shouldRunPairedIntakeDetection('brand-new-write-surface')).toBe(false);
  });

  it('runs the detector for content-preserving origins and suppresses replacements', () => {
    expect(shouldRunPairedIntakeDetection('agent-write')).toBe(true);
    expect(shouldRunPairedIntakeDetection('agent-undo')).toBe(true);
    expect(shouldRunPairedIntakeDetection('file-watcher')).toBe(true);
    expect(shouldRunPairedIntakeDetection('rollback-apply')).toBe(false);
    expect(shouldRunPairedIntakeDetection('managed-rename')).toBe(false);
    expect(shouldRunPairedIntakeDetection('park-snapshot')).toBe(false);
  });
});