feat: initialize AI agent harness
This commit is contained in:
354
tests/harness.test.mjs
Normal file
354
tests/harness.test.mjs
Normal file
@@ -0,0 +1,354 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { execFile } from "node:child_process";
|
||||
import { cp, mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { createRequire } from "node:module";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import test from "node:test";
|
||||
import { promisify } from "node:util";
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
const {
|
||||
composeProject,
|
||||
planProject,
|
||||
registerProject,
|
||||
selectProjectIdsByContract,
|
||||
verifyProject,
|
||||
} = require("../dist/index.cjs");
|
||||
|
||||
const packageRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
|
||||
const execFileAsync = promisify(execFile);
|
||||
|
||||
async function writeText(filePath, content) {
|
||||
await mkdir(path.dirname(filePath), { recursive: true });
|
||||
await writeFile(filePath, content, "utf8");
|
||||
}
|
||||
|
||||
async function createFixture(t) {
|
||||
const temporaryRoot = await mkdtemp(path.join(os.tmpdir(), "kri-agent-harness-"));
|
||||
t.after(async () => {
|
||||
await rm(temporaryRoot, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
const harnessRoot = path.join(temporaryRoot, "_________AgentHarnessBase");
|
||||
const appRoot = path.join(temporaryRoot, "apps", "sample-front");
|
||||
const anotherAppRoot = path.join(temporaryRoot, "apps", "another-front");
|
||||
const unrelatedRoot = path.join(temporaryRoot, "tools", "unrelated-tool");
|
||||
const providerRoot = path.join(temporaryRoot, "providers", "kri-ui-registry");
|
||||
|
||||
await mkdir(path.join(harnessRoot, "schemas"), { recursive: true });
|
||||
await cp(
|
||||
path.join(packageRoot, "schemas", "profile.schema.json"),
|
||||
path.join(harnessRoot, "schemas", "profile.schema.json"),
|
||||
);
|
||||
await cp(
|
||||
path.join(packageRoot, "schemas", "workspace.schema.json"),
|
||||
path.join(harnessRoot, "schemas", "workspace.schema.json"),
|
||||
);
|
||||
|
||||
await writeText(
|
||||
path.join(harnessRoot, "workspace.yml"),
|
||||
`schema_version: 1
|
||||
workspace_root: ..
|
||||
projects:
|
||||
sample-front:
|
||||
path: apps/sample-front
|
||||
unrelated-tool:
|
||||
path: tools/unrelated-tool
|
||||
kri-ui-registry:
|
||||
path: providers/kri-ui-registry
|
||||
another-front:
|
||||
path: apps/another-front
|
||||
`,
|
||||
);
|
||||
await writeText(
|
||||
path.join(harnessRoot, "catalog", "contracts.yml"),
|
||||
`schema_version: 1
|
||||
contracts:
|
||||
- id: kri-ui-registry
|
||||
provider_project_id: kri-ui-registry
|
||||
consumer_file: .agent/consumer.md
|
||||
order: 100
|
||||
required_layers:
|
||||
- frontend
|
||||
- fullstack
|
||||
`,
|
||||
);
|
||||
|
||||
const fragments = {
|
||||
"sources/base.md": "# Base Rule",
|
||||
"sources/project-types/internal-service.md": "## Internal Service Rule",
|
||||
"sources/project-types/library.md": "## Library Rule",
|
||||
"sources/project-types/scaffold-cli.md": "## Scaffold CLI Rule",
|
||||
"sources/stacks/typescript.md": "## TypeScript Rule",
|
||||
"sources/stacks/react.md": "## React Rule",
|
||||
"sources/stacks/nextjs.md": "## Next.js Rule",
|
||||
"sources/stacks/mysql.md": "## MySQL Rule",
|
||||
"sources/policies/planned-migration.md": "## Migration Rule",
|
||||
};
|
||||
for (const [relativePath, content] of Object.entries(fragments)) {
|
||||
await writeText(path.join(harnessRoot, relativePath), `${content}\n`);
|
||||
}
|
||||
|
||||
await writeText(
|
||||
path.join(providerRoot, ".agent", "consumer.md"),
|
||||
"## KRI UI Consumer Rule\n",
|
||||
);
|
||||
await writeText(
|
||||
path.join(providerRoot, ".agent", "profile.yml"),
|
||||
`schema_version: 1
|
||||
project:
|
||||
id: kri-ui-registry
|
||||
name: KRI UI Registry
|
||||
type: library
|
||||
layer: frontend
|
||||
legacy: false
|
||||
stack:
|
||||
- typescript
|
||||
- react
|
||||
shared_contracts:
|
||||
provides:
|
||||
- kri-ui-registry
|
||||
`,
|
||||
);
|
||||
await writeText(path.join(providerRoot, ".agent", "project.md"), "## Registry Project Rule\n");
|
||||
await writeText(
|
||||
path.join(appRoot, ".agent", "profile.yml"),
|
||||
`schema_version: 1
|
||||
project:
|
||||
id: sample-front
|
||||
name: Sample Front
|
||||
type: internal-service
|
||||
layer: frontend
|
||||
legacy: false
|
||||
stack:
|
||||
- typescript
|
||||
- react
|
||||
- nextjs
|
||||
shared_contracts:
|
||||
consumes:
|
||||
- kri-ui-registry
|
||||
databases:
|
||||
- id: application
|
||||
engine: mysql
|
||||
role: primary
|
||||
access: read-write
|
||||
migration:
|
||||
status: planned
|
||||
target_stack:
|
||||
- typescript
|
||||
- react
|
||||
- nextjs
|
||||
strategy: staged
|
||||
`,
|
||||
);
|
||||
await writeText(path.join(appRoot, ".agent", "project.md"), "## Project Rule\n");
|
||||
|
||||
await writeText(
|
||||
path.join(anotherAppRoot, ".agent", "profile.yml"),
|
||||
`schema_version: 1
|
||||
project:
|
||||
id: another-front
|
||||
name: Another Front
|
||||
type: internal-service
|
||||
layer: frontend
|
||||
legacy: false
|
||||
stack:
|
||||
- typescript
|
||||
- react
|
||||
- nextjs
|
||||
shared_contracts:
|
||||
consumes:
|
||||
- kri-ui-registry
|
||||
`,
|
||||
);
|
||||
await writeText(path.join(anotherAppRoot, ".agent", "project.md"), "## Another Project Rule\n");
|
||||
|
||||
await writeText(
|
||||
path.join(unrelatedRoot, ".agent", "profile.yml"),
|
||||
`schema_version: 1
|
||||
project:
|
||||
id: unrelated-tool
|
||||
name: Unrelated Tool
|
||||
type: scaffold-cli
|
||||
layer: standalone
|
||||
legacy: false
|
||||
stack:
|
||||
- typescript
|
||||
`,
|
||||
);
|
||||
await writeText(path.join(unrelatedRoot, ".agent", "project.md"), "## Unrelated Rule\n");
|
||||
|
||||
return { temporaryRoot, harnessRoot, appRoot, anotherAppRoot, providerRoot, unrelatedRoot };
|
||||
}
|
||||
|
||||
test("composes fragments in deterministic order and verifies byte-for-byte output", async (t) => {
|
||||
const { harnessRoot, appRoot } = await createFixture(t);
|
||||
const plan = await planProject(harnessRoot, "sample-front");
|
||||
|
||||
assert.deepEqual(
|
||||
plan.fragments.map((fragment) => `${fragment.kind}:${fragment.id}`),
|
||||
[
|
||||
"base:base",
|
||||
"project-type:internal-service",
|
||||
"stack:typescript",
|
||||
"stack:react",
|
||||
"stack:nextjs",
|
||||
"database:mysql",
|
||||
"policy:planned-migration",
|
||||
"shared-contract:kri-ui-registry",
|
||||
"project:sample-front",
|
||||
],
|
||||
);
|
||||
assert.equal(plan.output.agents, "missing");
|
||||
assert.equal(plan.output.claude, "missing");
|
||||
|
||||
const result = await composeProject(harnessRoot, "sample-front");
|
||||
assert.deepEqual(result.changed, ["AGENTS.md", "CLAUDE.md"]);
|
||||
assert.equal(await readFile(path.join(appRoot, "AGENTS.md"), "utf8"), result.agentsContent);
|
||||
assert.equal(await readFile(path.join(appRoot, "CLAUDE.md"), "utf8"), "@AGENTS.md\n");
|
||||
|
||||
const verified = await verifyProject(harnessRoot, "sample-front");
|
||||
assert.deepEqual(verified.output, { agents: "current", claude: "current" });
|
||||
assert.deepEqual((await composeProject(harnessRoot, "sample-front")).changed, []);
|
||||
});
|
||||
|
||||
test("rejects nextjs without react", async (t) => {
|
||||
const { harnessRoot, appRoot } = await createFixture(t);
|
||||
const profilePath = path.join(appRoot, ".agent", "profile.yml");
|
||||
const profile = await readFile(profilePath, "utf8");
|
||||
await writeFile(profilePath, profile.replace(" - react\n", ""), "utf8");
|
||||
|
||||
await assert.rejects(
|
||||
() => planProject(harnessRoot, "sample-front"),
|
||||
/nextjs stack must also declare react/,
|
||||
);
|
||||
});
|
||||
|
||||
test("refuses to overwrite unmanaged AGENTS.md", async (t) => {
|
||||
const { harnessRoot, appRoot } = await createFixture(t);
|
||||
await writeText(path.join(appRoot, "AGENTS.md"), "# Hand-written instructions\n");
|
||||
|
||||
await assert.rejects(
|
||||
() => composeProject(harnessRoot, "sample-front"),
|
||||
/Refusing to overwrite unmanaged AGENTS\.md/,
|
||||
);
|
||||
assert.equal(
|
||||
await readFile(path.join(appRoot, "AGENTS.md"), "utf8"),
|
||||
"# Hand-written instructions\n",
|
||||
);
|
||||
assert.equal(await readFile(path.join(appRoot, "CLAUDE.md"), "utf8").catch(() => null), null);
|
||||
});
|
||||
|
||||
test("registers a safe relative project path and rejects path escape", async (t) => {
|
||||
const { temporaryRoot, harnessRoot } = await createFixture(t);
|
||||
const newProjectRoot = path.join(temporaryRoot, "apps", "new-tool");
|
||||
await writeText(
|
||||
path.join(newProjectRoot, ".agent", "profile.yml"),
|
||||
`schema_version: 1
|
||||
project:
|
||||
id: new-tool
|
||||
name: New Tool
|
||||
type: scaffold-cli
|
||||
layer: standalone
|
||||
legacy: false
|
||||
stack:
|
||||
- typescript
|
||||
`,
|
||||
);
|
||||
|
||||
const result = await registerProject(harnessRoot, "new-tool", "apps/new-tool");
|
||||
assert.equal(result.changed, true);
|
||||
const localWorkspace = await readFile(path.join(harnessRoot, "workspace.local.yml"), "utf8");
|
||||
assert.match(localWorkspace, /new-tool:/);
|
||||
assert.match(localWorkspace, /path: apps\/new-tool/);
|
||||
|
||||
await assert.rejects(
|
||||
() => registerProject(harnessRoot, "outside", "../outside"),
|
||||
/escapes the allowed root/,
|
||||
);
|
||||
});
|
||||
|
||||
test("fails instead of truncating output above the hard size limit", async (t) => {
|
||||
const { harnessRoot, appRoot } = await createFixture(t);
|
||||
await writeText(path.join(appRoot, ".agent", "project.md"), `## Project Rule\n\n${"x".repeat(31_000)}\n`);
|
||||
|
||||
await assert.rejects(
|
||||
() => planProject(harnessRoot, "sample-front"),
|
||||
/exceeds the 30720-byte limit/,
|
||||
);
|
||||
});
|
||||
|
||||
test("selects a contract provider first, sorts consumers, and composes only affected projects", async (t) => {
|
||||
const {
|
||||
harnessRoot,
|
||||
appRoot,
|
||||
anotherAppRoot,
|
||||
providerRoot,
|
||||
unrelatedRoot,
|
||||
} = await createFixture(t);
|
||||
|
||||
assert.deepEqual(
|
||||
await selectProjectIdsByContract(harnessRoot, "kri-ui-registry"),
|
||||
["kri-ui-registry", "another-front", "sample-front"],
|
||||
);
|
||||
await assert.rejects(
|
||||
() => selectProjectIdsByContract(harnessRoot, "unknown-contract"),
|
||||
/Unknown shared contract: unknown-contract/,
|
||||
);
|
||||
|
||||
const environment = {
|
||||
...process.env,
|
||||
KRI_AGENT_HARNESS_ROOT: harnessRoot,
|
||||
};
|
||||
const composed = await execFileAsync(
|
||||
process.execPath,
|
||||
[path.join(packageRoot, "dist", "cli.cjs"), "compose", "--contract", "kri-ui-registry"],
|
||||
{ env: environment },
|
||||
);
|
||||
assert.equal(composed.stderr, "");
|
||||
assert.ok(composed.stdout.indexOf("[kri-ui-registry]") < composed.stdout.indexOf("[another-front]"));
|
||||
assert.ok(composed.stdout.indexOf("[another-front]") < composed.stdout.indexOf("[sample-front]"));
|
||||
|
||||
for (const affectedRoot of [providerRoot, anotherAppRoot, appRoot]) {
|
||||
assert.match(await readFile(path.join(affectedRoot, "AGENTS.md"), "utf8"), /GENERATED BY KRI AGENT HARNESS/);
|
||||
assert.equal(await readFile(path.join(affectedRoot, "CLAUDE.md"), "utf8"), "@AGENTS.md\n");
|
||||
}
|
||||
assert.equal(
|
||||
await readFile(path.join(unrelatedRoot, "AGENTS.md"), "utf8").catch(() => null),
|
||||
null,
|
||||
);
|
||||
|
||||
const verified = await execFileAsync(
|
||||
process.execPath,
|
||||
[path.join(packageRoot, "dist", "cli.cjs"), "verify", "--contract", "kri-ui-registry"],
|
||||
{ env: environment },
|
||||
);
|
||||
assert.equal(verified.stderr, "");
|
||||
});
|
||||
|
||||
test("runs the bundled CLI against an isolated harness root", async (t) => {
|
||||
const { harnessRoot } = await createFixture(t);
|
||||
const { stdout, stderr } = await execFileAsync(
|
||||
process.execPath,
|
||||
[path.join(packageRoot, "dist", "cli.cjs"), "list"],
|
||||
{
|
||||
env: {
|
||||
...process.env,
|
||||
KRI_AGENT_HARNESS_ROOT: harnessRoot,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
assert.equal(stderr, "");
|
||||
assert.match(stdout, /sample-front\tshared\tpresent\tapps\/sample-front/);
|
||||
assert.match(stdout, /kri-ui-registry\tshared\tpresent\tproviders\/kri-ui-registry/);
|
||||
|
||||
const help = await execFileAsync(
|
||||
process.execPath,
|
||||
[path.join(packageRoot, "dist", "cli.cjs"), "--help"],
|
||||
);
|
||||
assert.equal(help.stderr, "");
|
||||
assert.match(help.stdout, /KRI Agent Harness/);
|
||||
});
|
||||
Reference in New Issue
Block a user