|
| 1 | +import { InvokeLogger } from '../invoke-logger.js'; |
| 2 | +import { mkdtempSync, readFileSync, rmSync } from 'node:fs'; |
| 3 | +import { tmpdir } from 'node:os'; |
| 4 | +import path from 'node:path'; |
| 5 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 6 | + |
| 7 | +vi.mock('../../../lib', async importOriginal => { |
| 8 | + const actual = await importOriginal<typeof import('../../../lib')>(); |
| 9 | + return { |
| 10 | + ...actual, |
| 11 | + findConfigRoot: () => tempDir, |
| 12 | + }; |
| 13 | +}); |
| 14 | + |
| 15 | +let tempDir: string; |
| 16 | + |
| 17 | +beforeEach(() => { |
| 18 | + tempDir = mkdtempSync(path.join(tmpdir(), 'invoke-logger-test-')); |
| 19 | +}); |
| 20 | + |
| 21 | +afterEach(() => { |
| 22 | + rmSync(tempDir, { recursive: true, force: true }); |
| 23 | +}); |
| 24 | + |
| 25 | +function readLog(logger: InvokeLogger): string { |
| 26 | + return readFileSync(logger.logFilePath, 'utf-8'); |
| 27 | +} |
| 28 | + |
| 29 | +describe('InvokeLogger session ID', () => { |
| 30 | + it('writes session ID in header when provided via constructor', () => { |
| 31 | + const logger = new InvokeLogger({ |
| 32 | + agentName: 'testAgent', |
| 33 | + runtimeArn: 'arn:aws:bedrock-agentcore:us-east-1:123456:runtime/test', |
| 34 | + region: 'us-east-1', |
| 35 | + sessionId: 'my-session-123', |
| 36 | + }); |
| 37 | + |
| 38 | + const content = readLog(logger); |
| 39 | + expect(content).toContain('Session ID: my-session-123'); |
| 40 | + expect(content).not.toContain('Session ID: none'); |
| 41 | + }); |
| 42 | + |
| 43 | + it('writes "none" when session ID is not provided', () => { |
| 44 | + const logger = new InvokeLogger({ |
| 45 | + agentName: 'testAgent', |
| 46 | + runtimeArn: 'arn:aws:bedrock-agentcore:us-east-1:123456:runtime/test', |
| 47 | + region: 'us-east-1', |
| 48 | + }); |
| 49 | + |
| 50 | + const content = readLog(logger); |
| 51 | + expect(content).toContain('Session ID: none'); |
| 52 | + }); |
| 53 | + |
| 54 | + it('includes session ID in logPrompt output when passed as argument', () => { |
| 55 | + const logger = new InvokeLogger({ |
| 56 | + agentName: 'testAgent', |
| 57 | + runtimeArn: 'arn:aws:bedrock-agentcore:us-east-1:123456:runtime/test', |
| 58 | + region: 'us-east-1', |
| 59 | + sessionId: 'my-session-456', |
| 60 | + }); |
| 61 | + |
| 62 | + logger.logPrompt('hello world', 'my-session-456', 'user-1'); |
| 63 | + |
| 64 | + const content = readLog(logger); |
| 65 | + expect(content).toContain('Session: my-session-456'); |
| 66 | + expect(content).toContain('"sessionId": "my-session-456"'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('logPrompt falls back to constructor sessionId when argument is undefined', () => { |
| 70 | + const logger = new InvokeLogger({ |
| 71 | + agentName: 'testAgent', |
| 72 | + runtimeArn: 'arn:aws:bedrock-agentcore:us-east-1:123456:runtime/test', |
| 73 | + region: 'us-east-1', |
| 74 | + sessionId: 'constructor-session', |
| 75 | + }); |
| 76 | + |
| 77 | + logger.logPrompt('hello world', undefined, 'user-1'); |
| 78 | + |
| 79 | + const content = readLog(logger); |
| 80 | + expect(content).toContain('Session: constructor-session'); |
| 81 | + expect(content).toContain('"sessionId": "constructor-session"'); |
| 82 | + }); |
| 83 | + |
| 84 | + it('logPrompt shows "none" when no session ID anywhere', () => { |
| 85 | + const logger = new InvokeLogger({ |
| 86 | + agentName: 'testAgent', |
| 87 | + runtimeArn: 'arn:aws:bedrock-agentcore:us-east-1:123456:runtime/test', |
| 88 | + region: 'us-east-1', |
| 89 | + }); |
| 90 | + |
| 91 | + logger.logPrompt('hello world'); |
| 92 | + |
| 93 | + const content = readLog(logger); |
| 94 | + expect(content).toContain('Session: none'); |
| 95 | + }); |
| 96 | +}); |
0 commit comments