wishforge-ide/tests/integration/artifacts.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

134 lines
6.2 KiB
TypeScript

import { afterAll, beforeAll, describe, expect, it } from 'vitest';
import { randomUUID } from 'node:crypto';
import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
import { execFile } from 'node:child_process';
import { promisify } from 'node:util';
import os from 'node:os';
import path from 'node:path';
import { DeleteObjectsCommand, ListObjectsV2Command, PutObjectCommand } from '@aws-sdk/client-s3';
import type { Actor } from '@wishforge/contracts';
import { GitWorkspace, type PortableCheckpoint } from '@wishforge/runtime';
import { ControlPlane } from '../../apps/api/src/domain/control-plane.js';
import { Connections } from '../../apps/api/src/domain/connections.js';
import type { ArtifactRecord } from '../../apps/api/src/domain/artifacts.js';
import { MemoryStore } from '../helpers/memory-store.js';
const enabled = process.env.WF_ARTIFACT_INTEGRATION === '1';
const prefix = `integration-${randomUUID()}`;
const env = {
NODE_ENV: 'test',
WF_VAULT_KEY: 'd'.repeat(64),
S3_ENDPOINT: process.env.WF_TEST_S3_ENDPOINT ?? 'http://127.0.0.1:49000',
S3_REGION: 'us-east-1',
S3_BUCKET: 'wishforge-artifacts',
S3_ACCESS_KEY_ID: 'wishforge-dev',
S3_SECRET_ACCESS_KEY: 'wishforge-minio-dev-only',
S3_ARTIFACT_PREFIX: prefix,
};
const owner: Actor = { tenantId: 'artifact-test-alpha', userId: 'owner', role: 'owner', kind: 'human' };
const worker: Actor = { ...owner, kind: 'worker', userId: 'worker' };
const command = promisify(execFile);
describe.skipIf(!enabled)('real MinIO checkpoint persistence and clean-workspace recovery', () => {
let temp: string, db: MemoryStore, control: ControlPlane;
beforeAll(async () => {
temp = await mkdtemp(path.join(os.tmpdir(), 'wishforge-artifact-'));
db = new MemoryStore();
db.seed(owner.tenantId);
db.seed('artifact-test-beta');
control = new ControlPlane(db, env);
});
afterAll(async () => {
if (control) {
const client = control.artifacts.objects.client;
const listed = await client.send(new ListObjectsV2Command({ Bucket: env.S3_BUCKET, Prefix: `${prefix}/` }));
const owned = (listed.Contents ?? []).filter(o => o.Key?.startsWith(`${prefix}/`)).map(o => ({ Key: o.Key! }));
if (owned.length)
await client.send(new DeleteObjectsCommand({ Bucket: env.S3_BUCKET, Delete: { Objects: owned } }));
client.destroy();
}
if (temp) await rm(temp, { recursive: true, force: true });
});
it('stores bundle bytes outside PostgreSQL, verifies ownership/digest and restores real Git contents', async () => {
const project = await control.createProject(owner, {
name: 'MinIO fixture',
repositoryUrl: 'https://example.org/fixture.git',
});
await new Connections(db, env).create(owner, {
provider: 'codex',
label: 'fixture',
apiKey: 'fixture-only-not-real',
});
const wish = await control.submit(
owner,
project.id,
{ goal: 'Persist Git checkpoint', acceptanceCriteria: ['Fresh verifier can restore exact source'] },
'minio-checkpoint',
);
const run = await control.lease(worker, wish.run_id, 'artifact-worker');
const repo = path.join(temp, 'source');
await command('git', ['init', '-b', 'main', repo]);
await command('git', ['-C', repo, 'config', 'user.name', 'Artifact Test']);
await command('git', ['-C', repo, 'config', 'user.email', 'artifact-test@example.invalid']);
await writeFile(path.join(repo, 'proof.txt'), 'durable checkpoint fixture\n');
await command('git', ['-C', repo, 'add', 'proof.txt']);
await command('git', ['-C', repo, 'commit', '-m', 'fixture']);
const commit = (await command('git', ['-C', repo, 'rev-parse', 'HEAD'])).stdout.trim();
const git = new GitWorkspace();
const portable = await git.checkpoint(repo, commit, run.id, 'codex');
const checkpoint = await control.checkpoint(
worker,
run.id,
portable as unknown as Record<string, unknown>,
run.lease!.token,
repo,
);
const stored = checkpoint.checkpoint;
expect(stored.gitBundleBase64).toBeUndefined();
expect(stored.artifactStorage).toBe('s3');
const again = await control.checkpoint(
worker,
run.id,
portable as unknown as Record<string, unknown>,
run.lease!.token,
repo,
);
expect(again.checkpoint.gitBundleArtifactId).toBe(stored.gitBundleArtifactId);
const artifactId = String(stored.gitBundleArtifactId);
expect(await control.artifacts.metadata(owner, artifactId)).toMatchObject({
runId: run.id,
projectId: project.id,
sha256: portable.gitBundleSha256,
});
await expect(
control.artifacts.download({ ...owner, tenantId: 'artifact-test-beta' }, artifactId),
).rejects.toMatchObject({ code: 'NOT_FOUND' });
await expect(
control.artifacts.download({ ...owner, userId: 'viewer', role: 'viewer' }, artifactId),
).rejects.toMatchObject({ code: 'NOT_FOUND' });
await expect(
control.artifacts.hydrateCheckpoint({ ...worker, runId: 'other-run' }, run.id, stored),
).rejects.toMatchObject({ code: 'FORBIDDEN' });
const hydrated = await control.artifacts.hydrateCheckpoint(worker, run.id, stored);
const restored = await git.restore({
checkpoint: hydrated as unknown as PortableCheckpoint,
root: path.join(temp, 'fresh-verifier'),
runId: 'restore-fixture',
});
expect(await readFile(path.join(restored.workspace, 'proof.txt'), 'utf8')).toBe('durable checkpoint fixture\n');
const artifact = await db.transaction(owner.tenantId, tx => tx.get<ArtifactRecord>('artifact', artifactId));
expect(await db.transaction(owner.tenantId, tx => tx.list('artifact'))).toHaveLength(1);
expect((await control.usage(owner)).storageBytes).toBe(artifact!.size);
// Corrupt only this test's private prefix to prove digest verification catches valid-looking metadata.
await control.artifacts.objects.client.send(
new PutObjectCommand({
Bucket: env.S3_BUCKET,
Key: artifact!.object.key,
Body: Buffer.alloc(artifact!.size),
Metadata: { tenant: owner.tenantId, project: project.id, run: run.id, sha256: artifact!.sha256 },
}),
);
await expect(control.artifacts.download(owner, artifactId)).rejects.toMatchObject({
code: 'ARTIFACT_DIGEST_MISMATCH',
});
}, 30_000);
});