import { describe, expect, it } from 'vitest';
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';

const runner = readFileSync(resolve(__dirname, '../src/tier1/runner.ts'), 'utf8');

describe('SET 94a — watcher digest cache', () => {
  it('keys by stage, feature, and deterministic digest identity', () => {
    expect(runner).toContain('const digestCacheKey = (stage: string, featureId: string | null, digest: object)');
    expect(runner).toContain('return `${stage}|${featureId ?? \'_\'}|${h.toString(36)}|${json.length}`;');
    expect(runner).toContain('const cached = watch.cache.get(cacheKey);');
  });

  it('records cache dispositions without caching failed engine calls', () => {
    expect(runner).toContain('metrics.watcherCacheHits++');
    expect(runner).toContain('metrics.watcherCacheMisses++');
    expect(runner).toContain('watch.cache.set(cacheKey, fresh);');
    expect(runner.indexOf('watch.cache.set(cacheKey, fresh);'))
      .toBeGreaterThan(runner.indexOf('const fresh = parseWatchReport(await engine(watchPrompt(digest)));'));
  });
});
