* refactor(test): colocate pure-domain tests and reduce test debt Move focused immutable domain/contracts/adapter tests from tests/composition/** beside their production owners under src/** as colocated *.test.ts/*.fixture.ts, and relocate persistence-shaped composition tests under tests/boundary/filesystem/**. Update import paths, the Vitest project topology (shared worker context for pure domain/contracts and recording-port service projects), the test migration ledger, testing docs, and AGENTS.md to match. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * docs: remove test migration ledger The cutover is complete and the ledger is no longer referenced anywhere in source, scripts, or the docs gate. Drop the standalone artifact. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * docs: clean up stale migration artifacts Remove empty directories left by the test migration and replace the "final cutover" language in docs/testing.md with standing PR guidance that no longer references the completed migration. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix: repair CI failures from schema, knip, and stale generated artifacts Three root causes fixed: 1. catalog-schemas.mjs: valuesAt only traversed anyOf from Zod's toJSONSchema, not oneOf. The webScreenshotDiffSchema change to a discriminated union produces oneOf, so the product catalog generator could not derive the web_screenshot_diff schema version. Add oneOf traversal. 2. conformancePackage.ts: the test rewrite removed the only imports of ScenarioManifest, ReplayPlan, ShimPlan, and ExpectedEvidence, making them unused exports that knip flags. Remove the dead type aliases. 3. Regenerate stale artifacts (product catalog, MCP tool catalog, skill metadata, conformance manifest) that drifted from the source changes. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com> --------- Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
351 lines
11 KiB
TypeScript
351 lines
11 KiB
TypeScript
import { mkdir, symlink, writeFile } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
|
|
|
import {
|
|
canonicalizePermissionCeilings,
|
|
canonicalizePermissionRequest,
|
|
createPermissionAuthority,
|
|
type PermissionAuthority,
|
|
} from "../../../src/application/PermissionAuthority.js";
|
|
import {
|
|
createPermissionPolicy,
|
|
evaluatePermission,
|
|
type PermissionGrant,
|
|
type PermissionScope,
|
|
} from "../../../src/domain/permissionPolicy.js";
|
|
|
|
describe("permission authority", () => {
|
|
it("distinguishes an elicitable grant from administrator reconfiguration", async () => {
|
|
const sandbox = await createTestTempDirectory(
|
|
"rea-permission-remediation-",
|
|
);
|
|
const allowedRoot = join(sandbox, "allowed");
|
|
const outsideRoot = join(sandbox, "outside");
|
|
await Promise.all([mkdir(allowedRoot), mkdir(outsideRoot)]);
|
|
const authority = await createPermissionAuthority([
|
|
evidenceScope(allowedRoot),
|
|
]);
|
|
expect(authority.ok).toBe(true);
|
|
if (!authority.ok) return;
|
|
|
|
const inside = await authority.value.authorize(
|
|
{
|
|
...evidenceScope(allowedRoot),
|
|
operation_identity: "read:inside-ceiling",
|
|
},
|
|
"read",
|
|
{ remediation: "elicit" },
|
|
);
|
|
expect(inside).toMatchObject({
|
|
ok: false,
|
|
error: { remediation: "elicit" },
|
|
});
|
|
|
|
const outside = await authority.value.createConnectionAuthority().authorize(
|
|
{
|
|
...evidenceScope(outsideRoot),
|
|
operation_identity: "read:outside-ceiling",
|
|
},
|
|
"read",
|
|
{ remediation: "elicit" },
|
|
);
|
|
expect(outside).toMatchObject({
|
|
ok: false,
|
|
error: { remediation: "restart" },
|
|
});
|
|
});
|
|
|
|
it("accepts canonical aliases but rejects a symlink escape", async () => {
|
|
const sandbox = await createTestTempDirectory("rea-permission-");
|
|
const root = join(sandbox, "root");
|
|
const outside = join(sandbox, "outside");
|
|
await Promise.all([mkdir(root), mkdir(outside)]);
|
|
await writeFile(join(root, "inside"), "inside");
|
|
await writeFile(join(outside, "secret"), "secret");
|
|
await symlink(root, join(sandbox, "root-alias"));
|
|
await symlink(join(outside, "secret"), join(root, "escape"));
|
|
const ceiling = await canonicalizePermissionCeilings([
|
|
{
|
|
capability: "evidence_read",
|
|
roots: [join(sandbox, "root-alias")],
|
|
executables: [],
|
|
environment_names: [],
|
|
network: "none",
|
|
mount: false,
|
|
},
|
|
]);
|
|
expect(ceiling.ok).toBe(true);
|
|
if (!ceiling.ok) return;
|
|
const policy = createPermissionPolicy(ceiling.value, [
|
|
{
|
|
...ceiling.value[0]!,
|
|
grant_id: "administrator:evidence-read",
|
|
lifetime: "administrator",
|
|
operation_identity: null,
|
|
expires_at: null,
|
|
},
|
|
]);
|
|
const inside = await canonicalizePermissionRequest(
|
|
{
|
|
capability: "evidence_read",
|
|
roots: [join(root, "inside")],
|
|
executables: [],
|
|
environment_names: [],
|
|
network: "none",
|
|
mount: false,
|
|
operation_identity: "read:inside",
|
|
},
|
|
"read",
|
|
);
|
|
const escaped = await canonicalizePermissionRequest(
|
|
{
|
|
capability: "evidence_read",
|
|
roots: [join(root, "escape")],
|
|
executables: [],
|
|
environment_names: [],
|
|
network: "none",
|
|
mount: false,
|
|
operation_identity: "read:escape",
|
|
},
|
|
"read",
|
|
);
|
|
expect(inside.ok).toBe(true);
|
|
expect(escaped.ok).toBe(true);
|
|
if (!inside.ok || !escaped.ok) return;
|
|
expect(evaluatePermission(policy, inside.value)).toMatchObject({
|
|
allowed: true,
|
|
});
|
|
expect(evaluatePermission(policy, escaped.value)).toMatchObject({
|
|
allowed: false,
|
|
reason: "outside_administrator_ceiling",
|
|
});
|
|
});
|
|
|
|
it("canonicalizes a new write through its existing parent", async () => {
|
|
const sandbox = await createTestTempDirectory("rea-permission-write-");
|
|
const result = await canonicalizePermissionRequest(
|
|
{
|
|
capability: "evidence_write",
|
|
roots: [join(sandbox, "new.json")],
|
|
executables: [],
|
|
environment_names: [],
|
|
network: "none",
|
|
mount: false,
|
|
operation_identity: "write:new.json",
|
|
},
|
|
"write",
|
|
);
|
|
|
|
expect(result).toMatchObject({ ok: true });
|
|
if (result.ok)
|
|
expect(result.value.roots).toEqual([join(sandbox, "new.json")]);
|
|
});
|
|
});
|
|
|
|
describe("permission authority policy and connection lifecycle", () => {
|
|
it("commits a configured policy only after every grant validates", async () => {
|
|
const sandbox = await createTestTempDirectory("rea-permission-reload-");
|
|
const oldRoot = join(sandbox, "old");
|
|
const newRoot = join(sandbox, "new");
|
|
const outsideRoot = join(sandbox, "outside");
|
|
await Promise.all([mkdir(oldRoot), mkdir(newRoot), mkdir(outsideRoot)]);
|
|
const oldScope = evidenceScope(oldRoot);
|
|
const newScope = evidenceScope(newRoot);
|
|
const authority = await createPermissionAuthority(
|
|
[oldScope],
|
|
[administratorGrant(oldScope)],
|
|
);
|
|
expect(authority.ok).toBe(true);
|
|
if (!authority.ok) return;
|
|
|
|
const reloaded = await authority.value.replaceConfiguredPolicy({
|
|
ceilings: [newScope],
|
|
administratorGrants: [administratorGrant(newScope)],
|
|
projectGrants: [
|
|
{
|
|
...evidenceScope(outsideRoot),
|
|
grant_id: "project:outside-ceiling",
|
|
lifetime: "project",
|
|
operation_identity: null,
|
|
expires_at: null,
|
|
},
|
|
],
|
|
});
|
|
|
|
expect(reloaded.ok).toBe(false);
|
|
expect(await canRead(authority.value, oldRoot)).toBe(true);
|
|
expect(await canRead(authority.value, newRoot)).toBe(false);
|
|
|
|
expect(
|
|
await authority.value.replaceConfiguredPolicy({
|
|
ceilings: [newScope],
|
|
administratorGrants: [administratorGrant(newScope)],
|
|
projectGrants: [],
|
|
}),
|
|
).toEqual({ ok: true, value: null });
|
|
expect(await canRead(authority.value, oldRoot)).toBe(false);
|
|
expect(await canRead(authority.value, newRoot)).toBe(true);
|
|
});
|
|
|
|
it("isolates transient grants and consumption between live connections", async () => {
|
|
const sandbox = await createTestTempDirectory("rea-permission-session-");
|
|
const scope = evidenceScope(sandbox);
|
|
const configured = await createPermissionAuthority([scope]);
|
|
expect(configured.ok).toBe(true);
|
|
if (!configured.ok) return;
|
|
const connectionA = configured.value.createConnectionAuthority();
|
|
const connectionB = configured.value.createConnectionAuthority();
|
|
const request = {
|
|
...scope,
|
|
operation_identity: "read:connection-owned",
|
|
};
|
|
|
|
expect(
|
|
connectionA.grant({
|
|
...request,
|
|
grant_id: "session:a",
|
|
lifetime: "session",
|
|
operation_identity: null,
|
|
expires_at: null,
|
|
}),
|
|
).toMatchObject({ ok: true });
|
|
expect(await connectionA.authorize(request, "read")).toMatchObject({
|
|
ok: true,
|
|
});
|
|
expect(await connectionB.authorize(request, "read")).toMatchObject({
|
|
ok: false,
|
|
});
|
|
|
|
connectionB.clearSessionGrants();
|
|
expect(await connectionA.authorize(request, "read")).toMatchObject({
|
|
ok: true,
|
|
});
|
|
|
|
connectionA.clearSessionGrants();
|
|
const onceRequest = {
|
|
...request,
|
|
operation_identity: "read:once-on-a",
|
|
};
|
|
expect(
|
|
connectionA.grant({
|
|
...onceRequest,
|
|
grant_id: "once:a",
|
|
lifetime: "once",
|
|
expires_at: null,
|
|
}),
|
|
).toMatchObject({ ok: true });
|
|
expect(
|
|
connectionB.grant({
|
|
...onceRequest,
|
|
grant_id: "once:a",
|
|
lifetime: "once",
|
|
expires_at: null,
|
|
}),
|
|
).toMatchObject({ ok: true });
|
|
const concurrent = await Promise.all([
|
|
connectionA.authorize(onceRequest, "read"),
|
|
connectionA.authorize(onceRequest, "read"),
|
|
]);
|
|
expect(concurrent.filter(({ ok }) => ok)).toHaveLength(1);
|
|
expect(await connectionB.authorize(onceRequest, "read")).toMatchObject({
|
|
ok: true,
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("permission authority policy reload", () => {
|
|
it("applies configured policy reloads to existing connections", async () => {
|
|
const sandbox = await createTestTempDirectory(
|
|
"rea-permission-live-reload-",
|
|
);
|
|
const allowedRoot = join(sandbox, "allowed");
|
|
const replacementRoot = join(sandbox, "replacement");
|
|
await Promise.all([mkdir(allowedRoot), mkdir(replacementRoot)]);
|
|
const configured = await createPermissionAuthority([
|
|
evidenceScope(allowedRoot),
|
|
]);
|
|
expect(configured.ok).toBe(true);
|
|
if (!configured.ok) return;
|
|
const connection = configured.value.createConnectionAuthority();
|
|
expect(
|
|
connection.grant({
|
|
...evidenceScope(allowedRoot),
|
|
grant_id: "session:before-reload",
|
|
lifetime: "session",
|
|
operation_identity: null,
|
|
expires_at: null,
|
|
}),
|
|
).toMatchObject({ ok: true });
|
|
expect(await canRead(connection, allowedRoot)).toBe(true);
|
|
|
|
const collisionRequest = {
|
|
...evidenceScope(allowedRoot),
|
|
operation_identity: "read:reload-collision",
|
|
};
|
|
expect(
|
|
connection.grant({
|
|
...collisionRequest,
|
|
grant_id: "shared-id",
|
|
lifetime: "once",
|
|
expires_at: null,
|
|
}),
|
|
).toMatchObject({ ok: true });
|
|
expect(await connection.authorize(collisionRequest, "read")).toMatchObject({
|
|
ok: true,
|
|
});
|
|
expect(
|
|
await configured.value.replaceAdministratorGrants([
|
|
{
|
|
...evidenceScope(allowedRoot),
|
|
grant_id: "shared-id",
|
|
lifetime: "administrator",
|
|
operation_identity: null,
|
|
expires_at: null,
|
|
},
|
|
]),
|
|
).toEqual({ ok: true, value: null });
|
|
expect(await connection.authorize(collisionRequest, "read")).toMatchObject({
|
|
ok: true,
|
|
});
|
|
|
|
expect(
|
|
await configured.value.reload([evidenceScope(replacementRoot)]),
|
|
).toEqual({ ok: true, value: null });
|
|
expect(await canRead(connection, allowedRoot)).toBe(false);
|
|
});
|
|
});
|
|
|
|
const evidenceScope = (root: string): PermissionScope => ({
|
|
capability: "evidence_read",
|
|
roots: [root],
|
|
executables: [],
|
|
environment_names: [],
|
|
network: "none",
|
|
mount: false,
|
|
});
|
|
|
|
const administratorGrant = (scope: PermissionScope): PermissionGrant => ({
|
|
...scope,
|
|
grant_id: "administrator:evidence_read",
|
|
lifetime: "administrator",
|
|
operation_identity: null,
|
|
expires_at: null,
|
|
});
|
|
|
|
const canRead = async (
|
|
authority: PermissionAuthority,
|
|
root: string,
|
|
): Promise<boolean> =>
|
|
(
|
|
await authority.explain(
|
|
{
|
|
...evidenceScope(root),
|
|
operation_identity: `read:${root}`,
|
|
},
|
|
"read",
|
|
)
|
|
).ok;
|