Skip to content

armgit / kiln

Public Imported from GitHub
0

Build and revise procedural 3D assets with your coding agent. Local MCP server, CLI and TypeScript engine with rendered review, editable source and GLB export.

5.5 KB
Raw Blame
import { describe, expect, it } from 'bun:test';
import { mkdtemp, mkdir, rm, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import { tmpdir } from 'node:os';
import { FileProgramStore } from '../program-store-node';
import { ExperimentalProgramAliases, isLockContention } from '../experiments/program-aliases';

it('requires explicit CAS, preserves immutable source, and rejects collisions and corrupt aliases', async () => {
  const directory = await mkdtemp(join(tmpdir(), 'kiln-alias-test-'));
  try {
    const store = new FileProgramStore(join(directory, 'programs'));
    const aliases = new ExperimentalProgramAliases(join(directory, 'aliases'), store);
    const a = await store.put('original');
    const b = await store.put('revised');
    expect(await aliases.resolve('bridge')).toBeNull();
    await aliases.compareAndSet('bridge', null, a);
    await expect(aliases.compareAndSet('bridge', null, b)).rejects.toThrow('conflict');
    await aliases.compareAndSet('bridge', a, b);
    await expect(aliases.compareAndSet('bridge', a, a)).rejects.toThrow('conflict');
    expect(await aliases.resolve('bridge')).toBe(b);
    expect(await store.get(a)).toBe('original');
    await expect(aliases.compareAndSet('../escape', null, a)).rejects.toThrow('alias');
    await mkdir(join(directory, 'aliases', 'bridge.json.lock'));
    await expect(aliases.compareAndSet('bridge', b, a)).rejects.toThrow('busy');
    expect(await aliases.resolve('bridge')).toBe(b);
    await rm(join(directory, 'aliases', 'bridge.json.lock'), { recursive: true });
    await writeFile(join(directory, 'aliases', 'bridge.json'), '{broken');
    await expect(aliases.resolve('bridge')).rejects.toThrow('corrupt');
    await expect(aliases.compareAndSet('bridge', null, a)).rejects.toThrow('corrupt');
    await expect(
      aliases.compareAndSet('missing', null, `sha256:${'0'.repeat(64)}`),
    ).rejects.toThrow('not found');
  } finally {
    await rm(directory, { recursive: true, force: true });
  }
});

// The Windows red on 2026-09-12 failed in 123ms, which is what rules out every timing
// explanation: both hang guards are seconds away. A worker died, fast, and the only way
// `compareAndSet` does that is the `throw error` beside the lock -- reached whenever
// `mkdir` reports contention with a code other than `EEXIST`.
//
// Windows has exactly that case. Directory deletion is not synchronous there, so a
// directory whose last handle has not closed sits in pending-delete, and `mkdir` on that
// name answers `EPERM` or `EACCES`. Every release runs `rmdir` in a `finally`, so with
// eight processes contending, one landing in that window is ordinary contention.
//
// Asserted on the predicate rather than through a real Windows filesystem, because the
// decision is what was wrong and a Linux host can check every branch of it. The platform
// is a parameter for the same reason.
describe('lock contention is recognised per platform', () => {
  const err = (code: string) => Object.assign(new Error(code), { code });

  it('treats EEXIST as contention everywhere', () => {
    expect(isLockContention(err('EEXIST'), 'linux')).toBe(true);
    expect(isLockContention(err('EEXIST'), 'win32')).toBe(true);
    expect(isLockContention(err('EEXIST'), 'darwin')).toBe(true);
  });

  // The regression itself: these are what a pending-delete directory reports.
  it('treats EPERM and EACCES as contention on Windows', () => {
    expect(isLockContention(err('EPERM'), 'win32')).toBe(true);
    expect(isLockContention(err('EACCES'), 'win32')).toBe(true);
  });

  // And does not swallow them elsewhere, where they mean the parent is not writable.
  // Widening this to every platform would turn a real permission fault into "busy".
  it('does not treat EPERM or EACCES as contention on POSIX', () => {
    for (const platform of ['linux', 'darwin']) {
      expect(isLockContention(err('EPERM'), platform)).toBe(false);
      expect(isLockContention(err('EACCES'), platform)).toBe(false);
    }
  });

  it('never swallows an unrelated failure', () => {
    for (const code of ['ENOSPC', 'EROFS', 'ENAMETOOLONG', 'EMFILE']) {
      expect(isLockContention(err(code), 'win32')).toBe(false);
      expect(isLockContention(err(code), 'linux')).toBe(false);
    }
    expect(isLockContention(new Error('no code at all'), 'win32')).toBe(false);
  });
});

it('rejects lost updates from eight independent processes', async () => {
  const child = Bun.spawn(
    [
      process.execPath,
      join(import.meta.dir, '../../scripts/experiment-program-aliases.ts'),
      '--check',
    ],
    { stdout: 'pipe', stderr: 'pipe' },
  );
  const timer = setTimeout(() => child.kill(), 15000);
  try {
    const [code, output, error] = await Promise.all([
      child.exited,
      new Response(child.stdout).text(),
      new Response(child.stderr).text(),
    ]);
    expect(error).toBe('');
    expect(code).toBe(0);
    const receipt = JSON.parse(output);
    expect(receipt).toMatchObject({
      processes: 8,
      updated: 1,
      rejected: 7,
      immutableReferencesResolved: 3,
      originalSourcePreserved: true,
    });
    // Presence, not a bound. This is a wall-clock figure on whatever host is running, so
    // asserting a ceiling here would be the flake this field exists to diagnose. What
    // matters is that a passing receipt records its own headroom against the 10s guard --
    // ~60ms on an idle Linux host, and the number that would have shown how close the
    // 2026-09-12 Windows run came before it went red.
    expect(typeof receipt.slowestWorkerMs).toBe('number');
  } finally {
    clearTimeout(timer);
  }
}, 20000);