fix(tools): preserve literal and realm-safe schemas

This commit is contained in:
Tianyi Cui
2026-07-21 21:37:11 +08:00
parent 0feeecbfbd
commit acc628f3af
8 changed files with 72 additions and 12 deletions

View File

@@ -1,5 +1,6 @@
import { runInNewContext } from 'node:vm'
import { describe, expect, it } from 'vitest'
import { isJsonValue, snapshotJsonValue } from '@deepseek-ai/dsh-session'
import { isJsonValue, snapshotJsonValue, type JsonValue } from '@deepseek-ai/dsh-session'
describe('snapshotJsonValue', () => {
it('copies the complete JSON scalar vocabulary and rejects unsupported scalars', () => {
@@ -36,6 +37,22 @@ describe('snapshotJsonValue', () => {
expect(Object.getPrototypeOf(snapshot.list[0])).toBe(Object.prototype)
})
it('accepts intrinsic plain containers from another JavaScript realm', () => {
const foreign = runInNewContext('({ object: { nested: [1] }, array: [2, { ok: true }] })') as {
object: { nested: number[] }
array: JsonValue[]
}
expect(isJsonValue(foreign.object)).toBe(true)
expect(isJsonValue(foreign.array)).toBe(true)
const objectSnapshot = snapshotJsonValue(foreign.object)!
const arraySnapshot = snapshotJsonValue(foreign.array)!
expect(objectSnapshot).toEqual({ nested: [1] })
expect(arraySnapshot).toEqual([2, { ok: true }])
expect(Object.getPrototypeOf(objectSnapshot)).toBe(Object.prototype)
expect(Object.getPrototypeOf(arraySnapshot)).toBe(Array.prototype)
})
it('reads each object value and array slot once while materializing', () => {
class Exotic {
readonly accepted = false
@@ -77,10 +94,17 @@ describe('snapshotJsonValue', () => {
Object.defineProperty(symbolDecorated, Symbol('extra'), { value: true })
const cyclic: Record<string, unknown> = {}
cyclic.self = cyclic
const foreignExotics = runInNewContext(`(() => {
class Box { constructor() { this.value = 1 } }
class List extends Array {}
return [new Box(), new List(1)]
})()`) as [object, unknown[]]
expect(snapshotJsonValue(new ExoticObject())).toBeUndefined()
expect(snapshotJsonValue(new Map([['value', 1]]))).toBeUndefined()
expect(snapshotJsonValue(new ExoticArray(1))).toBeUndefined()
expect(snapshotJsonValue(foreignExotics[0])).toBeUndefined()
expect(snapshotJsonValue(foreignExotics[1])).toBeUndefined()
expect(snapshotJsonValue(sparse)).toBeUndefined()
expect(snapshotJsonValue(compensatedSparse)).toBeUndefined()
expect(snapshotJsonValue(decorated)).toBeUndefined()