wishforge-ide/tests/verification-profile.test.ts
eust-w ee2fbe9f6a fix: match the Kubernetes baseline to the provisioner and drop stale settings
The base manifests described a design the code does not have: a Role that only
allowed Jobs, an unused sandbox account, a tool gateway that does not exist and
a deployer bound into the sandbox namespace. They now grant exactly what the
provisioner applies and deletes for runs and IDE workspaces, let sandbox pods
reach the API, Temporal and Git through the control namespace's default deny,
and let the IDE gateway reach workspace pods.

- The API reads the runtime image from WF_RUN_WORKER_IMAGE only, the same name
  the provisioner uses; the WF_RUN_IMAGE alias is gone.
- The provisioner takes the public gateway origin from WF_IDE_PUBLIC_URL, which
  is what the gateway and API call it, and refuses to start without an explicit
  WF_DEPLOYMENT_MODE.
- Environments no longer accept a runnerId that was always rejected.
- The Compose acceptance script uses the current Docker context unless
  WF_DOCKER_CONTEXT names one, instead of a hardcoded colima.
- Tests cover waiting reasons and their exits, and the production rejection of
  unsandboxed or unstated verification isolation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 18:01:28 +08:00

209 lines
9.0 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import type { Actor, Project, VerificationProfile } from '@wishforge/contracts';
import { MemoryStore } from './helpers/memory-store.js';
import { ControlPlane } from '../apps/api/src/domain/control-plane.js';
import { Connections } from '../apps/api/src/domain/connections.js';
import { Releases } from '../apps/api/src/domain/releases.js';
import { digest } from '../apps/api/src/domain/policy.js';
import {
configuredVerificationProfiles,
verificationPolicyDigest,
} from '../apps/api/src/domain/verification-policy.js';
import { validateVerificationProfile } from '../packages/runtime/src/verification-profile.js';
const owner: Actor = { tenantId: 'alpha', userId: 'owner', role: 'owner', kind: 'human' };
const verifier: Actor = { ...owner, userId: 'independent-verifier', kind: 'verifier' };
const profile: VerificationProfile = {
id: 'catalog',
version: '1',
commands: [['/usr/local/bin/node', '/usr/local/lib/wishforge-verification/catalog/accept.mjs']],
files: [{ path: '/usr/local/lib/wishforge-verification/catalog/accept.mjs', sha256: 'a'.repeat(64) }],
};
async function fixture(withProfile = true) {
const db = new MemoryStore();
db.seed('alpha');
const dev = new ControlPlane(db, { NODE_ENV: 'test', WF_VAULT_KEY: 'a'.repeat(64) });
const project = await dev.createProject(owner, {
name: 'Catalog',
repositoryUrl: 'https://example.test/catalog.git',
verificationCommands: [['node', '--test']],
});
const deliveryConfig = {
containerPort: 3000,
healthPath: '/health',
smokeChecks: [{ path: '/', expectedStatus: 200 }],
};
await db.transaction('alpha', tx =>
tx.put('project', { ...project, deliveryConfig, ...(withProfile ? { verificationProfile: profile } : {}) }),
);
await new Connections(db, dev.env).create(owner, {
provider: 'codex',
label: 'fixture',
apiKey: 'test-only-credential',
});
const { run_id: runId } = await dev.submit(
owner,
project.id,
{ goal: 'Catalog search', acceptanceCriteria: ['Results match persisted data'] },
'profile-wish-001',
);
// The executor hands its checkpoint to verification first.
const executor: Actor = { ...owner, userId: 'executor', kind: 'worker' };
const implementing = await dev.lease(executor, runId, 'executor');
for (const stage of ['plan', 'implement', 'verify'])
await dev.transition(executor, runId, {
leaseToken: implementing.lease!.token,
stage,
status: 'running',
message: `Fixture ${stage}`,
});
await dev.releaseLease(executor, runId, implementing.lease!.token);
const leased = await dev.lease(verifier, runId, 'verifier');
const env = { NODE_ENV: 'production', WF_VERIFICATION_PROFILES_JSON: JSON.stringify([profile]) };
const control = new ControlPlane(db, env);
await control.integration.acquire(verifier, runId, leased.lease!.token);
const evidence = {
leaseToken: leased.lease!.token,
runId,
revision: 1,
sourceCommit: 'b'.repeat(40),
type: 'verification',
passed: true,
isolation: 'sandbox',
verificationProfileDigest: digest(profile),
commands: [...project.verificationCommands, ...profile.commands].map(argv => ({
argv,
exitCode: 0,
outputDigest: digest('observed output'),
})),
};
/** What the verifier does between verification and the build: integrate the verified commit. */
const integrate = async () => {
const step = { leaseToken: evidence.leaseToken, status: 'running', message: 'Fixture step' };
await control.transition(verifier, runId, { ...step, stage: 'integrate' });
await control.evidence(verifier, {
leaseToken: evidence.leaseToken,
runId,
revision: 1,
sourceCommit: evidence.sourceCommit,
type: 'integration',
passed: true,
targetRef: 'refs/heads/main',
verificationProfileDigest: digest(profile),
});
await control.transition(verifier, runId, { ...step, stage: 'build' });
};
return { db, project, control, env, evidence, integrate };
}
describe('independent acceptance profiles', () => {
it('does not accept repository-only test success in production', async () => {
const { control, evidence } = await fixture(false);
await expect(control.evidence(verifier, evidence)).rejects.toMatchObject({ code: 'VERIFICATION_PROFILE_REQUIRED' });
});
it('rejects verification run without a sandbox in production (FR-D2)', async () => {
const { control, evidence } = await fixture();
await expect(control.evidence(verifier, { ...evidence, isolation: 'host' })).rejects.toMatchObject({
code: 'SANDBOX_REQUIRED',
});
const { isolation: _, ...unstated } = evidence;
await expect(control.evidence(verifier, unstated)).rejects.toMatchObject({ code: 'ISOLATION_REQUIRED' });
});
it('requires both supplemental checks and pinned acceptance commands', async () => {
const { control, evidence } = await fixture();
await expect(
control.evidence(verifier, { ...evidence, commands: evidence.commands.slice(0, 1) }),
).rejects.toMatchObject({ code: 'INCOMPLETE_VERIFICATION' });
await expect(
control.evidence(verifier, { ...evidence, verificationProfileDigest: digest('changed') }),
).rejects.toMatchObject({ code: 'STALE_EVIDENCE' });
expect(await control.evidence(verifier, evidence)).toMatchObject({
passed: true,
verificationProfileDigest: digest(profile),
});
});
it('invalidates build authorization after the profile changes', async () => {
const { db, project, control, env, evidence, integrate } = await fixture();
await control.evidence(verifier, evidence);
await integrate();
const next = { ...profile, version: '2' };
env.WF_VERIFICATION_PROFILES_JSON = JSON.stringify([next]);
await db.transaction('alpha', async tx => {
const current = await tx.get<Project>('project', project.id);
await tx.put('project', { ...current!, verificationProfile: next });
});
await expect(
control.evidence(verifier, {
leaseToken: evidence.leaseToken,
runId: evidence.runId,
revision: 1,
sourceCommit: evidence.sourceCommit,
type: 'build',
passed: true,
verificationProfileDigest: digest(next),
imageDigest: `registry.example/test@sha256:${'c'.repeat(64)}`,
}),
).rejects.toMatchObject({ code: 'UNVERIFIED_BUILD' });
});
it('cannot use an old approval after operator profile replacement', async () => {
const { control, db, project, env, evidence, integrate } = await fixture();
const verified = await control.evidence(verifier, evidence);
await integrate();
const built = await control.evidence(verifier, {
leaseToken: evidence.leaseToken,
runId: evidence.runId,
revision: 1,
sourceCommit: evidence.sourceCommit,
type: 'build',
passed: true,
verificationProfileDigest: digest(profile),
imageDigest: `registry.example/test@sha256:${'c'.repeat(64)}`,
});
const releases = new Releases(db, env);
const environment = await releases.createEnvironment(owner, {
projectId: project.id,
name: 'prod',
type: 'compose',
production: true,
applicationUrl: 'https://catalog.example.test/',
});
const release = await releases.request(
owner,
{
runId: evidence.runId,
environmentId: environment.id,
verificationEvidenceId: verified.id,
buildEvidenceId: built.id,
},
'profile-release-001',
);
// Production approvals need a second person: the requester cannot approve their own release.
await expect(
releases.decide(owner, release.approvalId!, { decision: 'approve', bindingDigest: release.bindingDigest }),
).rejects.toMatchObject({ code: 'SELF_APPROVAL_FORBIDDEN' });
const reviewer: Actor = { ...owner, userId: 'reviewer', role: 'admin' };
env.WF_VERIFICATION_PROFILES_JSON = JSON.stringify([{ ...profile, version: '2' }]);
await expect(
releases.decide(reviewer, release.approvalId!, { decision: 'approve', bindingDigest: release.bindingDigest }),
).rejects.toMatchObject({ code: 'VERIFICATION_PROFILE_REPLACED' });
});
it('rejects caller-like profile paths and unpinned commands', async () => {
expect(() =>
configuredVerificationProfiles({
WF_VERIFICATION_PROFILES_JSON: JSON.stringify([{ ...profile, commands: [['node', 'test.js']] }]),
}),
).toThrow('configuration');
await expect(
validateVerificationProfile({ ...profile, files: [{ path: '/workspace/accept.mjs', sha256: 'a'.repeat(64) }] }),
).rejects.toMatchObject({ code: 'VERIFICATION_PROFILE_INVALID' });
});
it('binds profile content, not just a reusable name, into the policy digest', () => {
const project = { verificationCommands: [['node', '--test']], verificationProfile: profile } as Project;
expect(verificationPolicyDigest(project)).not.toBe(
verificationPolicyDigest({
...project,
verificationProfile: { ...profile, files: [{ ...profile.files[0]!, sha256: 'd'.repeat(64) }] },
}),
);
});
});