Logs.utils.test.ts185 lines · main
| 1 | import { describe, expect, test } from 'vitest' |
| 2 | |
| 3 | import type { LogData } from './Logs.types' |
| 4 | import { |
| 5 | buildLogsPrompt, |
| 6 | extractEdgeFunctionName, |
| 7 | formatLogsAsCsv, |
| 8 | formatLogsAsJson, |
| 9 | formatLogsAsMarkdown, |
| 10 | } from './Logs.utils' |
| 11 | |
| 12 | const createLog = (overrides: Partial<LogData> = {}): LogData => ({ |
| 13 | id: 'test-id', |
| 14 | timestamp: 1621323232312, |
| 15 | event_message: 'test message', |
| 16 | ...overrides, |
| 17 | }) |
| 18 | |
| 19 | describe('Logs.utils', () => { |
| 20 | describe('formatLogsAsJson', () => { |
| 21 | test('formats single log as JSON', () => { |
| 22 | const rows: LogData[] = [createLog({ id: '1', event_message: 'test message' })] |
| 23 | const result = formatLogsAsJson(rows) |
| 24 | expect(result).toContain('"id": "1"') |
| 25 | expect(result).toContain('"event_message": "test message"') |
| 26 | }) |
| 27 | |
| 28 | test('formats multiple logs as JSON array', () => { |
| 29 | const rows: LogData[] = [ |
| 30 | createLog({ id: '1', event_message: 'first' }), |
| 31 | createLog({ id: '2', event_message: 'second' }), |
| 32 | ] |
| 33 | const result = formatLogsAsJson(rows) |
| 34 | expect(result).toContain('"id": "1"') |
| 35 | expect(result).toContain('"id": "2"') |
| 36 | }) |
| 37 | }) |
| 38 | |
| 39 | describe('formatLogsAsCsv', () => { |
| 40 | test('returns empty string for empty list', () => { |
| 41 | expect(formatLogsAsCsv([])).toBe('') |
| 42 | }) |
| 43 | |
| 44 | test('formats single row with header line', () => { |
| 45 | const rows: LogData[] = [createLog({ id: '1', event_message: 'hello' })] |
| 46 | const result = formatLogsAsCsv(rows) |
| 47 | const [header, row] = result.split('\r\n') |
| 48 | expect(header.split(',').sort()).toEqual(['event_message', 'id', 'timestamp'].sort()) |
| 49 | expect(row).toContain('1') |
| 50 | expect(row).toContain('hello') |
| 51 | }) |
| 52 | |
| 53 | test('formats multiple rows', () => { |
| 54 | const rows: LogData[] = [ |
| 55 | createLog({ id: '1', event_message: 'first' }), |
| 56 | createLog({ id: '2', event_message: 'second' }), |
| 57 | ] |
| 58 | const lines = formatLogsAsCsv(rows).split('\r\n') |
| 59 | expect(lines).toHaveLength(3) |
| 60 | expect(lines[1]).toContain('first') |
| 61 | expect(lines[2]).toContain('second') |
| 62 | }) |
| 63 | |
| 64 | test('escapes commas, quotes, and newlines per RFC 4180', () => { |
| 65 | const rows: LogData[] = [ |
| 66 | createLog({ |
| 67 | id: 'a,b', |
| 68 | event_message: 'line1\nline2', |
| 69 | }), |
| 70 | createLog({ |
| 71 | id: 'c"d', |
| 72 | event_message: 'has "quotes"', |
| 73 | }), |
| 74 | ] |
| 75 | const result = formatLogsAsCsv(rows) |
| 76 | expect(result).toContain('"a,b"') |
| 77 | expect(result).toContain('"line1\nline2"') |
| 78 | expect(result).toContain('"c""d"') |
| 79 | expect(result).toContain('"has ""quotes"""') |
| 80 | }) |
| 81 | |
| 82 | test('emits columns based on the first row', () => { |
| 83 | const rows: LogData[] = [ |
| 84 | { id: '1', event_message: 'first', timestamp: 1 }, |
| 85 | // Extra `status` key on later rows is dropped because headers |
| 86 | // come from the first row. |
| 87 | { id: '2', event_message: 'second', timestamp: 2, status: '500' } as LogData, |
| 88 | ] |
| 89 | const result = formatLogsAsCsv(rows) |
| 90 | expect(result).not.toContain('500') |
| 91 | expect(result.split('\r\n')[0]).toBe('id,event_message,timestamp') |
| 92 | }) |
| 93 | |
| 94 | test('renders null as the string "null" and undefined as empty', () => { |
| 95 | const rows: LogData[] = [ |
| 96 | { id: '1', event_message: null as unknown as string, timestamp: undefined as any }, |
| 97 | ] |
| 98 | const result = formatLogsAsCsv(rows) |
| 99 | const dataRow = result.split('\r\n')[1] |
| 100 | // Order matches first-row keys: id, event_message, timestamp |
| 101 | expect(dataRow).toBe('1,null,') |
| 102 | }) |
| 103 | }) |
| 104 | |
| 105 | describe('formatLogsAsMarkdown', () => { |
| 106 | test('formats single log with timestamp', () => { |
| 107 | const rows: LogData[] = [ |
| 108 | createLog({ |
| 109 | id: '123', |
| 110 | timestamp: 1621323232312, |
| 111 | event_message: 'Test error', |
| 112 | status: '500', |
| 113 | }), |
| 114 | ] |
| 115 | const result = formatLogsAsMarkdown(rows) |
| 116 | expect(result).toContain('## Log 1') |
| 117 | expect(result).toContain('**Timestamp:**') |
| 118 | expect(result).toContain('**Message:** Test error') |
| 119 | expect(result).toContain('**Details:**') |
| 120 | }) |
| 121 | |
| 122 | test('formats multiple logs with separators', () => { |
| 123 | const rows: LogData[] = [ |
| 124 | createLog({ id: '1', event_message: 'first error' }), |
| 125 | createLog({ id: '2', event_message: 'second error' }), |
| 126 | ] |
| 127 | const result = formatLogsAsMarkdown(rows) |
| 128 | expect(result).toContain('## Log 1') |
| 129 | expect(result).toContain('## Log 2') |
| 130 | expect(result).toContain('---') |
| 131 | }) |
| 132 | }) |
| 133 | |
| 134 | describe('buildLogsPrompt', () => { |
| 135 | test('builds prompt with single log', () => { |
| 136 | const rows: LogData[] = [createLog({ id: '1', event_message: 'error occurred' })] |
| 137 | const result = buildLogsPrompt(rows) |
| 138 | expect(result).toContain('1 Briven log entry') |
| 139 | expect(result).toContain('error occurred') |
| 140 | expect(result).toContain('What do these logs indicate') |
| 141 | }) |
| 142 | |
| 143 | test('builds prompt with multiple logs', () => { |
| 144 | const rows: LogData[] = [ |
| 145 | createLog({ id: '1', event_message: 'error 1' }), |
| 146 | createLog({ id: '2', event_message: 'error 2' }), |
| 147 | ] |
| 148 | const result = buildLogsPrompt(rows) |
| 149 | expect(result).toContain('2 Briven log entries') |
| 150 | }) |
| 151 | |
| 152 | test('handles singular correctly', () => { |
| 153 | const rows: LogData[] = [createLog({ id: '1', event_message: 'single error' })] |
| 154 | const result = buildLogsPrompt(rows) |
| 155 | expect(result).toContain('1 Briven log entry') |
| 156 | }) |
| 157 | }) |
| 158 | |
| 159 | describe('extractEdgeFunctionName', () => { |
| 160 | test('extracts function name from full pathname', () => { |
| 161 | expect(extractEdgeFunctionName('/functions/v1/hello-world-1')).toBe('hello-world-1') |
| 162 | }) |
| 163 | |
| 164 | test('returns empty string for null', () => { |
| 165 | expect(extractEdgeFunctionName(null)).toBe('') |
| 166 | }) |
| 167 | |
| 168 | test('returns empty string for undefined', () => { |
| 169 | expect(extractEdgeFunctionName(undefined)).toBe('') |
| 170 | }) |
| 171 | |
| 172 | test('returns empty string for non-string values', () => { |
| 173 | expect(extractEdgeFunctionName(42)).toBe('') |
| 174 | expect(extractEdgeFunctionName({})).toBe('') |
| 175 | }) |
| 176 | |
| 177 | test('handles pathname with no slashes', () => { |
| 178 | expect(extractEdgeFunctionName('my-function')).toBe('my-function') |
| 179 | }) |
| 180 | |
| 181 | test('handles trailing slash', () => { |
| 182 | expect(extractEdgeFunctionName('/functions/v1/hello-world-1/')).toBe('hello-world-1') |
| 183 | }) |
| 184 | }) |
| 185 | }) |