wishforge-ide/tests/evolution-source.test.ts
eust-w b47a07e8d2 chore: baseline snapshot of the Codex-delivered code before review
Imports the previously untracked working tree as-is so that the review
changes that follow (requirements -> design -> implementation) can be
reviewed as diffs and reverted if needed. No content changes.

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

52 lines
5.4 KiB
TypeScript

import { expect, it } from 'vitest';
import { generateKeyPairSync } from 'node:crypto';
import type { Actor, Project } from '@wishforge/contracts';
import { signRecord } from '../packages/evolution/src/index.js';
import { ControlPlane, record } from '../apps/api/src/domain/control-plane.js';
import { Evolution } from '../apps/api/src/domain/evolution.js';
import { digest } from '../apps/api/src/domain/policy.js';
import { MemoryStore } from './helpers/memory-store.js';
const owner: Actor = { tenantId: 'platform', userId: 'owner', kind: 'human', role: 'owner' };
const verifier: Actor = { ...owner, userId: 'verifier', kind: 'verifier' };
async function fixture(paths = ['packages/runtime/src/some-tool.ts']) {
const db = new MemoryStore(); db.seed('platform');
const env: NodeJS.ProcessEnv = { NODE_ENV: 'test', WF_SYSTEM_TENANT: 'platform' };
const control = new ControlPlane(db, env); const evolution = new Evolution(db, env);
const project = await control.createProject(owner, { name: 'Platform source', repositoryUrl: 'https://example.test/platform.git', verificationCommands: [['node', '--test']] });
await db.transaction('platform', tx => tx.put('provider_connection', { ...record('platform'), provider: 'codex', status: 'active' }));
const result = await control.submit({ ...owner, kind: 'evaluator' }, project.id, { goal: 'Repair repeated tool failure', acceptanceCriteria: ['Protected checks pass'] }, 'source-evolution-001', { purpose: 'evolution', noDeployment: true, sourceRunIds: ['source-failure'], baselineBundle: 'baseline-v1', evolutionScope: 'tool', metaTaskId: 'meta-source' });
const run = await control.lease(verifier, result.run_id, 'verifier'); const token = run.lease!.token;
// Domain fixture metadata; actual Git restoration is covered by runtime tests.
await control.checkpoint(verifier, run.id, { sourceCommit: 'a'.repeat(40), baseCommit: 'b'.repeat(40), gitBundleSha256: 'c'.repeat(64) }, token);
await control.evidence(verifier, { leaseToken: token, runId: run.id, revision: 1, sourceCommit: 'a'.repeat(40), type: 'verification', passed: true, commands: [{ argv: ['node', '--test'], exitCode: 0, outputDigest: digest('pass') }] });
await control.classify(verifier, run.id, { leaseToken: token, sourceCommit: 'a'.repeat(40), changedPaths: paths, migration: false, permissionsExpanded: true });
return { db, env, control, evolution, project, runId: run.id, token };
}
it('archives a verified source proposal without installing code or marking delivery', async () => {
const f = await fixture(); const candidate = await f.evolution.createCodeCandidate(verifier, f.runId, f.token);
expect(candidate).toMatchObject({ scope: 'tool', status: 'proposed', sourceArtifact: { sourceCommit: 'a'.repeat(40), gitBundleSha256: 'c'.repeat(64), changedPaths: ['packages/runtime/src/some-tool.ts'] } });
expect(candidate.bundleConfig).toBeUndefined();
expect(await f.control.run(owner, f.runId)).toMatchObject({ status: 'waiting', waitingReason: 'evolution_evaluation', noDeployment: true });
expect(await f.db.transaction('platform', tx => tx.list('release'))).toEqual([]);
expect((await f.evolution.createCodeCandidate(verifier, f.runId, f.token)).id).toBe(candidate.id);
});
it('does not allow the Builder to extract or attest its own candidate', async () => {
const f = await fixture(); await expect(f.evolution.createCodeCandidate({ ...verifier, kind: 'worker' }, f.runId, f.token)).rejects.toMatchObject({ code: 'FORBIDDEN' });
});
it('escalates a protected code path to the separate trust-root channel', async () => {
const f = await fixture(['apps/api/src/auth.ts']); const candidate = await f.evolution.createCodeCandidate(verifier, f.runId, f.token);
expect(candidate.scope).toBe('trust_root');
await expect(f.evolution.command(owner, candidate.id, 'approve')).rejects.toMatchObject({ code: 'EXTERNAL_TRUST_ROOT_RELEASE_REQUIRED' });
});
it('invalidates proposal extraction when the protected verification policy changes', async () => {
const f = await fixture(); await f.db.transaction('platform', async tx => { const project = await tx.get<Project>('project', f.project.id); await tx.put('project', { ...project!, verificationCommands: [['node', 'different-test.js']] }); });
await expect(f.evolution.createCodeCandidate(verifier, f.runId, f.token)).rejects.toMatchObject({ code: 'STALE_EVIDENCE' });
});
it('binds a signed external build receipt to the proposal source, not only its candidate id', async () => {
const f = await fixture(); const candidate = await f.evolution.createCodeCandidate(verifier, f.runId, f.token);
const keys = generateKeyPairSync('ed25519'); f.env.WF_PLATFORM_RELEASE_PUBLIC_KEYS = JSON.stringify({ release: keys.publicKey.export({ type: 'spki', format: 'pem' }).toString() });
const receipt = signRecord({ schemaVersion: 1, tenantId: 'platform', candidateId: candidate.id, candidateDigest: candidate.candidateDigest, keyId: 'release', issuedAt: new Date().toISOString(), operationId: 'external-build-001', kind: 'platform_release', releaseDigest: candidate.candidateDigest, sourceCommit: 'd'.repeat(40), imageDigest: `registry.example/platform@sha256:${'e'.repeat(64)}`, deploymentId: 'independent-fixture', policyGatePassed: true, target: 'independent_canary' }, keys.privateKey.export({ type: 'pkcs8', format: 'pem' }).toString());
await expect(f.evolution.observe({ ...owner, kind: 'evaluator' }, candidate.id, receipt)).rejects.toMatchObject({ code: 'EVIDENCE_BINDING_MISMATCH' });
});