import { describe, expect, it } from 'vitest';
import type { FeatureNode, FeatureSpec, LedgerRow } from '../src/core/types';
import { assembleContext } from '../src/tier1/assembleContext';

const feature: FeatureNode = {
  id: 'F1',
  name: 'Codec',
  description: 'Encodes and decodes text.',
  parentId: null,
  deps: [],
  files: ['src/codec.ts'],
  testFile: 'tests/codec.test.ts',
  edgeCases: ['empty input'],
  exports: [{ file: 'src/codec.ts', symbol: 'codec' }],
  envVars: [],
  stubs: [{
    path: 'src/codec.ts',
    content: 'export function codec(input: string): string { throw new Error("stub"); }\n',
  }],
};

const spec: FeatureSpec = {
  title: 'Codec package',
  prompt: 'Create a deterministic text codec.',
  features: [feature],
};

const rows: LedgerRow[] = [{
  step: 1,
  id: 'spec-F1',
  type: 'spec',
  featureId: 'F1',
  parentId: null,
  causeId: null,
  edgeFrom: null,
  edgeTo: null,
  artifactPath: null,
  bugClass: null,
  status: 'info',
  summary: 'encoded F1',
  ts: '2026-07-28T00:00:00.000Z',
}];

describe('SET 96a — context bundle', () => {
  it('is byte-identical for identical deterministic inputs', () => {
    const input = {
      stage: 'ir' as const,
      featureId: feature.id,
      spec,
      feature,
      parts: null,
      stubs: feature.stubs,
      lessons: [],
      siblingParts: new Map(),
      target: 'codec handles empty input',
      ledgerRows: rows,
      artifact: feature.stubs,
      cachedReading: {
        idioms: ['uses direct roundtrips'],
        tropes: [],
        schemes: ['small contracts hold'],
        union: 'The codec contract remains unimplemented.',
      },
    };

    const first = assembleContext(input);
    const second = assembleContext(input);

    expect(second.superbufferText).toBe(first.superbufferText);
    expect(second.digestPreamble).toBe(first.digestPreamble);
  });
});
