|
| 1 | +import { createTempConfig } from '../../__tests__/helpers/temp-config'; |
| 2 | +import { resolveAuditFilePath } from '../config'; |
| 3 | +import { FileSystemSink } from '../sinks/filesystem-sink'; |
| 4 | +import { readFile } from 'fs/promises'; |
| 5 | +import { join } from 'node:path'; |
| 6 | +import { afterAll, beforeEach, describe, expect, it } from 'vitest'; |
| 7 | + |
| 8 | +const tmp = createTempConfig('fs-sink'); |
| 9 | +const outputDir = join(tmp.configDir, 'telemetry'); |
| 10 | + |
| 11 | +function createSink(opts: { dir?: string; log?: (msg: string) => void } = {}) { |
| 12 | + const filePath = join(opts.dir ?? outputDir, 'test-session.json'); |
| 13 | + return new FileSystemSink({ filePath, log: opts.log }); |
| 14 | +} |
| 15 | + |
| 16 | +function readJsonl(path: string): Promise<unknown[]> { |
| 17 | + return readFile(path, 'utf-8').then(data => |
| 18 | + data |
| 19 | + .trim() |
| 20 | + .split('\n') |
| 21 | + .map(line => JSON.parse(line)) |
| 22 | + ); |
| 23 | +} |
| 24 | + |
| 25 | +describe('FileSystemSink', () => { |
| 26 | + beforeEach(() => tmp.setup()); |
| 27 | + afterAll(() => tmp.cleanup()); |
| 28 | + |
| 29 | + it('writes each record as a JSONL line on disk', async () => { |
| 30 | + const sink = createSink(); |
| 31 | + sink.record(42, { command_group: 'deploy', command: 'deploy', exit_reason: 'success' }); |
| 32 | + await sink.flush(); |
| 33 | + |
| 34 | + const entries = await readJsonl(join(outputDir, 'test-session.json')); |
| 35 | + expect(entries).toHaveLength(1); |
| 36 | + expect(entries[0]).toMatchObject({ |
| 37 | + value: 42, |
| 38 | + attrs: { command_group: 'deploy', command: 'deploy', exit_reason: 'success' }, |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + it('appends multiple records as separate lines', async () => { |
| 43 | + const sink = createSink(); |
| 44 | + sink.record(10, { command_group: 'add', command: 'add.agent' }); |
| 45 | + sink.record(20, { command_group: 'add', command: 'add.memory' }); |
| 46 | + await sink.flush(); |
| 47 | + |
| 48 | + const entries = await readJsonl(join(outputDir, 'test-session.json')); |
| 49 | + expect(entries).toHaveLength(2); |
| 50 | + expect(entries[0]).toMatchObject({ value: 10 }); |
| 51 | + expect(entries[1]).toMatchObject({ value: 20 }); |
| 52 | + }); |
| 53 | + |
| 54 | + it('creates output directory if it does not exist', async () => { |
| 55 | + const nested = join(tmp.testDir, 'deep', 'nested', 'telemetry'); |
| 56 | + const filePath = join(nested, 'test.json'); |
| 57 | + const sink = new FileSystemSink({ filePath }); |
| 58 | + sink.record(1, { command_group: 'status', command: 'status' }); |
| 59 | + await sink.flush(); |
| 60 | + |
| 61 | + const entries = await readJsonl(filePath); |
| 62 | + expect(entries).toHaveLength(1); |
| 63 | + }); |
| 64 | + |
| 65 | + it('flush is a no-op when no records exist', async () => { |
| 66 | + const sink = createSink(); |
| 67 | + await expect(sink.flush()).resolves.toBeUndefined(); |
| 68 | + }); |
| 69 | + |
| 70 | + it('shutdown logs audit message when records were written', async () => { |
| 71 | + const logged: string[] = []; |
| 72 | + const sink = createSink({ log: msg => logged.push(msg) }); |
| 73 | + sink.record(99, { command_group: 'invoke', command: 'invoke' }); |
| 74 | + await sink.shutdown(); |
| 75 | + |
| 76 | + expect(logged).toHaveLength(1); |
| 77 | + expect(logged[0]).toContain('[audit mode]'); |
| 78 | + expect(logged[0]).toContain('test-session.json'); |
| 79 | + }); |
| 80 | + |
| 81 | + it('shutdown does not log when no records were written', async () => { |
| 82 | + const logged: string[] = []; |
| 83 | + const sink = createSink({ log: msg => logged.push(msg) }); |
| 84 | + await sink.shutdown(); |
| 85 | + |
| 86 | + expect(logged).toHaveLength(0); |
| 87 | + }); |
| 88 | +}); |
| 89 | + |
| 90 | +describe('resolveAuditFilePath', () => { |
| 91 | + it('joins outputDir, entrypoint, and sessionId into a JSON file path', () => { |
| 92 | + const path = resolveAuditFilePath('/home/user/.agentcore/telemetry', 'deploy', 'abc-123'); |
| 93 | + expect(path).toBe('/home/user/.agentcore/telemetry/deploy-abc-123.json'); |
| 94 | + }); |
| 95 | +}); |
0 commit comments