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