import { describe, test } from 'node:test';
import assert from 'node:assert/strict';
import { validateDslText } from '@arbiter/evidence-dsl/validation/DSLValidation';

describe('DSL type guards', () => {
  test('infix is guard narrows for attribute access', () => {
    const dsl = `
      definition Profile {
        email_verified: boolean
      }

      definition Session {
        provider_profile: any
      }

      evidence can_login(session: Session) {
        session.provider_profile is Profile && session.provider_profile.email_verified
      }
    `;

    const result = validateDslText(dsl);
    assert.equal(result.success, true);
    assert.equal(result.errors.length, 0);
  });

  test('infix is guard rejects unknown type names', () => {
    const dsl = `
      definition Session {
        provider_profile: any
      }

      evidence can_login(session: Session) {
        session.provider_profile is MissingType
      }
    `;

    const result = validateDslText(dsl);
    assert.equal(result.success, false);
    assert.ok(result.errors.some(err => err.includes('Type guard requires a known type name')));
  });

  test('infix is guard rejects non-type expressions', () => {
    const dsl = `
      definition Session {
        provider_profile: any
      }

      evidence can_login(session: Session) {
        session.provider_profile is "Profile"
      }
    `;

    const result = validateDslText(dsl);
    assert.equal(result.success, false);
    assert.ok(result.errors.some(err => err.includes('Type guard requires a known type name')));
  });

  test('infix is guard exposes readable error text', () => {
    const dsl = `
      definition Session {
        provider_profile: any
      }

      evidence can_login(session: Session) {
        session.provider_profile is TypoedProfile
      }
    `;

    const result = validateDslText(dsl);
    assert.equal(result.success, false);
    assert.ok(result.errors.some(err => err.includes('Type guard requires a known type name')));
  });
});
