fix(code-runtime): validate arguments before worker dispatch

This commit is contained in:
Tianyi Cui
2026-07-21 18:19:07 +08:00
parent 9e01fb060e
commit 2623ddbee4
8 changed files with 194 additions and 28 deletions
@@ -0,0 +1,18 @@
import { describe, expect, it } from 'vitest'
import { truncateJsonStringBytes } from '../src/output-json.ts'
describe('truncateJsonStringBytes', () => {
it('returns a fitting string whole and rejects budgets without JSON quotes', () => {
expect(truncateJsonStringBytes('fits', 6)).toBe('fits')
expect(truncateJsonStringBytes('x', 1)).toBe('')
})
it('accounts every JSON escape and cuts only between complete code points', () => {
const prefix = '"\\\b\t\n\f\r\u0000😀\ud800€a'
const text = `${prefix}z`
const budget = Buffer.byteLength(JSON.stringify(prefix), 'utf8')
expect(truncateJsonStringBytes(text, budget)).toBe(prefix)
expect(Buffer.byteLength(JSON.stringify(truncateJsonStringBytes(text, budget)), 'utf8')).toBe(budget)
})
})