-
Notifications
You must be signed in to change notification settings - Fork 445
[jsweep] Clean write_large_content_to_file.cjs #43312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1161cf7
500d22c
4638dca
bb9a027
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -171,4 +171,61 @@ describe("writeLargeContentToFile", () => { | |
|
|
||
| expect(result.description).toBe("number"); | ||
| }); | ||
|
|
||
| it("should handle JSON boolean primitive", async () => { | ||
| const { writeLargeContentToFile } = await import("./write_large_content_to_file.cjs"); | ||
|
|
||
| const content = JSON.stringify(true); | ||
| const result = writeLargeContentToFile(content); | ||
|
|
||
| expect(result.description).toBe("boolean"); | ||
| }); | ||
|
|
||
| it("should handle JSON string primitive", async () => { | ||
| const { writeLargeContentToFile } = await import("./write_large_content_to_file.cjs"); | ||
|
|
||
| const content = JSON.stringify("hello world"); | ||
| const result = writeLargeContentToFile(content); | ||
|
|
||
| expect(result.description).toBe("string"); | ||
| }); | ||
|
|
||
| it("should handle JSON null primitive", async () => { | ||
| const { writeLargeContentToFile } = await import("./write_large_content_to_file.cjs"); | ||
|
|
||
| const content = JSON.stringify(null); | ||
| const result = writeLargeContentToFile(content); | ||
|
|
||
| expect(result.description).toBe("null"); | ||
| }); | ||
|
|
||
| it("should handle array of primitives", async () => { | ||
| const { writeLargeContentToFile } = await import("./write_large_content_to_file.cjs"); | ||
|
|
||
| const content = JSON.stringify(["a", "b", "c"]); | ||
| const result = writeLargeContentToFile(content); | ||
|
|
||
| expect(result.description).toBe("[string] (3 items)"); | ||
| }); | ||
|
|
||
| it("should truncate description for objects with more than 10 keys", async () => { | ||
| const { writeLargeContentToFile } = await import("./write_large_content_to_file.cjs"); | ||
|
|
||
| const obj = Object.fromEntries(Array.from({ length: 12 }, (_, i) => [`key${i}`, i])); | ||
| const content = JSON.stringify(obj); | ||
| const result = writeLargeContentToFile(content); | ||
|
|
||
| expect(result.description).toBe("{key0, key1, key2, key3, key4, key5, key6, key7, key8, key9, ...} (12 keys)"); | ||
| }); | ||
|
|
||
| it("should return both filename and description fields", async () => { | ||
| const { writeLargeContentToFile } = await import("./write_large_content_to_file.cjs"); | ||
|
|
||
| const content = JSON.stringify({ a: 1 }); | ||
| const result = writeLargeContentToFile(content); | ||
|
|
||
| expect(result).toHaveProperty("filename"); | ||
| expect(result).toHaveProperty("description"); | ||
| expect(Object.keys(result)).toHaveLength(2); | ||
| }); | ||
| }); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Brittle key-order assertion: 💡 Suggested fixUse a set-membership check instead: expect(new Set(Object.keys(result))).toEqual(new Set(['filename', 'description']));
// or
expect(result).toHaveProperty('filename');
expect(result).toHaveProperty('description');
expect(Object.keys(result)).toHaveLength(2);The intent — verify exactly these two keys and no others — is better expressed through the length + membership pattern. V8 preserves integer-indexed then string-insertion order today, but this is an implementation detail rather than a contract.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed in 4638dca by changing this to |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing
nullprimitive coverage: the PR adds boolean/string/array primitive tests but omitsnull, which has a latent bug —typeof null === "object"ingenerateCompactSchemacauses it to hit the object branch but then fall through toreturn${typeof parsed}`` returning"object"instead of `"null"`.💡 Why this matters and suggested fix
JSON.stringify(null)produces the valid JSON string"null". When passed towriteLargeContentToFile,generateCompactSchemaparses it, gets JSnull, and evaluatestypeof parsed === 'object' && parsed !== null— the guard evaluates to false (becausenull !== nullis false), so it falls through toreturn${typeof parsed}``, which returns"object"(since `typeof null === 'object'` in JavaScript). A consumer reading the description would incorrectly infer the payload was an object.Adding this test exposes the bug today:
Fix in
generate_compact_schema.cjs, before the object branch:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in 4638dca: added
nullhandling inactions/setup/js/generate_compact_schema.cjsand added aJSON null primitivetest inactions/setup/js/write_large_content_to_file.test.cjs.