import assert from 'node:assert/strict' import { registerHooks } from 'node:module' import { test } from 'node:test' let lastGraphqlInput: Record | null = null let lastRequestInput: Record | null = null 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/graphql') { const code = `export default async function(input) { globalThis.__lastGraphqlInput = input; return { data: { repository: { pullRequest: null } } }; }` return { url: `data:text/javascript,${encodeURIComponent(code)}`, shortCircuit: true } } if (spec === 'kody:@kody/github/request') { const code = `export default async function(input) { globalThis.__lastRequestInput = input; return { ok: true, status: 200, data: {} }; }` return { url: `data:text/javascript,${encodeURIComponent(code)}`, shortCircuit: true } } return next(spec, ctx) }, }) const g = globalThis as unknown as { __lastGraphqlInput: Record | null; __lastRequestInput: Record | null } test('githubGraphql is called without an account option', async () => { const { fetchSnapshot } = await import('./github-port.ts') g.__lastGraphqlInput = null try { await fetchSnapshot({ owner: 'test', repo: 'test', prNumber: 1, prUrl: 'https://github.com/test/test/pull/1', key: 'test/test#1' }) } catch { /* PR not found is expected */ } assert.ok(g.__lastGraphqlInput, 'githubGraphql was called') assert.equal('account' in g.__lastGraphqlInput!, false, 'no account option in githubGraphql call') assert.ok('query' in g.__lastGraphqlInput!, 'query is present') }) test('githubRequest is called without an account option (rerunJob)', async () => { const { rerunJob } = await import('./github-port.ts') g.__lastRequestInput = null try { await rerunJob({ owner: 'test', repo: 'test', prNumber: 1, prUrl: 'https://github.com/test/test/pull/1', key: 'test/test#1' }, 123) } catch { /* expected */ } assert.ok(g.__lastRequestInput, 'githubRequest was called') assert.equal('account' in g.__lastRequestInput!, false, 'no account option in githubRequest call') }) test('githubRequest is called without an account option (replyToReviewComment)', async () => { const { replyToReviewComment } = await import('./github-port.ts') g.__lastRequestInput = null try { await replyToReviewComment({ owner: 'test', repo: 'test', prNumber: 1, prUrl: 'https://github.com/test/test/pull/1', key: 'test/test#1' }, 42, 'test body') } catch { /* expected */ } assert.ok(g.__lastRequestInput, 'githubRequest was called') assert.equal('account' in g.__lastRequestInput!, false, 'no account option in githubRequest call') }) test('live GitHub writes pass confirm: true', async () => { const { rerunJob, replyToReviewComment, replyToHumanItem, resolveReviewThread } = await import('./github-port.ts') const locator = { owner: 'test', repo: 'test', prNumber: 1, prUrl: 'https://github.com/test/test/pull/1', key: 'test/test#1' } g.__lastRequestInput = null await rerunJob(locator, 123) assert.equal(g.__lastRequestInput?.confirm, true, 'rerunJob') g.__lastRequestInput = null await replyToReviewComment(locator, 42, 'test body') assert.equal(g.__lastRequestInput?.confirm, true, 'replyToReviewComment') g.__lastRequestInput = null await replyToHumanItem(locator, 'comment:5', null, 'test body') assert.equal(g.__lastRequestInput?.confirm, true, 'replyToHumanItem') g.__lastGraphqlInput = null await resolveReviewThread('PRRT_1') assert.equal(g.__lastGraphqlInput?.confirm, true, 'resolveReviewThread') })