import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import type { Actor, Project } from '@wishforge/contracts'; import { ControlPlane } from '../apps/api/src/domain/control-plane.js'; import { MemoryStore } from './helpers/memory-store.js'; // A wish may start from a branch people pushed with their own changes. const env = { NODE_ENV: 'test', WF_DISPATCH_TENANTS: 'demo', WF_GIT_SERVICE_URL: 'http://127.0.0.1:4504', WF_GIT_ADMIN_TOKEN: 'git-admin-token-for-tests-only-0001', }; const owner: Actor = { tenantId: 'demo', userId: 'owner', role: 'owner', kind: 'human' }; let store: MemoryStore; let project: Project; beforeEach(async () => { vi.stubGlobal('fetch', async (input: string | URL, init: RequestInit) => { const body = JSON.parse(String(init.body)); if (new URL(String(input)).pathname === '/repos') return Response.json({ repositoryUrl: `https://git.test/git/${body.tenantId}/${body.projectId}.git`, initialCommit: 'a'.repeat(40), defaultBranch: 'main', }); return Response.json({ token: 'payload.signature', expiresAt: new Date(Date.now() + 900_000).toISOString() }); }); store = new MemoryStore(); store.seed('demo'); project = await new ControlPlane(store, env).createProject(owner, { name: 'Todo App' }); }); afterEach(() => vi.unstubAllGlobals()); describe('wishes that start from another branch', () => { it('records the branch on the run and refuses the target branch or the platform’s own branches', async () => { const control = new ControlPlane(store, env); const submit = (sourceBranch: string, key: string) => control.submit( owner, project.id, { goal: 'Merge the pushed work', acceptanceCriteria: ['Checks pass'], sourceBranch }, key, ); await expect(submit('main', 'source-branch-0001')).rejects.toMatchObject({ code: 'INVALID_SOURCE_BRANCH' }); await expect(submit('wishforge/run/abc', 'source-branch-0002')).rejects.toMatchObject({ code: 'INVALID_SOURCE_BRANCH', }); await expect(submit('../main', 'source-branch-0003')).rejects.toThrow('A safe Git branch name is required'); const { run_id } = await submit('feature/work', 'source-branch-0004'); expect(await control.run(owner, run_id)).toMatchObject({ sourceBranch: 'feature/work' }); }); });