import '../test/kody-mocks.mjs' import assert from 'node:assert/strict' import { afterEach, beforeEach, describe, it } from 'node:test' // @ts-expect-error plain JS test helper import { createStorageMock } from '../test/storage-mock.mjs' const PATCH_ID = 'a2600d4e-d242-4709-8a9f-f756ad2d7e82' const HOST = 'api2.cursor.sh' let mock = createStorageMock() const listedSecrets: Array> = [] const mocks = (globalThis as any).__kodyMocks mocks['kody:runtime'] = { packageStorage: () => mock.storage, packageContext: undefined, kody: { secretList: async () => listedSecrets, }, } mocks['kody:@kentcdodds/quiet-hours/status'] = { default: async () => ({ quiet: false }), } const { friendlyWakeKeyName, isWakeSenderSecretName, resolvedWakeSecretName, secretNameFor, secretPlaceholder, secretUseStatusFrom, wakeSecretMatches, writeBotRecord, } = await import('./registry.ts') const { default: wake } = await import('./wake.ts') const posted: Array<{ authorization: string; automationKey: string }> = [] const originalFetch = globalThis.fetch describe('friendly wake key names', () => { it('builds GrokBotWakeKey without hardcoding bot names', () => { assert.equal(friendlyWakeKeyName('Patch'), 'patchGrokBotWakeKey') assert.equal(friendlyWakeKeyName('kody'), 'kodyGrokBotWakeKey') assert.equal(friendlyWakeKeyName('ship-pr'), 'shipPrGrokBotWakeKey') assert.equal(friendlyWakeKeyName('Kody (chief of staff)'), 'kodyChiefOfStaffGrokBotWakeKey') assert.equal(friendlyWakeKeyName(' '), '') }) it('recognizes legacy and preferred vault names', () => { assert.equal(isWakeSenderSecretName(`grokBotWake.${PATCH_ID}`), true) assert.equal(isWakeSenderSecretName('patchGrokBotWakeKey'), true) assert.equal(isWakeSenderSecretName('grokBotWakeWebhookUrl'), false) }) it('matches a renamed key by friendly name or by agent id in the description', () => { const bot = { id: PATCH_ID, name: 'Patch' } const renamed = { name: 'patchGrokBotWakeKey', scope: 'package', package_id: 'pkg', allowed_hosts: [HOST], description: `Grok Bot wake sender key for Patch. Agent id: ${PATCH_ID}.`, updated_at: null, } const custom = { name: 'someOtherWakeKey', scope: 'package', package_id: 'pkg', allowed_hosts: [HOST], description: `Wake sender key. Agent id: ${PATCH_ID}.`, updated_at: null, } const other = { name: 'coleGrokBotWakeKey', scope: 'package', package_id: 'pkg', allowed_hosts: [HOST], description: 'Grok Bot wake sender key for Cole. Agent id: 6519d644-b6ac-4e61-989c-3d96d0b63cca.', updated_at: null, } assert.equal(wakeSecretMatches(renamed, bot), true) assert.equal(wakeSecretMatches(renamed, `grokBotWake.${PATCH_ID}`), true) assert.equal(wakeSecretMatches(custom, bot), true) assert.equal(wakeSecretMatches(other, bot), false) assert.equal(resolvedWakeSecretName([renamed], bot), 'patchGrokBotWakeKey') const status = secretUseStatusFrom([renamed], bot) assert.equal(status.exists, true) assert.equal(status.secretName, 'patchGrokBotWakeKey') assert.equal(status.needsHostApproval, false) }) it('still stamp-declares only the legacy slot name', () => { assert.equal( secretPlaceholder(`grokBotWake.${PATCH_ID}`), `{{secret:grokBotWake.${PATCH_ID}}}`, ) assert.throws(() => secretPlaceholder('patchGrokBotWakeKey'), /not stamp-declared/) }) }) describe('wake after a vault rename', () => { beforeEach(async () => { mock = createStorageMock() listedSecrets.length = 0 listedSecrets.push({ name: 'patchGrokBotWakeKey', scope: 'package', allowed_hosts: [HOST], allowedHosts: [HOST], description: `Grok Bot wake sender key for Patch. Agent id: ${PATCH_ID}.`, }) posted.length = 0 globalThis.fetch = (async (_url: unknown, init?: RequestInit) => { const headers = new Headers(init?.headers) posted.push({ authorization: headers.get('Authorization') || '', automationKey: headers.get('X-Automation-Key') || '', }) return new Response('{}', { status: 200 }) }) as typeof fetch await writeBotRecord({ id: PATCH_ID, name: 'Patch', aliases: ['patch'], url: 'https://api2.cursor.sh/automations/webhook/test-patch', secretName: secretNameFor(PATCH_ID), createdAt: '2026-01-01T00:00:00.000Z', updatedAt: '2026-01-01T00:00:00.000Z', }) }) afterEach(() => { globalThis.fetch = originalFetch }) it('dryRun reports the clear name and a real wake still uses the sealed slot placeholder', async () => { const preview = await wake({ bot: 'Patch', prompt: 'dryRun after rename', dryRun: true, }) assert.equal(preview.ok, true) assert.equal('dryRun' in preview && preview.dryRun, true) assert.equal('secretName' in preview && preview.secretName, 'patchGrokBotWakeKey') const result = await wake({ bot: 'Patch', prompt: 'connectivity check after rename', }) assert.equal(result.ok, true) assert.equal('woke' in result && result.woke, true) assert.equal(posted.length, 1) assert.equal(posted[0].authorization, `Bearer {{secret:grokBotWake.${PATCH_ID}}}`) assert.equal(posted[0].automationKey, `{{secret:grokBotWake.${PATCH_ID}}}`) }) })