Files
deepseek-harness/packages/experimental/webworker-runtime/tests/node/shim-diff.spec.ts
T
_KermanandTianyi Cui f4a32dbd0a refactor(llm): flatten tool results and validate native V4 sessions
Represent tool results as tool-role messages and migrate released wrappers
without losing outer own JSON fields, call identity, error state, or writer
field order. Refuse nested or wrapper-only information that has no lossless
current representation before successor publication.

Validate current event relationships directly instead of projecting V4 data
back into V3. Keep lifecycle, PTC, title/command, compaction, system-field and
retired-syntax checks on catalog restoration and ordinary JSONL opens. The
Worker JSON equality helper must inspect own keys so it cannot admit PTC
arguments that Node rejects.

Keep the source role change, required native admission and consumer fixture
updates in one complete review layer. Explain shared-type changes in the
persistence record and the native reader rationale in its existing note.
Use checked branded fixture identities and discriminant narrowing instead
of unknown double casts; fixture values and assertions remain intact.

This recomposes the original tool-role work by _Kerman with the independently
validated migration and admission repairs by Tianyi Cui. Frozen generations
and snapshot input version selection are retained.

Co-authored-by: Tianyi Cui <[email protected]>
2026-09-18 19:05:32 +08:00

84 lines
3.8 KiB
TypeScript

/**
* Differential check of the worker's crypto/url/util shims against Node.
*/
import { expect, test } from 'vitest'
import { createHash as nodeCreateHash } from 'node:crypto'
import { fileURLToPath as nodeFileURLToPath, pathToFileURL as nodePathToFileURL } from 'node:url'
import { isDeepStrictEqual as nodeIsDeepStrictEqual, promisify as nodePromisify } from 'node:util'
import * as shimCrypto from '@deepseek-ai/dsh-experimental-webworker-runtime/src/node/builtin_modules/implemented/crypto.ts'
import * as shimUrl from '@deepseek-ai/dsh-experimental-webworker-runtime/src/node/builtin_modules/implemented/url.ts'
import * as shimUtil from '@deepseek-ai/dsh-experimental-webworker-runtime/src/node/builtin_modules/implemented/util.ts'
const compare = (label: string, actual: unknown, expected: unknown): void => {
const [shimmed, node] = [JSON.stringify(actual), JSON.stringify(expected)]
test(label, () => { expect(shimmed).toBe(node) })
}
const INPUTS = ['', 'a', 'hello world', '中文字符串', 'ÿ', 'x'.repeat(100_000)]
for (const algorithm of ['sha1', 'sha256', 'sha512']) {
for (const input of INPUTS) {
compare(
`${algorithm}(${input.slice(0, 12)}… len ${String(input.length)})`,
shimCrypto.createHash(algorithm).update(input).digest('hex'),
nodeCreateHash(algorithm).update(input).digest('hex'),
)
}
// Chained updates must equal the concatenated input.
compare(
`${algorithm} chained update`,
shimCrypto.createHash(algorithm).update('ab').update('cd').digest('hex'),
nodeCreateHash(algorithm).update('abcd').digest('hex'),
)
compare(
`${algorithm} base64`,
shimCrypto.createHash(algorithm).update('abc').digest('base64'),
nodeCreateHash(algorithm).update('abc').digest('base64'),
)
compare(
`${algorithm} bytes`,
[...shimCrypto.createHash(algorithm).update(new Uint8Array([1, 2, 3])).digest()],
[...nodeCreateHash(algorithm).update(new Uint8Array([1, 2, 3])).digest()],
)
}
const PATHS = [
'/dsh/config/cordis.yml', '/a b/c', '/中文/x.md', '/a%b', '/dsh/node_modules/@scope/pkg/lib/index.js',
]
for (const path of PATHS) {
compare(`pathToFileURL(${path})`, shimUrl.pathToFileURL(path).href, nodePathToFileURL(path).href)
compare(
`fileURLToPath(pathToFileURL(${path}))`,
shimUrl.fileURLToPath(shimUrl.pathToFileURL(path)),
nodeFileURLToPath(nodePathToFileURL(path)),
)
}
const PAIRS: [unknown, unknown][] = [
[JSON.parse('{"constructor":{"value":1}}'), JSON.parse('{"constructor":{"value":1}}')],
[JSON.parse('{"constructor":{"value":1}}'), { other: { value: 1 } }],
[JSON.parse('{"__proto__":{}}'), { other: {} }],
[{ other: {} }, JSON.parse('{"__proto__":{}}')],
[JSON.parse('{"__proto__":{}}'), JSON.parse('{"__proto__":{}}')],
[JSON.parse('{"nested":{"__proto__":{}}}'), { nested: { other: {} } }],
[1, 1], [1, '1'], [{ a: 1 }, { a: 1 }], [{ a: 1 }, { a: 2 }], [{ a: 1 }, { a: 1, b: undefined }],
[[1, 2], [1, 2]], [[1, 2], [2, 1]], [null, undefined], [{ a: { b: [1, { c: 2 }] } }, { a: { b: [1, { c: 2 }] } }],
]
for (const [left, right] of PAIRS) {
compare(
`isDeepStrictEqual(${JSON.stringify(left)}, ${JSON.stringify(right)})`,
shimUtil.isDeepStrictEqual(left, right),
nodeIsDeepStrictEqual(left, right),
)
}
const callbackStyle = (value: number, callback: (error: unknown, result?: number) => void): void => {
if (value < 0) callback(new Error('negative'))
else callback(null, value * 2)
}
const shimPromised = shimUtil.promisify(callbackStyle)
const nodePromised = nodePromisify(callbackStyle)
compare('promisify success', await shimPromised(21), await nodePromised(21))
const shimError = await shimPromised(-1).then(() => undefined, (error: unknown) => (error as Error).message)
const nodeError = await nodePromised(-1).then(() => undefined, (error: unknown) => (error as Error).message)
compare('promisify rejection', shimError, nodeError)