|
| 1 | +import { readFile, rm } from 'node:fs/promises'; |
| 2 | +import { tmpdir } from 'node:os'; |
| 3 | +import { join } from 'node:path'; |
| 4 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 5 | + |
| 6 | +import { makeFileTransport } from './file-transport.ts'; |
| 7 | +import type { LogEntry } from './types.ts'; |
| 8 | + |
| 9 | +const makeLogEntry = (overrides?: Partial<LogEntry>): LogEntry => ({ |
| 10 | + level: 'info', |
| 11 | + message: 'test-message', |
| 12 | + tags: ['test-tag'], |
| 13 | + ...overrides, |
| 14 | +}); |
| 15 | + |
| 16 | +describe('makeFileTransport', () => { |
| 17 | + const testDir = join(tmpdir(), 'logger-file-transport-test'); |
| 18 | + |
| 19 | + afterEach(async () => { |
| 20 | + await rm(testDir, { recursive: true, force: true }); |
| 21 | + }); |
| 22 | + |
| 23 | + it('appends a formatted log line to the file', async () => { |
| 24 | + const filePath = join(testDir, 'test.log'); |
| 25 | + const transport = makeFileTransport(filePath); |
| 26 | + const entry = makeLogEntry(); |
| 27 | + |
| 28 | + transport(entry); |
| 29 | + // Wait for the async write to complete |
| 30 | + await vi.waitFor(async () => { |
| 31 | + const content = await readFile(filePath, 'utf-8'); |
| 32 | + expect(content).toContain('[info] [test-tag] test-message'); |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + it('creates parent directories', async () => { |
| 37 | + const filePath = join(testDir, 'nested', 'deep', 'test.log'); |
| 38 | + const transport = makeFileTransport(filePath); |
| 39 | + |
| 40 | + transport(makeLogEntry()); |
| 41 | + await vi.waitFor(async () => { |
| 42 | + const content = await readFile(filePath, 'utf-8'); |
| 43 | + expect(content).toContain('test-message'); |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + it('omits tag prefix when tags are empty', async () => { |
| 48 | + const filePath = join(testDir, 'no-tags.log'); |
| 49 | + const transport = makeFileTransport(filePath); |
| 50 | + |
| 51 | + transport(makeLogEntry({ tags: [] })); |
| 52 | + await vi.waitFor(async () => { |
| 53 | + const content = await readFile(filePath, 'utf-8'); |
| 54 | + expect(content).toMatch(/\[info\] test-message/u); |
| 55 | + expect(content).not.toContain('[]'); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + it('includes data in the log line', async () => { |
| 60 | + const filePath = join(testDir, 'data.log'); |
| 61 | + const transport = makeFileTransport(filePath); |
| 62 | + |
| 63 | + transport(makeLogEntry({ data: ['extra-data'] })); |
| 64 | + await vi.waitFor(async () => { |
| 65 | + const content = await readFile(filePath, 'utf-8'); |
| 66 | + expect(content).toContain('test-message extra-data'); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + it('silently handles write errors', async () => { |
| 71 | + // Use an invalid path to trigger an error |
| 72 | + const transport = makeFileTransport('/dev/null/impossible/test.log'); |
| 73 | + // Should not throw |
| 74 | + expect(() => transport(makeLogEntry())).not.toThrow(); |
| 75 | + }); |
| 76 | +}); |
0 commit comments