File size: 6,593 Bytes
518343a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
 * compliance_pass.test.ts
 * Doctrine v6 R3 — Vertical Governance Receipts
 * 10 PASSING compliance cases: valid policy documents correctly accepted by schema and loader.
 *
 * Test framework: Jest / ts-jest
 * Run: npx jest tests/compliance/compliance_pass.test.ts
 */

import Ajv from "ajv";
import addFormats from "ajv-formats";
import * as fs from "fs";
import * as path from "path";
import * as yaml from "js-yaml";

// ── Helpers ──────────────────────────────────────────────────────────────────

const SCHEMA_PATH = path.resolve(__dirname, "../../a11oy-knowledge.schema.json");
const POLICIES_DIR = path.resolve(__dirname, "../../policies/vertical");

function loadSchema() {
  const raw = fs.readFileSync(SCHEMA_PATH, "utf8");
  return JSON.parse(raw);
}

function loadPolicy(filename: string): unknown {
  const filepath = path.join(POLICIES_DIR, filename);
  const raw = fs.readFileSync(filepath, "utf8");
  return yaml.load(raw);
}

function buildValidator() {
  const ajv = new Ajv({ allErrors: true, strict: false });
  addFormats(ajv);
  const schema = loadSchema();
  return ajv.compile(schema);
}

// ── Test Suite ────────────────────────────────────────────────────────────────

describe("Compliance PASS — valid vertical policies accepted", () => {
  const validate = buildValidator();

  // PASS-001: Healthcare HIPAA policy validates against schema
  test("PASS-001: healthcare-hipaa.yaml validates against a11oy schema", () => {
    const policy = loadPolicy("healthcare-hipaa.yaml");
    const valid = validate(policy);
    if (!valid) console.error("Validation errors:", validate.errors);
    expect(valid).toBe(true);
  });

  // PASS-002: Financial SOX policy validates against schema
  test("PASS-002: financial-sox.yaml validates against a11oy schema", () => {
    const policy = loadPolicy("financial-sox.yaml");
    const valid = validate(policy);
    if (!valid) console.error("Validation errors:", validate.errors);
    expect(valid).toBe(true);
  });

  // PASS-003: Defense CMMC-L3 policy validates against schema
  test("PASS-003: defense-cmmc-l3.yaml validates against a11oy schema", () => {
    const policy = loadPolicy("defense-cmmc-l3.yaml");
    const valid = validate(policy);
    if (!valid) console.error("Validation errors:", validate.errors);
    expect(valid).toBe(true);
  });

  // PASS-004: Aviation DO-178C policy validates against schema
  test("PASS-004: aviation-do178c.yaml validates against a11oy schema", () => {
    const policy = loadPolicy("aviation-do178c.yaml");
    const valid = validate(policy);
    if (!valid) console.error("Validation errors:", validate.errors);
    expect(valid).toBe(true);
  });

  // PASS-005: Automotive ISO-26262 policy validates against schema
  test("PASS-005: automotive-iso26262.yaml validates against a11oy schema", () => {
    const policy = loadPolicy("automotive-iso26262.yaml");
    const valid = validate(policy);
    if (!valid) console.error("Validation errors:", validate.errors);
    expect(valid).toBe(true);
  });

  // PASS-006: Pharmaceutical 21CFR11 policy validates against schema
  test("PASS-006: pharma-21cfr11.yaml validates against a11oy schema", () => {
    const policy = loadPolicy("pharma-21cfr11.yaml");
    const valid = validate(policy);
    if (!valid) console.error("Validation errors:", validate.errors);
    expect(valid).toBe(true);
  });

  // PASS-007: Energy NERC-CIP policy validates against schema
  test("PASS-007: energy-nerc-cip.yaml validates against a11oy schema", () => {
    const policy = loadPolicy("energy-nerc-cip.yaml");
    const valid = validate(policy);
    if (!valid) console.error("Validation errors:", validate.errors);
    expect(valid).toBe(true);
  });

  // PASS-008: Maritime IMO-ISPS policy validates against schema
  test("PASS-008: maritime-imo-isps.yaml validates against a11oy schema", () => {
    const policy = loadPolicy("maritime-imo-isps.yaml");
    const valid = validate(policy);
    if (!valid) console.error("Validation errors:", validate.errors);
    expect(valid).toBe(true);
  });

  // PASS-009: LegalTech GDPR policy validates against schema
  test("PASS-009: legaltech-gdpr.yaml validates against a11oy schema", () => {
    const policy = loadPolicy("legaltech-gdpr.yaml");
    const valid = validate(policy);
    if (!valid) console.error("Validation errors:", validate.errors);
    expect(valid).toBe(true);
  });

  // PASS-010: Academic research ethics policy validates against schema
  test("PASS-010: academic-research-ethics.yaml validates against a11oy schema", () => {
    const policy = loadPolicy("academic-research-ethics.yaml");
    const valid = validate(policy);
    if (!valid) console.error("Validation errors:", validate.errors);
    expect(valid).toBe(true);
  });
});

// ── Structural Invariants ─────────────────────────────────────────────────────

describe("Compliance PASS — structural invariants across all policies", () => {
  const validate = buildValidator();
  const policyFiles = [
    "healthcare-hipaa.yaml",
    "financial-sox.yaml",
    "defense-cmmc-l3.yaml",
    "aviation-do178c.yaml",
    "automotive-iso26262.yaml",
    "pharma-21cfr11.yaml",
    "energy-nerc-cip.yaml",
    "maritime-imo-isps.yaml",
    "legaltech-gdpr.yaml",
    "academic-research-ethics.yaml",
  ];

  for (const file of policyFiles) {
    test(`INV: ${file} — regulatory_clauses count ∈ [8, 12]`, () => {
      const policy = loadPolicy(file) as any;
      const count = policy.regulatory_clauses.length;
      expect(count).toBeGreaterThanOrEqual(8);
      expect(count).toBeLessThanOrEqual(12);
    });

    test(`INV: ${file} — receipt_retention_days ≥ 365`, () => {
      const policy = loadPolicy(file) as any;
      expect(policy.compliance_thresholds.receipt_retention_days).toBeGreaterThanOrEqual(365);
    });

    test(`INV: ${file} — receipt chain algorithm is SHA3-256`, () => {
      const policy = loadPolicy(file) as any;
      expect(policy.receipt_chain.algorithm).toBe("SHA3-256");
    });

    test(`INV: ${file} — quorum format matches N-of-M`, () => {
      const policy = loadPolicy(file) as any;
      expect(policy.receipt_chain.quorum).toMatch(/^\d+-of-\d+$/);
    });
  }
});