import { describe, expect, it } from 'vitest'
import { readFileSync } from 'node:fs'
import { join } from 'node:path'
import * as contracts from 'relay-contracts'

const pkg = JSON.parse(
  readFileSync(join(import.meta.dirname, '..', 'package.json'), 'utf8'),
) as { dependencies?: Record<string, string>; devDependencies?: Record<string, string> }

describe('package scaffold', () => {
  it('resolves the frozen relay-contracts surface', () => {
    // §4.1 codec + §4.4 shapes + §4.5 pairing must all be importable.
    expect(typeof contracts.encodeMuxFrame).toBe('function')
    expect(typeof contracts.decodeMuxFrame).toBe('function')
    expect(typeof contracts.decodeGoAwayReason).toBe('function')
    expect(contracts.EnrollResultSchema).toBeDefined()
  })

  it('declares web-terminal-agent bin (INDEX §4.5 distribution)', () => {
    expect(pkg.dependencies?.['relay-contracts']).toBe('file:../relay-contracts')
  })

  it('INV11 tripwire: no ANSI/xterm/vt100 dependency', () => {
    const allDeps = { ...(pkg.dependencies ?? {}), ...(pkg.devDependencies ?? {}) }
    const forbidden = /xterm|ansi|vt100/i
    for (const name of Object.keys(allDeps)) {
      expect(forbidden.test(name), `forbidden terminal-parser dep: ${name}`).toBe(false)
    }
  })
})
