wishforge-ide/apps/worker/test/project-contract.test.ts
eust-w 0ef99b1e6d style: add Prettier and EditorConfig, format the codebase
Adds prettier 3.9.8 (exact) with a shared config (single quotes,
120 columns, trailing commas), .prettierignore, .editorconfig and the
`format` / `format:check` scripts, then formats all TypeScript,
JavaScript, CSS, JSON and YAML sources.

Purely mechanical: no behaviour change. Typecheck passes, the vitest
suite and the 74 IDE node tests produce the same results as before
(socket-binding tests still cannot run inside this sandbox).
Previously the longest source line was ~3000 characters; reviewing the
following semantic changes now produces readable diffs.

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

58 lines
2.8 KiB
TypeScript

import { afterEach, expect, it } from 'vitest';
import { mkdtemp, rm, writeFile } from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import { GitWorkspace } from '@wishforge/runtime';
import { projectContract } from '../src/project-contract.js';
const directories: string[] = [];
afterEach(async () => {
for (const dir of directories.splice(0)) await rm(dir, { recursive: true, force: true });
});
async function fixture(yaml: string) {
const root = await mkdtemp(path.join(os.tmpdir(), 'wishforge-project-contract-'));
directories.push(root);
const git = new GitWorkspace();
const source = await git.create({ root, runId: 'fixture' });
await writeFile(path.join(source.workspace, 'wishforge.yaml'), yaml);
const checkpoint = await git.checkpoint(source.workspace, source.baseCommit, 'fixture', 'test');
return { workspace: source.workspace, sourceCommit: checkpoint.sourceCommit, commands: [['/trusted/acceptance']] };
}
it('keeps trusted checks before supplemental manifest tests and classifies migrations', async () => {
const input = await fixture(
'schemaVersion: 1\ntests: [[npm, test]]\nmigrations: { required: true, strategy: expand-contract, commands: [[node, migrate.js]] }\n',
);
const result = await projectContract(input);
expect(result.commands).toEqual([['/trusted/acceptance'], ['npm', 'test']]);
expect(result.migrationRequired).toBe(true);
});
it('requires an explicit grant and rejects a repository resource escalation', async () => {
const input = await fixture('schemaVersion: 1\nresources: { cpuMilli: 4000, memoryMiB: 2048 }\n');
await expect(projectContract(input)).rejects.toMatchObject({ code: 'MANIFEST_RESOURCE_POLICY_REQUIRED' });
await expect(
projectContract({ ...input, resourceLimits: { cpuMilli: 2000, memoryMiB: 4096 } }),
).rejects.toMatchObject({ code: 'MANIFEST_RESOURCE_LIMIT' });
});
it('does not silently replace approved source context or runtime health settings', async () => {
const input = await fixture(
'schemaVersion: 1\nbuild: { dockerfile: app/Dockerfile, context: app }\nports: [{ name: web, containerPort: 3000 }]\nhealth: { path: /health, port: web }\n',
);
await expect(projectContract({ ...input, delivery: { dockerfile: 'Dockerfile' } })).rejects.toMatchObject({
code: 'MANIFEST_BUILD_POLICY_MISMATCH',
});
await expect(
projectContract({
...input,
delivery: { dockerfile: 'app/Dockerfile', sourceContext: 'app', containerPort: 3000, healthPath: '/ready' },
}),
).rejects.toMatchObject({ code: 'MANIFEST_HEALTH_POLICY_MISMATCH' });
expect(
(
await projectContract({
...input,
delivery: { dockerfile: 'app/Dockerfile', sourceContext: 'app', containerPort: 3000, healthPath: '/health' },
})
).checked?.sourceCommit,
).toBe(input.sourceCommit);
});