auth-yes/.forum/poc-g2/frontmatter_poc.ts

90 lines
2.5 KiB
TypeScript

import { assertEquals } from "https://deno.land/std@0.224.0/testing/asserts.ts";
import { parse as yamlParse } from "https://deno.land/std@0.224.0/yaml/mod.ts";
/**
* Generation 2 Proof of Concept: Declarative Frontmatter (YAML UUIDs)
*
* This module demonstrates extracting strict UUIDv7 identifiers from
* Markdown YAML frontmatter using production-ready YAML parsing,
* ensuring precise artifact identification.
*/
// A helper to test if a string matches UUIDv7 structure
// (8-4-4-4-12, where the 13th hex char is '7')
function isUUIDv7(uuid: string): boolean {
const uuidV7Regex =
/^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
return uuidV7Regex.test(uuid);
}
export function extractArtifactId(markdownContent: string): string {
// Extract YAML block strictly
const match = markdownContent.match(/^---\n([\s\S]*?)\n---/);
if (!match || !match[1]) {
throw new Error("No YAML frontmatter found");
}
// Parse YAML block with std/yaml
const metadata = yamlParse(match[1]) as Record<string, unknown>;
const id = metadata["Artifact-ID"];
if (!id || typeof id !== "string") {
throw new Error("Missing or invalid Artifact-ID in frontmatter");
}
if (!isUUIDv7(id)) {
throw new Error(`Artifact-ID ${id} is not a valid UUIDv7`);
}
return id;
}
if (import.meta.main) {
console.log("Running Gen 2 Declarative Frontmatter PoC tests...");
// Mock UUIDv7: 018fa031-6e3e-7a2e-8c4d-91b5a6c7d8e9
const validMarkdown = `---
Artifact-ID: 018fa031-6e3e-7a2e-8c4d-91b5a6c7d8e9
Title: Authentication Architecture
Status: Draft
---
# Authentication Architecture
This document details the auth system...
`;
const invalidMarkdown = `---
Artifact-ID: task-123
Title: Legacy Task
---
# Legacy System
`;
try {
const id = extractArtifactId(validMarkdown);
assertEquals(id, "018fa031-6e3e-7a2e-8c4d-91b5a6c7d8e9");
console.log(
"✅ Gen 2 Frontmatter PoC successful: Correctly extracted UUIDv7 using std/yaml.",
);
// Test rejection of non-UUIDv7
let failedAsExpected = false;
try {
extractArtifactId(invalidMarkdown);
} catch (e: any) {
if (e.message.includes("not a valid UUIDv7")) {
failedAsExpected = true;
}
}
assertEquals(failedAsExpected, true, "Should have rejected invalid UUID");
console.log(
"✅ Gen 2 Frontmatter PoC successful: Successfully rejected non-UUIDv7 artifact.",
);
} catch (err) {
console.error("❌ Gen 2 Frontmatter PoC failed:", err);
Deno.exit(1);
}
}