inspector/clients/web/src/utils/jsonObjectDraft.test.ts
cliffhall 7f5992081e fix: reject non-finite numbers before they reach _meta
`StrictJsonValue` removed `undefined`, but the type cannot express the rest of
the gap: TypeScript has one `number`, and JSON has no encoding for `NaN` or
`±Infinity`, so `JSON.stringify` rewrites them to `null`.

This is reachable from ordinary input, not a careless caller.
`JSON.parse('{"a":1e400}')` returns `Infinity` on overflow — so text a user
types parses cleanly, satisfies the type, and reaches the server as `null`
while the editor still shows `1e400`.

`isSerializableJson` in `core/json` is the runtime half of `StrictJsonValue`,
applied at both boundaries that turn text into metadata:

- the editor's draft parser, which now reports "Numbers must be finite" inline
  and withholds the value rather than emitting one it cannot send;
- the CLI's `--metadata` / `--tool-metadata` parser, which throws. Silently
  sending `null` would be wrong, and so would falling back to the literal
  string — neither is what was asked for.

Tests cover the validator directly (including that `JSON.stringify` really does
write `null`, which is the reason it matters), the editor parser, and the CLI
flag.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0177mPHAdECD18nLLCTwR5rh
Signed-off-by: cliffhall <cliff@futurescale.com>
2026-08-23 19:43:39 -04:00

65 lines
2 KiB
TypeScript

import { describe, it, expect } from "vitest";
import { parseJsonObjectDraft } from "./jsonObjectDraft";
describe("parseJsonObjectDraft", () => {
it("reads empty text as the empty object rather than an error", () => {
expect(parseJsonObjectDraft("")).toEqual({ ok: true, value: {} });
expect(parseJsonObjectDraft(" \n ")).toEqual({ ok: true, value: {} });
});
it("accepts a JSON object with values of any JSON type", () => {
const result = parseJsonObjectDraft(
'{"s":"a","n":1,"b":true,"z":null,"arr":[1,{"k":2}],"o":{"deep":{"er":1}}}',
);
expect(result).toEqual({
ok: true,
value: {
s: "a",
n: 1,
b: true,
z: null,
arr: [1, { k: 2 }],
o: { deep: { er: 1 } },
},
});
});
it.each([
["an overflowing literal", '{"n":1e400}'],
["a negative overflow", '{"n":-1e400}'],
["one nested in an array", '{"a":[1,1e400]}'],
])("rejects %s, which would be sent as null", (_label, text) => {
// `JSON.parse` accepts these; `JSON.stringify` then writes `null`, so the
// editor would show one value and the wire would carry another.
const result = parseJsonObjectDraft(text);
expect(result.ok).toBe(false);
expect(result.ok === false && result.error).toMatch(/finite/);
});
it("still accepts large-but-finite numbers", () => {
expect(parseJsonObjectDraft('{"n":1e308}')).toEqual({
ok: true,
value: { n: 1e308 },
});
});
it("rejects text that is not JSON", () => {
expect(parseJsonObjectDraft('{"a":')).toEqual({
ok: false,
error: "Not valid JSON — changes are not applied",
});
});
it.each([
["an array", "[1,2]"],
["a string", '"hello"'],
["a number", "42"],
["null", "null"],
])("rejects valid JSON that is not an object: %s", (_label, text) => {
const result = parseJsonObjectDraft(text);
expect(result.ok).toBe(false);
expect(result.ok === false && result.error).toMatch(
/Must be a JSON object/,
);
});
});