|
| 1 | +// SPDX-License-Identifier: PMPL-1.0-or-later |
| 2 | +// Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) <j.d.a.jewell@open.ac.uk> |
| 3 | + |
| 4 | +import { assertEquals, assert } from "https://deno.land/std@0.208.0/assert/mod.ts"; |
| 5 | + |
| 6 | +// Unit tests: Basic A2ML example structure validation |
| 7 | +Deno.test("Unit: A2ML examples file exists", async () => { |
| 8 | + const path = "content/examples.md"; |
| 9 | + const result = await Deno.stat(path); |
| 10 | + assertEquals(result.isFile, true); |
| 11 | +}); |
| 12 | + |
| 13 | +Deno.test("Unit: A2ML examples contain required sections", async () => { |
| 14 | + const content = await Deno.readTextFile("content/examples.md"); |
| 15 | + assert(content.includes("# Examples")); |
| 16 | + assert(content.includes("Minimal Manifest")); |
| 17 | + assert(content.includes("CI/CD Agent Manifest")); |
| 18 | + assert(content.includes("Security Scanner Manifest")); |
| 19 | + assert(content.includes("Multi-Agent Orchestration")); |
| 20 | +}); |
| 21 | + |
| 22 | +Deno.test("Unit: A2ML examples have SPDX headers", async () => { |
| 23 | + const content = await Deno.readTextFile("content/examples.md"); |
| 24 | + const matches = content.match(/SPDX-License-Identifier: PMPL-1\.0-or-later/g); |
| 25 | + assert(matches !== null && matches.length >= 4, "Should have at least 4 SPDX headers"); |
| 26 | +}); |
| 27 | + |
| 28 | +Deno.test("Unit: All content markdown files exist", async () => { |
| 29 | + const expectedFiles = [ |
| 30 | + "content/index.md", |
| 31 | + "content/specification.md", |
| 32 | + "content/examples.md", |
| 33 | + "content/integrations.md", |
| 34 | + "content/getting-started.md", |
| 35 | + ]; |
| 36 | + |
| 37 | + for (const file of expectedFiles) { |
| 38 | + const result = await Deno.stat(file); |
| 39 | + assertEquals(result.isFile, true, `File ${file} should exist`); |
| 40 | + } |
| 41 | +}); |
| 42 | + |
| 43 | +// Smoke tests: A2ML syntax validation |
| 44 | +Deno.test("Smoke: A2ML attestation blocks are properly formatted", async () => { |
| 45 | + const content = await Deno.readTextFile("content/examples.md"); |
| 46 | + const attestationBlocks = content.match(/@attestation:([\s\S]*?)@end/g); |
| 47 | + assert( |
| 48 | + attestationBlocks !== null && attestationBlocks.length > 0, |
| 49 | + "Should have at least one @attestation block" |
| 50 | + ); |
| 51 | + |
| 52 | + // Validate structure of first attestation |
| 53 | + const firstBlock = attestationBlocks[0]; |
| 54 | + assert(firstBlock.includes("agent-id:"), "attestation should have agent-id"); |
| 55 | + assert(firstBlock.includes("trust-level:"), "attestation should have trust-level"); |
| 56 | + assert(firstBlock.includes("capabilities:"), "attestation should have capabilities"); |
| 57 | +}); |
| 58 | + |
| 59 | +Deno.test("Smoke: A2ML policy blocks are present", async () => { |
| 60 | + const content = await Deno.readTextFile("content/examples.md"); |
| 61 | + const policyBlocks = content.match(/@policy:([\s\S]*?)@end/g); |
| 62 | + assert(policyBlocks !== null && policyBlocks.length > 0, "Should have at least one @policy block"); |
| 63 | + |
| 64 | + policyBlocks.forEach((block) => { |
| 65 | + assert(block.includes("require:") || block.includes("enforce:"), |
| 66 | + "policy should have require or enforce"); |
| 67 | + }); |
| 68 | +}); |
| 69 | + |
| 70 | +Deno.test("Smoke: README references content correctly", async () => { |
| 71 | + const readme = await Deno.readTextFile("README.adoc"); |
| 72 | + assert(readme.includes("content/")); |
| 73 | + assert(readme.includes("A2ML")); |
| 74 | +}); |
| 75 | + |
| 76 | +// Contract tests: Required A2ML fields |
| 77 | +Deno.test("Contract: Every attestation has required fields", async () => { |
| 78 | + const content = await Deno.readTextFile("content/examples.md"); |
| 79 | + const attestationBlocks = content.match(/@attestation:([\s\S]*?)@end/g) || []; |
| 80 | + |
| 81 | + assert(attestationBlocks.length > 0, "Should have attestation blocks"); |
| 82 | + |
| 83 | + attestationBlocks.forEach((block, idx) => { |
| 84 | + assert(block.includes("agent-id:"), `Attestation ${idx} missing agent-id`); |
| 85 | + assert(block.includes("attested-by:"), `Attestation ${idx} missing attested-by`); |
| 86 | + assert(block.includes("trust-level:"), `Attestation ${idx} missing trust-level`); |
| 87 | + }); |
| 88 | +}); |
| 89 | + |
| 90 | +Deno.test("Contract: Trust levels are valid values", async () => { |
| 91 | + const content = await Deno.readTextFile("content/examples.md"); |
| 92 | + const validLevels = ["self-declared", "verified", "audited"]; |
| 93 | + |
| 94 | + validLevels.forEach((level) => { |
| 95 | + assert(content.includes(`trust-level: ${level}`), |
| 96 | + `Should have examples of trust-level: ${level}`); |
| 97 | + }); |
| 98 | +}); |
| 99 | + |
| 100 | +// Aspect tests: Cross-cutting consistency |
| 101 | +Deno.test("Aspect: All agents have consistent naming", async () => { |
| 102 | + const content = await Deno.readTextFile("content/examples.md"); |
| 103 | + const agentIds = new Set<string>(); |
| 104 | + |
| 105 | + const matches = content.matchAll(/agent-id:\s*(\S+)/g); |
| 106 | + for (const match of matches) { |
| 107 | + const agentId = match[1]; |
| 108 | + assert( |
| 109 | + agentId.match(/^[a-z0-9\-]+$/), |
| 110 | + `Agent ID "${agentId}" should be lowercase alphanumeric with hyphens` |
| 111 | + ); |
| 112 | + agentIds.add(agentId); |
| 113 | + } |
| 114 | + |
| 115 | + assert(agentIds.size > 0, "Should have at least one agent"); |
| 116 | +}); |
| 117 | + |
| 118 | +Deno.test("Aspect: All examples have proper formatting", async () => { |
| 119 | + const content = await Deno.readTextFile("content/examples.md"); |
| 120 | + |
| 121 | + // Check for balanced @...@end blocks |
| 122 | + const attestationCount = (content.match(/@attestation:/g) || []).length; |
| 123 | + const attestationEndCount = (content.match(/@end/g) || []).length; |
| 124 | + |
| 125 | + assert(attestationCount > 0, "Should have attestation blocks"); |
| 126 | + assert(attestationEndCount >= attestationCount, "Should have matching @end tags"); |
| 127 | +}); |
| 128 | + |
| 129 | +Deno.test("Aspect: SPDX headers consistent in all examples", async () => { |
| 130 | + const files = [ |
| 131 | + "content/examples.md", |
| 132 | + "content/specification.md", |
| 133 | + "content/integrations.md", |
| 134 | + ]; |
| 135 | + |
| 136 | + const results = []; |
| 137 | + for (const file of files) { |
| 138 | + try { |
| 139 | + const content = await Deno.readTextFile(file); |
| 140 | + const hasSpdx = content.includes("SPDX-License-Identifier"); |
| 141 | + results.push({ file, hasSpdx }); |
| 142 | + } catch { |
| 143 | + // File may not exist |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + assert(results.length > 0, "Should find content files"); |
| 148 | +}); |
| 149 | + |
| 150 | +// Property-based tests: Invariants |
| 151 | +Deno.test("Property: Every agent-id appears in at least one attestation", async () => { |
| 152 | + const content = await Deno.readTextFile("content/examples.md"); |
| 153 | + const agentIds = new Set<string>(); |
| 154 | + const attestations = new Map<string, number>(); |
| 155 | + |
| 156 | + // Extract agent IDs |
| 157 | + const agentMatches = content.matchAll(/agent-id:\s*(\S+)/g); |
| 158 | + for (const match of agentMatches) { |
| 159 | + const agentId = match[1]; |
| 160 | + agentIds.add(agentId); |
| 161 | + attestations.set(agentId, (attestations.get(agentId) || 0) + 1); |
| 162 | + } |
| 163 | + |
| 164 | + agentIds.forEach((id) => { |
| 165 | + assert( |
| 166 | + attestations.has(id), |
| 167 | + `Agent ID ${id} should appear in attestations` |
| 168 | + ); |
| 169 | + }); |
| 170 | +}); |
| 171 | + |
| 172 | +Deno.test("Property: Trust level progression is documented", async () => { |
| 173 | + const content = await Deno.readTextFile("content/examples.md"); |
| 174 | + |
| 175 | + // self-declared -> verified -> audited progression |
| 176 | + const selfDeclared = (content.match(/trust-level:\s*self-declared/g) || []).length; |
| 177 | + const verified = (content.match(/trust-level:\s*verified/g) || []).length; |
| 178 | + const audited = (content.match(/trust-level:\s*audited/g) || []).length; |
| 179 | + |
| 180 | + assert(selfDeclared > 0, "Should have self-declared examples"); |
| 181 | + assert(verified > 0, "Should have verified examples"); |
| 182 | + assert(audited > 0, "Should have audited examples"); |
| 183 | +}); |
| 184 | + |
| 185 | +// E2E/Reflexive tests: Complete pipeline |
| 186 | +Deno.test("E2E: Example parsing round-trip", async () => { |
| 187 | + const content = await Deno.readTextFile("content/examples.md"); |
| 188 | + |
| 189 | + // Extract first example block |
| 190 | + const match = content.match(/@attestation:([\s\S]*?)@end/); |
| 191 | + assert(match !== null, "Should find an attestation block"); |
| 192 | + |
| 193 | + const attestationBlock = match[1]; |
| 194 | + |
| 195 | + // Verify it can be parsed as key-value pairs |
| 196 | + const lines = attestationBlock.split("\n").filter((l) => l.trim()); |
| 197 | + const parsed = new Map<string, string>(); |
| 198 | + |
| 199 | + for (const line of lines) { |
| 200 | + if (line.includes(":")) { |
| 201 | + const [key, ...rest] = line.split(":"); |
| 202 | + parsed.set(key.trim(), rest.join(":").trim()); |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + assert(parsed.size > 0, "Should parse fields from attestation"); |
| 207 | + assert(parsed.has("agent-id"), "Should have agent-id field"); |
| 208 | +}); |
| 209 | + |
| 210 | +Deno.test("E2E: All references are resolvable", async () => { |
| 211 | + const content = await Deno.readTextFile("content/examples.md"); |
| 212 | + const refMatches = content.matchAll(/\[(\d+)\]\s+([^\n]+)/g); |
| 213 | + |
| 214 | + const references = new Map<string, string>(); |
| 215 | + for (const match of refMatches) { |
| 216 | + references.set(match[1], match[2]); |
| 217 | + } |
| 218 | + |
| 219 | + assert(references.size > 0, "Should have references"); |
| 220 | + |
| 221 | + // Verify references are cited |
| 222 | + for (const [num] of references) { |
| 223 | + const cited = content.includes(`[${num}]`); |
| 224 | + assert(cited, `Reference [${num}] should be cited in text`); |
| 225 | + } |
| 226 | +}); |
| 227 | + |
| 228 | +// Benchmark baseline (timing assertions) |
| 229 | +Deno.test("Benchmark: A2ML parsing performance", async () => { |
| 230 | + const start = performance.now(); |
| 231 | + const content = await Deno.readTextFile("content/examples.md"); |
| 232 | + const end = performance.now(); |
| 233 | + |
| 234 | + const duration = end - start; |
| 235 | + assert(duration < 100, `File read should complete in < 100ms, took ${duration.toFixed(2)}ms`); |
| 236 | +}); |
| 237 | + |
| 238 | +Deno.test("Benchmark: Example count baseline", async () => { |
| 239 | + const content = await Deno.readTextFile("content/examples.md"); |
| 240 | + const exampleCount = (content.match(/## \d+\./g) || []).length; |
| 241 | + |
| 242 | + // Baseline: we expect ~4 examples |
| 243 | + assert(exampleCount >= 3, `Should have at least 3 numbered examples, found ${exampleCount}`); |
| 244 | +}); |
0 commit comments