import assert from 'node:assert/strict' import { registerHooks } from 'node:module' import { test } from 'node:test' const HEAD_SHA = 'abc123def456789012345678901234567890abcd' const PR_GQL_RESPONSE = { data: { repository: { pullRequest: { title: 'Test PR', url: 'https://github.com/test/repo/pull/42', state: 'OPEN', isDraft: false, createdAt: '2026-01-01T00:00:00Z', merged: false, mergedAt: null, mergeable: 'MERGEABLE', mergeStateStatus: 'CLEAN', reviewDecision: 'APPROVED', author: { login: 'testuser', __typename: 'User' }, baseRefName: 'main', headRefName: 'feature', headRefOid: HEAD_SHA, additions: 10, deletions: 5, mergeCommit: null, baseRef: { refUpdateRule: { requiredStatusCheckContexts: [] } }, timelineItems: { nodes: [{ createdAt: '2026-01-01T00:00:00Z' }] }, commits: { nodes: [{ commit: { oid: HEAD_SHA, committedDate: '2026-01-01T00:00:00Z', statusCheckRollup: { state: 'SUCCESS', contexts: { nodes: [ { __typename: 'CheckRun', name: 'Validate', status: 'COMPLETED', conclusion: 'SUCCESS', detailsUrl: '', startedAt: '2026-01-01T00:00:00Z', completedAt: '2026-01-01T00:00:01Z', checkSuite: { app: { slug: 'github-actions' } } }, ] } } } }] }, reviewThreads: { pageInfo: { hasNextPage: false }, nodes: [] }, reviews: { totalCount: 1, nodes: [{ databaseId: 1, state: 'APPROVED', body: '', submittedAt: '2026-01-01T00:00:00Z', url: '', authorAssociation: 'COLLABORATOR', commit: { oid: HEAD_SHA }, author: { login: 'reviewer', __typename: 'User' } }] }, comments: { totalCount: 0, nodes: [] }, }, }, }, } const STUB = ` const store = globalThis.__factoryStore ??= new Map(); export const packageStorage = () => ({ async get(k) { return store.get(k) ?? null }, async set(k, v) { store.set(k, JSON.stringify(v)) }, }); export const packageContext = { appBasePath: '/apps/factory/' }; export async function credential(key) { throw new Error('credential not mapped'); } export async function credentials() { return [] } export const kody = {}; ` registerHooks({ resolve(spec, ctx, next) { if (spec === 'kody:runtime') return { url: `data:text/javascript,${encodeURIComponent(STUB)}`, shortCircuit: true } if (spec === 'kody:@kody/github/pr/merge') { const code = `export default async function(input) { globalThis.__mergePrCalls.push(JSON.parse(JSON.stringify(input))); return { merged: true, sha: 'mergesha123', status: 'merged' }; }` return { url: `data:text/javascript,${encodeURIComponent(code)}`, shortCircuit: true } } if (spec === 'kody:@kody/github/graphql') { const code = `export default async function() { return globalThis.__prGqlResponse; }` return { url: `data:text/javascript,${encodeURIComponent(code)}`, shortCircuit: true } } if (spec === 'kody:@kody/github/request') { const code = `export default async function(input) { if (input.path && input.path.includes('/actions/runs')) return { ok: true, data: { workflow_runs: [] } }; return { ok: true, data: {} }; }` return { url: `data:text/javascript,${encodeURIComponent(code)}`, shortCircuit: true } } if (spec === 'kody:@kody/clef/evaluate') { const code = `export default async function() { return { ok: false, error: 'not configured' }; }` return { url: `data:text/javascript,${encodeURIComponent(code)}`, shortCircuit: true } } if (spec.startsWith('kody:@kody/discord/')) { const code = `export default async function() { return {}; }` return { url: `data:text/javascript,${encodeURIComponent(code)}`, shortCircuit: true } } return next(spec, ctx) }, }) const g = globalThis as unknown as { __mergePrCalls: Array>; __prGqlResponse: unknown } g.__mergePrCalls = [] g.__prGqlResponse = PR_GQL_RESPONSE const { default: shipMerge } = await import('./merge.ts') test('live merge calls mergePr with confirm: true and the head SHA', async () => { g.__mergePrCalls = [] g.__prGqlResponse = PR_GQL_RESPONSE const result = await shipMerge({ prUrl: 'https://github.com/test/repo/pull/42', risk: 'low', }) assert.equal(result.merged, true, 'merge should succeed') assert.equal(g.__mergePrCalls.length, 1, 'mergePr must be called exactly once') const call = g.__mergePrCalls[0]! assert.equal(call.confirm, true, 'mergePr must be called with confirm: true') assert.equal(call.sha, HEAD_SHA, 'mergePr must pin to the snapshot head SHA') assert.equal(call.mergeMethod, 'squash', 'default merge method is squash') }) test('dry-run does not call mergePr and returns wouldMerge', async () => { g.__mergePrCalls = [] g.__prGqlResponse = PR_GQL_RESPONSE const result = await shipMerge({ prUrl: 'https://github.com/test/repo/pull/42', risk: 'low', dryRun: true, }) assert.equal(result.merged, false, 'dry-run does not merge') assert.equal(result.dryRun, true) assert.equal(result.wouldMerge, true) assert.equal(g.__mergePrCalls.length, 0, 'mergePr must not be called on dry-run') assert.equal(result.headSha, HEAD_SHA, 'returns the head SHA') })