Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

workerd-console-logger.spec.ts

100 lines3.7 KB

1import path from 'node:path'
2import { readFile } from 'node:fs/promises'
3import { fileURLToPath } from 'node:url'
4import { expect, test } from '@playwright/test'
5import { Miniflare } from 'miniflare'
6​
7/**
8 * Regression test for #730: the default debug logger (`ConsoleLogger`)
9 * dropped every `meta` payload on Cloudflare Workers because workerd never
10 * forwards `console.dir` output to the terminal. These tests run the BUILT
11 * logger inside real workerd (via Miniflare, with `nodejs_compat` enabled —
12 * which also emulates `process.versions.node`, the trap that defeats naive
13 * Node detection) and assert the payloads actually reach the runtime's log
14 * stream.
15 *
16 * Policy exception — no aimock: this suite's aimock convention exists to
17 * mock provider LLM responses, but the unit under test here is console
18 * rendering inside the workerd runtime, a code path no provider HTTP ever
19 * reaches. Wiring aimock in would require bundling the full `chat()` +
20 * adapter stack into the Miniflare worker while adding no coverage of the
21 * behavior being fixed; the runtime itself (real workerd) is the thing
22 * being exercised instead.
23 *
24 * Requires `@tanstack/ai` to be built (`pnpm build`), same as every other
25 * spec in this suite.
26 */
27​
28const __dirname = path.dirname(fileURLToPath(import.meta.url))
29const LOGGER_DIST = path.resolve(
30 __dirname,
31 '../../../packages/ai/dist/esm/logger/console-logger.js',
33​
34/**
35 * Build a single-module worker script: the compiled ConsoleLogger source
36 * (self-contained, no imports) plus a fetch handler that exercises it.
37 */
38async function buildWorkerScript(): Promise<string> {
39 const loggerSource = await readFile(LOGGER_DIST, 'utf8')
40 return `${loggerSource}
41export default {
42 fetch() {
43 const logger = new ConsoleLogger()
44 logger.debug('E2E-DEBUG-HEADLINE', {
45 payload: { nested: { deeper: { deepest: { marker: 'E2E-META-DEPTH-5' } } } },
46 })
47 logger.error('E2E-ERROR-HEADLINE', { cause: new Error('E2E-UPSTREAM-401') })
48 const circular = { name: 'E2E-CIRCULAR' }
49 circular.self = circular
50 logger.warn('E2E-WARN-HEADLINE', circular)
51 return new Response('ok')
52 },
56​
57test.describe('ConsoleLogger on real workerd', () => {
58 test('meta payloads reach the log stream at full depth', async () => {
59 const chunks: Array<Buffer> = []
60 const mf = new Miniflare({
61 modules: true,
62 script: await buildWorkerScript(),
63 compatibilityDate: '2025-01-01',
64 compatibilityFlags: ['nodejs_compat'],
65 handleRuntimeStdio(stdout, stderr) {
66 stdout.on('data', (chunk: Buffer) => chunks.push(chunk))
67 stderr.on('data', (chunk: Buffer) => chunks.push(chunk))
68 },
69 })
70 try {
71 const response = await mf.dispatchFetch('http://localhost/')
72 expect(await response.text()).toBe('ok')
73​
74 const output = () => Buffer.concat(chunks).toString('utf8')
75 // Logs flush asynchronously from the workerd process.
76 await expect
77 .poll(output, { timeout: 10_000 })
78 .toContain('E2E-WARN-HEADLINE')
79​
80 // Headlines for every level.
81 expect(output()).toContain('E2E-DEBUG-HEADLINE')
82 expect(output()).toContain('E2E-ERROR-HEADLINE')
83​
84 // The #730 regression: meta must survive, at full depth — workerd's
85 // own inspect would truncate a depth-5 object passed as a console
86 // argument, so this also proves the JSON rendering path was taken.
87 expect(output()).toContain('E2E-META-DEPTH-5')
88​
89 // Error instances serialize meaningfully (plain JSON.stringify of an
90 // Error yields '{}').
91 expect(output()).toContain('E2E-UPSTREAM-401')
92​
93 // Circular meta must not crash or vanish.
94 expect(output()).toContain('E2E-CIRCULAR')
95 expect(output()).toContain('[Circular]')
96 } finally {
97 await mf.dispose()
98 }
99 })
100})