import { runInNewContext } from 'node:vm' import { inspect as nodeInspect } from 'node:util' import { describe, expect, it, vi } from 'vitest' import { LogBuffer, makeBindingErrorClass, makeInspect, makeNamespaces, captureStreamWrites, prepareCompletion, prepareException } from '../src/bootstrap.ts' import type { BindingErrorConstructor, ImageInspectOptions, PatchableStream } from '../src/bootstrap.ts' import type { HostMethods } from '../src/protocol.ts' import { encodePtcJsonWire, snapshotPtcJsonValue } from '../src/json-wire.ts' import type { BindingInspection } from '../src/bootstrap.ts' function host(): Pick { return { invoke: vi.fn(async () => ({ value: encodePtcJsonWire(42) })) } } const TOOL_ERROR_CLASS = { name: 'ToolCallError', memberNameProperty: 'toolName' } as const function toolNamespace(names: string[]) { return { global: 'tools', names, errorClass: TOOL_ERROR_CLASS } } function inspectedImage(collectImage: ReturnType['collectImage'], path: string, width = 8, height = 4) { const image = { path, width, height } Object.defineProperty(image, nodeInspect.custom, { value(this: object, _depth: number | null, options: ImageInspectOptions): object { collectImage(this, options) return this }, }) return Object.freeze(image) } describe('LogBuffer', () => { it('streams entries to the sink until the byte budget, then emits one fitting prefix and reports the limit once', () => { const seen: string[] = [] let limits = 0 const buffer = new LogBuffer(15, text => seen.push(text), () => { limits += 1 }) buffer.push('12345') buffer.push('123456') buffer.push('dropped') expect(seen).toEqual(['12345', '123']) expect(limits).toBe(1) expect(buffer.overflowed).toBe(true) const exactlyFull: string[] = [] const fullBuffer = new LogBuffer(6, text => exactlyFull.push(text)) fullBuffer.push('12') fullBuffer.push('no-prefix-fits') expect(exactlyFull).toEqual(['12']) }) }) describe('makeInspect', () => { it('includes its emitted newline in output accounting', () => { const text: string[] = [] const limit = vi.fn() makeInspect(new LogBuffer(7, value => text.push(value), limit), new WeakMap(), () => {}).inspect('12') expect(text).toEqual(['12']) expect(limit).toHaveBeenCalledOnce() const exact: string[] = [] makeInspect(new LogBuffer(8, value => exact.push(value), limit), new WeakMap(), () => {}).inspect('12') expect(exact).toEqual(['12\n']) expect(limit).toHaveBeenCalledOnce() }) it('renders inspected arguments into captured output', () => { const seen: string[] = [] const { inspect } = makeInspect(new LogBuffer(1_000, text => seen.push(text)), new WeakMap(), () => {}) inspect('plain', { a: 1 }) expect(seen).toEqual(['plain { a: 1 }\n']) }) it.each([ ['Buffer', Buffer.from([1, 2, 3]), ''], ['TypedArray', new Uint8Array([1, 2, 3]), 'Uint8Array(3) [ 1, 2, 3 ]'], ['function', () => 1, '[Function'], ['Error', new Error('inspection failure'), 'Error: inspection failure'], ['bigint', 1n, '1n'], ['non-finite number', Infinity, 'Infinity'], ['negative zero', -0, '-0'], ['undefined property', { value: undefined }, '{ value: undefined }'], ])('displays a %s argument and continues execution', (_name, value, expected) => { const text: string[] = [] const { inspect } = makeInspect({ overflowed: false, push: line => text.push(line) }, new WeakMap(), () => {}) inspect('before', value, 'after') expect(text).toHaveLength(1) expect(text[0]).toContain(expected) expect(text[0]).toMatch(/^before .* after\n$/s) }) it('displays revoked proxies and accessors without invoking them', () => { const revoked = Proxy.revocable({}, {}) revoked.revoke() const text: string[] = [] const { inspect } = makeInspect({ overflowed: false, push: line => text.push(line) }, new WeakMap(), () => {}) inspect(revoked.proxy, { get value() { throw new Error('getter') } }) expect(text).toEqual([' { value: [Getter] }\n']) }) it('renders complete arrays, long strings and deeply nested objects', () => { const value = { lines: Array.from({ length: 330 }, (_, index) => ({ number: index + 1, text: `line ${index + 1}` })), text: 'x'.repeat(12_000), nested: { a: { b: { c: { d: { e: { answer: 42 } } } } } }, } const text: string[] = [] makeInspect(new LogBuffer(100_000, line => text.push(line)), new WeakMap(), () => {}).inspect(value) expect(text).toHaveLength(1) expect(runInNewContext(`(${text[0]!})`)).toEqual(value) }) it('preserves preceding arguments when a custom formatter throws', () => { const value = { [Symbol.for('nodejs.util.inspect.custom')]() { throw new Error('formatter failed') } } const text: string[] = [] const { inspect } = makeInspect(new LogBuffer(1_000, line => text.push(line)), new WeakMap(), () => {}) expect(() => { inspect('before', value, 'after') }).toThrow('formatter failed') expect(text).toEqual(['before\n']) }) it('emits registered images in argument order from arrays and objects', () => { const images = new WeakMap() const text: string[] = [] const selected: string[] = [] const { inspect, collectImage } = makeInspect({ overflowed: false, push: line => text.push(line) }, images, id => selected.push(id)) const one = inspectedImage(collectImage, '/one.png') const two = inspectedImage(collectImage, '/two.png', 4, 8) images.set(one, 'one').set(two, 'two') const value = { first: [one, { second: two }] } inspect('before', value, 'after', one) expect(selected).toEqual(['one', 'two', 'one']) expect(runInNewContext(`(${text[0]!.slice('before '.length)})`)).toEqual(value) expect(text[1]).toBe('after\n') inspect({ path: '/fake.png', width: 1, height: 1 }, null) inspect() expect(selected).toEqual(['one', 'two', 'one']) expect(text.slice(2)).toEqual(["{ path: '/fake.png', width: 1, height: 1 } null\n", '\n']) }) it('does not select images behind getters', () => { const images = new WeakMap() const text: string[] = [] const selected: string[] = [] const { inspect, collectImage } = makeInspect({ overflowed: false, push: line => text.push(line) }, images, id => selected.push(id)) const image = inspectedImage(collectImage, '/image.png') images.set(image, 'image') const read = vi.fn(() => image) inspect({ get image() { return read() } }) expect(read).not.toHaveBeenCalled() expect(text).toEqual(['{ image: [Getter] }\n']) expect(selected).toEqual([]) }) it('does not select images hidden by a custom formatter', () => { const images = new WeakMap() const text: string[] = [] const selected: string[] = [] const { inspect, collectImage } = makeInspect({ overflowed: false, push: line => text.push(line) }, images, id => selected.push(id)) const image = inspectedImage(collectImage, '/image.png') images.set(image, 'image') inspect({ image, [nodeInspect.custom]() { return 'summary' }, }) expect(text).toEqual(['summary\n']) expect(selected).toEqual([]) }) it('selects registered images returned by a custom formatter', () => { const images = new WeakMap() const selected: string[] = [] const { inspect, collectImage } = makeInspect({ overflowed: false, push() {} }, images, id => selected.push(id)) const image = inspectedImage(collectImage, '/image.png') images.set(image, 'image') inspect({ [nodeInspect.custom]() { return { image } } }) inspect({ image: inspectedImage(collectImage, '/forged.png') }) expect(selected).toEqual(['image']) }) it('keeps collectors separate across reentrant inspection calls', () => { const images = new WeakMap() const selected: string[] = [] const { inspect, collectImage } = makeInspect({ overflowed: false, push() {} }, images, id => selected.push(id)) const one = inspectedImage(collectImage, '/one.png') const two = inspectedImage(collectImage, '/two.png') images.set(one, 'one').set(two, 'two') inspect({ [nodeInspect.custom](_depth: number | null, options: ImageInspectOptions) { nodeInspect({ image: one }, options) inspect({ image: two }) return nodeInspect({ image: one }, options) }, }) expect(selected).toEqual(['two', 'one', 'one']) }) it('discards collected images when their containing value fails to format', () => { const images = new WeakMap() const text: string[] = [] const selected: string[] = [] const { inspect, collectImage } = makeInspect({ overflowed: false, push: line => text.push(line) }, images, id => selected.push(id)) const image = inspectedImage(collectImage, '/image.png') images.set(image, 'image') expect(() => { inspect('before', { image, bad: { [nodeInspect.custom]() { throw new Error('formatter failed') } }, }) }).toThrow('formatter failed') expect(text).toEqual(['before\n']) expect(selected).toEqual([]) inspect({ image }) expect(selected).toEqual(['image']) }) it('selects images from Map entries, Set values and enumerable symbol properties', () => { const images = new WeakMap() const selected: string[] = [] const { inspect, collectImage } = makeInspect({ overflowed: false, push() {} }, images, id => selected.push(id)) const one = inspectedImage(collectImage, '/one.png') const two = inspectedImage(collectImage, '/two.png', 4, 8) images.set(one, 'one').set(two, 'two') const value = { map: new Map([[one, two]]), set: new Set([two, one]), [Symbol('image')]: one, } Object.defineProperty(value, 'hidden', { value: two }) inspect(value) expect(selected).toEqual(['one', 'two', 'two', 'one', 'one']) }) it('reads Map and Set entries without consulting overridden methods', () => { const images = new WeakMap() const selected: string[] = [] const { inspect, collectImage } = makeInspect({ overflowed: false, push() {} }, images, id => selected.push(id)) const image = inspectedImage(collectImage, '/image.png') images.set(image, 'image') const map = new Map([['image', image]]) const set = new Set([image]) Object.defineProperty(map, 'forEach', { get() { throw new Error('map getter') } }) Object.defineProperty(set, 'forEach', { get() { throw new Error('set getter') } }) inspect(map, set) expect(selected).toEqual(['image', 'image']) }) it('finds images in cyclic containers alongside binary views', () => { const images = new WeakMap() const selected: string[] = [] const { inspect, collectImage } = makeInspect({ overflowed: false, push() {} }, images, id => selected.push(id)) const image = inspectedImage(collectImage, '/image.png') images.set(image, 'image') const value: Record = { binary: new Uint8Array([1, 2, 3]), image } value.self = value inspect(value) expect(selected).toEqual(['image']) }) it('displays undefined arguments and cycles', () => { const text: string[] = [] const { inspect } = makeInspect({ overflowed: false, push: line => text.push(line) }, new WeakMap(), () => {}) inspect(undefined) expect(text).toEqual(['undefined\n']) inspect(null) expect(text).toEqual(['undefined\n', 'null\n']) const cyclic: Record = {} cyclic.self = cyclic inspect(cyclic) expect(text.at(-1)).toBe(' { self: [Circular *1] }\n') }) }) describe('binding inspection metadata', () => { it('emits unchanged object and array renderings in order with normalized newlines', () => { const object = { value: 1 } const array = [1, 2] const empty = {} const inspections = new WeakMap([ [object, { snapshot: { value: 1 }, text: 'object rendering' }], [array, { snapshot: [1, 2], text: 'array rendering\n' }], [empty, { snapshot: {}, text: '' }], ]) const seen: string[] = [] const { inspect } = makeInspect({ overflowed: false, push: text => seen.push(text) }, new WeakMap(), () => {}, inspections) inspect('before', object, array, empty, 'after') expect(seen).toEqual(['before\n', 'object rendering\n', 'array rendering\n', 'after\n']) expect(Reflect.ownKeys(object)).toEqual(['value']) }) it('falls back when an Image field is replaced with another identity having the same metadata', () => { const images = new WeakMap() const inspections = new WeakMap() const text: string[] = [] const selected: string[] = [] const { inspect, collectImage } = makeInspect( { overflowed: false, push: value => text.push(value) }, images, id => selected.push(id), inspections, ) const original = inspectedImage(collectImage, '/same.png') const replacement = inspectedImage(collectImage, '/same.png') images.set(original, 'original') images.set(replacement, 'replacement') const value = { shot: original } inspections.set(value, { snapshot: { shot: original }, text: 'original rendering' }) value.shot = replacement inspect(value) expect(text.join('')).not.toContain('original rendering') expect(selected).toEqual(['replacement']) }) it.each([ ['null field', (value: Record) => { value.value = null }], ['scalar field', (value: Record) => { value.value = 'one' }], ['object replacing a scalar', (value: Record) => { value.value = {} }], ['object replacing null', (value: Record) => { value.nullable = {} }], ['proxy', (value: Record) => { value.nested = new Proxy({}, {}) }], ['prototype', (value: Record) => { Object.setPrototypeOf(value.nested, null) }], ['renamed field', (value: Record) => { delete value.value; value.changed = 1 }], ['removed field', (value: Record) => { delete value.value }], ['hidden field', (value: Record) => { Object.defineProperty(value, 'value', { enumerable: false }) }], ] as const)('formats current data after changing a %s', (_name, change) => { const value: Record = { value: 1, nested: { answer: 42 }, nullable: null } const snapshot = snapshotPtcJsonValue(value)! const inspections = new WeakMap([[value, { snapshot, text: 'original rendering' }]]) const seen: string[] = [] const { inspect } = makeInspect({ overflowed: false, push: text => seen.push(text) }, new WeakMap(), () => {}, inspections) change(value) inspect(value) expect(seen.join('')).not.toContain('original rendering') expect(seen).toHaveLength(1) }) it.each([null, 42, [], {}, { value: [undefined] }, { value: encodePtcJsonWire({}), inspection: 42 }])('rejects malformed binding result %j', async (resolution) => { const remote = host() remote.invoke = async () => resolution as never const [tools] = makeNamespaces({ namespaces: [toolNamespace(['x'])] }, remote, new Map(), new WeakMap()) as [Record Promise>] await expect(tools.x?.({})).rejects.toThrow('binding resolution must be lossless JSON') }) it.each([null, 42])('rejects inspection metadata for scalar %j', async (value) => { const remote = host() remote.invoke = async () => ({ value: encodePtcJsonWire(value), inspection: 'text' }) const [tools] = makeNamespaces({ namespaces: [toolNamespace(['x'])] }, remote, new Map(), new WeakMap()) as [Record Promise>] await expect(tools.x?.({})).rejects.toThrow('binding inspection requires an object or array') }) it('rejects inspection metadata for non-JSON image identities', async () => { const remote = host() remote.invoke = async () => ({ value: [{ kind: 'image', id: 'one', attachmentId: 'one', path: '/one.png', width: 1, height: 1 }], inspection: 'text' }) const [tools] = makeNamespaces({ namespaces: [toolNamespace(['x'])] }, remote, new Map(), new WeakMap(), () => Object.create({ image: true }) as Record) as [Record Promise>] await expect(tools.x?.({})).rejects.toThrow('binding inspection requires lossless JSON data') }) it('registers inspection metadata while returning the original JSON value', async () => { const remote = host() remote.invoke = async () => ({ value: encodePtcJsonWire({ answer: 42 }), inspection: 'answer' }) const inspections = new WeakMap() const [tools] = makeNamespaces({ namespaces: [toolNamespace(['x'])] }, remote, new Map(), inspections) as [Record Promise>] const value = await tools.x?.({}) expect(value).toEqual({ answer: 42 }) expect(inspections.get(value as object)).toEqual({ snapshot: { answer: 42 }, text: 'answer' }) }) }) describe('captureStreamWrites', () => { it('redirects writes into the buffer and restores on request', () => { const seen: string[] = [] const buffer = new LogBuffer(1_000, text => seen.push(text)) let underlying = '' const stream: PatchableStream = { write: (chunk: unknown) => { underlying += String(chunk); return true } } const restore = captureStreamWrites(buffer, stream) stream.write('captured', 'utf8') stream.write(Buffer.from('bytes')) restore() stream.write('after') expect(seen).toEqual(['captured', 'bytes']) expect(underlying).toBe('after') }) it('invokes the write callback asynchronously, in both optional-encoding shapes', async () => { const buffer = new LogBuffer(1_000, () => {}) const stream: PatchableStream = { write: () => true } captureStreamWrites(buffer, stream) const calls: (Error | null | undefined)[] = [] stream.write('two-arg', (error?: Error | null) => calls.push(error)) stream.write('three-arg', 'utf8', (error?: Error | null) => calls.push(error)) // Node's contract: the callback fires after the write call returns. expect(calls).toEqual([]) await new Promise(resolve => stream.write('awaited flush', resolve)) expect(calls).toEqual([null, null]) }) it('still fires the callback for a write the exhausted budget drops', async () => { const buffer = new LogBuffer(4, () => {}) const stream: PatchableStream = { write: () => true } captureStreamWrites(buffer, stream) stream.write('this write overflows the budget and is dropped') await new Promise(resolve => stream.write('also dropped', resolve)) }) }) it('skips formatting after output is discarded, including later arguments in one inspect call', () => { const rendered = vi.fn(() => 'must not format') const value = { [nodeInspect.custom]: rendered } const logs = new LogBuffer(4, () => {}) const images = new WeakMap() const image = {} images.set(image, 'image') const { inspect } = makeInspect(logs, images, () => {}) inspect('overflow', image, value) inspect(value) expect(rendered).not.toHaveBeenCalled() }) describe('prepareCompletion', () => { it('omits undefined and passes lossless JSON values exactly', () => { expect(prepareCompletion(undefined, 100)).toEqual({}) expect(prepareCompletion({ a: [1, 'two'] }, 100)).toEqual({ value: encodePtcJsonWire({ a: [1, 'two'] }) }) }) it('turns every lossy completion shape into invalid-output', () => { const cyclic: Record = {} cyclic.self = cyclic const sparse = Array(2) class Exotic { readonly marker = true } for (const value of [{ fn: () => 1 }, -0, Number.POSITIVE_INFINITY, sparse, cyclic, new Exotic()]) { expect(prepareCompletion(value, 1_000)).toEqual({ error: { kind: 'invalid-output', message: 'program completion must be lossless JSON' }, }) } }) it('reports an oversized value instead of substituting rendered text', () => { expect(prepareCompletion('x'.repeat(50), 10)).toEqual({ error: { kind: 'output-limit', message: 'completion value exceeded 10 bytes' }, }) }) it('measures the exact JSON serialization at and over the boundary', () => { expect(prepareCompletion('€', 5)).toEqual({ value: encodePtcJsonWire('€') }) expect(prepareCompletion('€', 4)).toEqual({ error: { kind: 'output-limit', message: 'completion value exceeded 4 bytes' }, }) }) it('contains a getter failure as invalid-output', () => { const value = Object.defineProperty({}, 'x', { enumerable: true, get() { throw new Error('getter exploded') } }) expect(prepareCompletion(value, 1_000)).toEqual({ error: { kind: 'invalid-output', message: 'program completion must be lossless JSON' }, }) }) it('truncates invalid-output diagnostics without changing the failure kind', () => { expect(prepareCompletion(() => 1, 4, 64)).toEqual({ error: { kind: 'invalid-output', message: 'pr' }, outputTruncated: true, }) }) }) describe('prepareException', () => { it.each([ ['const error = new Error("foreign"); error.stack = "foreign stack"; error', 'foreign stack'], ['const error = new Error("bare"); error.stack = undefined; error', 'bare'], ['const error = new Error("hidden"); Object.defineProperty(error, "stack", { get() { throw 1 } }); error', 'program threw an unrenderable value'], ])('formats a cross-realm error through the shared diagnostic path: %s', (source, message) => { const error: unknown = runInNewContext(source) expect(prepareException(error, 1_000)).toEqual({ error: { kind: 'exception', message } }) }) it('passes a fitting diagnostic and truncates one byte over without changing its kind', () => { expect(prepareException('boom', 6)).toEqual({ error: { kind: 'exception', message: 'boom' } }) expect(prepareException('boom', 5)).toEqual({ error: { kind: 'exception', message: 'boo' }, outputTruncated: true, }) }) it('contains a thrown value whose string conversion fails', () => { const thrown = { toString() { throw new Error('cannot render') } } expect(prepareException(thrown, 1_000)).toEqual({ error: { kind: 'exception', message: 'program threw an unrenderable value' }, }) const strangeStack = Object.defineProperty(new Error('ignored'), 'stack', { value: 42 }) expect(prepareException(strangeStack, 1_000)).toEqual({ error: { kind: 'exception', message: '42' }, }) }) }) describe('makeNamespaces', () => { it('rejects malformed remote results and keeps prototype-colliding function names callable', async () => { const remote = host() remote.invoke = vi.fn(async (_global, name) => name === 'bad' ? { value: [undefined] as never } : { value: encodePtcJsonWire(`${name}-ok`) }) const [tools] = makeNamespaces({ namespaces: [{ global: 'tools', names: ['bad', '__proto__', 'constructor', 'toString'] }] }, remote, new Map(), new WeakMap()) as [Record Promise>] expect(Object.getPrototypeOf(tools)).toBeNull() for (const name of ['__proto__', 'constructor', 'toString']) await expect(tools[name]?.({})).resolves.toBe(`${name}-ok`) await expect(tools.bad?.({})).rejects.toThrow('binding resolution must be lossless JSON') }) it('uses the declared constructor for remote and argument failures', async () => { const remote = host() const data = { namespaces: [toolNamespace(['x'])] } const classes = new Map([['tools', makeBindingErrorClass(TOOL_ERROR_CLASS)]]) const [tools] = makeNamespaces(data, remote, classes, new WeakMap()) as [Record Promise>] const decorated = [1] Object.defineProperty(decorated, 'extra', { value: true }) const throwing = Object.defineProperty({}, 'value', { enumerable: true, get() { throw new Error('getter failed') } }) for (const value of [() => 1, new Date(), decorated, throwing]) { await expect(tools.x?.(value)).rejects.toMatchObject({ name: 'ToolCallError', toolName: 'x', message: 'binding arguments must be lossless JSON' }) } expect(vi.mocked(remote.invoke).mock.calls).toEqual([]) for (const failure of [new Error('denied'), 'raw failure']) { remote.invoke = async () => { throw failure } const result = tools.x?.({}) await expect(result).rejects.toBeInstanceOf(classes.get('tools')) await expect(result).rejects.toMatchObject({ name: 'ToolCallError', toolName: 'x', message: failure instanceof Error ? failure.message : failure }) } }) it('uses ordinary Error when the namespace declares no rejection class', async () => { const remote = host() remote.invoke = async () => { throw new Error('helper denied') } const [helpers] = makeNamespaces({ namespaces: [{ global: 'helpers', names: ['x'] }] }, remote, new Map(), new WeakMap()) as [Record Promise>] await expect(helpers.x?.({})).rejects.toMatchObject({ name: 'Error', message: 'helper denied' }) await expect(helpers.x?.(() => 1)).rejects.toThrow('binding arguments must be lossless JSON') }) })