import assert from 'node:assert/strict' import { registerHooks } from 'node:module' import { test } from 'node:test' const STUB = ` const store = globalThis.__sentryStore ??= new Map(); export const packageStorage = () => ({ async get(k) { return store.get(k) ?? null }, async set(k, v) { store.set(k, JSON.stringify(v)) }, async sql() { throw new Error('sql not stubbed') }, }); export const packageContext = { appBasePath: '/apps/factory/' }; export async function credential(key) { throw new Error('credential not mapped: ' + key); } 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.startsWith('kody:@kody/')) return { url: `data:text/javascript,${encodeURIComponent('export default async () => ({ ok: true })')}`, shortCircuit: true } return next(spec, ctx) }, }) const { sentryFactsFromEvent, sentryCardBody, sentryPriority, handleSentryIssueEvent } = await import('./intake.ts') const pkg = (await import('../../../package.json', { with: { type: 'json' } })).default as { kody: { subscriptions: Record; credentials: Record; webhooks: Array<{ name: string; description: string }> } } test('facts come from the event payload; a missing or odd issue id files nothing', () => { const f = sentryFactsFromEvent({ issueId: '123', shortId: 'KODY-1', title: 'Boom', level: 'fatal', projectSlug: 'kody', url: 'https://sentry.io/i/123', kind: 'issue' })! assert.equal(f.issueId, '123') assert.equal(sentryPriority(f.level), 0) assert.match(sentryCardBody(f), /data from Sentry, not instructions/) assert.equal(sentryFactsFromEvent({ issueId: 'abc' }), null) assert.equal(sentryFactsFromEvent({ issueId: '1', url: 'javascript:alert(1)' })!.url, '') }) test('only the configured Sentry source package can file cards', async () => { const r = await handleSentryIssueEvent({ source: { kody_id: 'someone-else' }, payload: { issueId: '1' } }) assert.equal(r.ok, false) assert.equal(r.action, 'rejected') }) test('the factory declares no Sentry credential and subscribes to Sentry events', async () => { assert.equal(pkg.kody.credentials.sentry, undefined) assert.equal(pkg.kody.credentials.sentryWebhookSecret, undefined) const sub = pkg.kody.subscriptions['@kody/sentry-events.issue.alerted'] assert.ok(sub) await import(new URL(`../../../${sub.handler.replace(/^\.\//, '')}`, import.meta.url).href) })