import { expect, test } from 'vitest' import { createExecutionSecretRedactor, secretValueForms, trackSecretValueForms, weakSecretRedactionNote, } from './execution-secret-redactor.ts' const apiToken = `kody_at_${'a1'.repeat(10)}_${'Z'.repeat(43)}` const bootstrapCode = `kody_bc_${'a'.repeat(16)}_${'B'.repeat(32)}` test('redacts Kody credentials in errors without tracked secrets and still redacts tracked values', () => { const redactor = createExecutionSecretRedactor() expect( redactor.redactErrorMessage(`token=${apiToken} bootstrap=${bootstrapCode}`), ).toBe('token=kody_at_[redacted] bootstrap=kody_bc_[redacted]') expect( redactor.redactUnknown({ nested: [{ token: apiToken }], count: 1 }), ).toEqual({ nested: [{ token: 'kody_at_[redacted]' }], count: 1 }) const untouched = { ok: true } expect(redactor.redactUnknown(untouched)).toBe(untouched) const redacted = redactor.redactUnknown({ error: new Error(`fetch failed with ${apiToken}`), }) as { error: unknown } expect(JSON.stringify(redacted)).not.toContain(apiToken) const direct = redactor.redactUnknown(new Error(`bad ${apiToken}`)) expect(direct instanceof Error ? direct.message : String(direct)).toBe( 'bad kody_at_[redacted]', ) redactor.track('hunter2') expect(redactor.redactUnknown(`hunter2 ${apiToken} ${bootstrapCode}`)).toBe( `[REDACTED SECRET] kody_at_[redacted] ${bootstrapCode}`, ) }) test('leaves bootstrap codes intact in ordinary result redaction', () => { const redactor = createExecutionSecretRedactor() expect(redactor.redactUnknown({ bootstrapCode })).toEqual({ bootstrapCode }) }) test('secretValueForms tracks the exact, URL-encoded, and base64 forms', () => { const redactor = createExecutionSecretRedactor() trackSecretValueForms((value) => redactor.track(value), 'a+b/c=') expect(secretValueForms('a+b/c=')).toEqual([ 'a+b/c=', 'a%2Bb%2Fc%3D', btoa('a+b/c='), ]) expect( redactor.redactErrorMessage(`a+b/c= a%2Bb%2Fc%3D ${btoa('a+b/c=')}`), ).toBe('[REDACTED SECRET] [REDACTED SECRET] [REDACTED SECRET]') expect(secretValueForms('')).toEqual([]) }) test('a short secret is redacted from run output only where it stands alone', () => { const redactor = createExecutionSecretRedactor() redactor.track('4821') const result = { id: 'run_9482173', created_at: '2026-11-28T14:48:21Z', count: 4821, pin: '4821', note: 'door code 4821, try again', } expect(redactor.weakRedactionNote()).toBeNull() expect(redactor.redactUnknown(result)).toEqual({ id: 'run_9482173', created_at: '2026-11-28T14:48:21Z', count: 4821, pin: '[REDACTED SECRET]', note: 'door code [REDACTED SECRET], try again', }) expect(redactor.weakRedactionNote()).toBe(weakSecretRedactionNote) expect(redactor.redactLogMessage('order 148210 used pin=4821')).toBe( 'order 148210 used pin=[REDACTED SECRET]', ) // Errors are not parsed, so the value goes wherever it appears. expect(redactor.redactErrorMessage('GET /pins/4821x failed')).toBe( 'GET /pins/[REDACTED SECRET]x failed', ) }) test('a short secret in JSON text output is redacted only inside strings', () => { const redactor = createExecutionSecretRedactor() redactor.track('4821') const text = JSON.stringify({ total: 4821, pin: '4821', id: 'a4821b' }) const redacted = redactor.redactUnknown(text) expect(JSON.parse(String(redacted))).toEqual({ total: 4821, pin: '[REDACTED SECRET]', id: 'a4821b', }) }) test('a strong secret is still redacted inside longer run output values', () => { const redactor = createExecutionSecretRedactor() const secret = 'sk_live_9fQ2xLm7Zp' redactor.track(secret) expect( redactor.redactUnknown({ url: `https://x.test/a${secret}b`, n: 1 }), ).toEqual({ url: 'https://x.test/a[REDACTED SECRET]b', n: 1 }) expect(redactor.weakRedactionNote()).toBeNull() }) test('a JSON log line with a level prefix keeps its numbers', () => { const redactor = createExecutionSecretRedactor() redactor.track('4821') expect( redactor.redactLogMessage('[warn] {"total": 4821, "pin": "4821"}'), ).toBe('[warn] {"total": 4821, "pin": "[REDACTED SECRET]"}') }) test('a short secret masked as part of a strong one leaves no note, and a byte-order mark stays', () => { const redactor = createExecutionSecretRedactor() const strong = 'sk_live_4821_9fQ2xLm7Zp' redactor.track('4821') redactor.track(strong) expect(redactor.redactUnknown(`\uFEFFname,key\nkody,${strong}`)).toBe( '\uFEFFname,key\nkody,[REDACTED SECRET]', ) expect(redactor.weakRedactionNote()).toBeNull() })