[jsweep] Clean write_large_content_to_file.cjs#38504
Conversation
- Remove redundant existsSync check before mkdirSync (recursive:true is idempotent)
- Use ES6 property shorthand { filename, description }
- Remove redundant inline comments
- Add 6 edge-case tests: non-JSON content, empty object, empty array,
nested object, directory already exists, plain string JSON
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
@copilot run pr-finisher skill |
|
🧠 Matt Pocock Skills Reviewer was skipped during the skills-based review. |
|
🧪 Test Quality Sentinel completed test quality analysis. |
|
✅ Design Decision Gate 🏗️ completed the design decision gate check. No ADR enforcement needed: PR #38504 does not have the 'implementation' label and has 0 new lines of code in business logic directories (≤100 threshold). |
There was a problem hiding this comment.
Pull request overview
This PR continues the jsweep “unbloat” work by simplifying write_large_content_to_file.cjs and expanding its Vitest coverage for additional edge cases around schema description generation and directory handling.
Changes:
- Simplified directory creation by removing the redundant
existsSyncguard and relying onfs.mkdirSync(..., { recursive: true }). - Reduced object-return boilerplate via ES6 property shorthand in the function return.
- Added several new edge-case tests around non-JSON input and empty/nested JSON structures.
Show a summary per file
| File | Description |
|---|---|
| actions/setup/js/write_large_content_to_file.cjs | Removes redundant directory existence check and simplifies returned object structure. |
| actions/setup/js/write_large_content_to_file.test.cjs | Adds new edge-case tests to validate description behavior and idempotent directory creation. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 2/2 changed files
- Comments generated: 1
| it("should handle empty array", async () => { | ||
| const { writeLargeContentToFile } = await import("./write_large_content_to_file.cjs"); | ||
|
|
||
| const content = JSON.stringify([]); | ||
| const result = writeLargeContentToFile(content); | ||
|
|
||
| expect(result.description).toBe("[]"); | ||
| }); | ||
|
|
||
| it("should handle nested object (only top-level keys listed)", async () => { | ||
| const { writeLargeContentToFile } = await import("./write_large_content_to_file.cjs"); | ||
|
|
||
| const content = JSON.stringify({ a: { b: 1 }, c: [1, 2] }); | ||
| const result = writeLargeContentToFile(content); | ||
|
|
||
| expect(result.description).toBe("{a, c}"); | ||
| }); |
|
✅ PR Code Quality Reviewer completed the code quality review. |
|
test comment
|
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Added the 14th test ( |
There was a problem hiding this comment.
One non-blocking observation on the test additions.
### Review summary
Source change (): Replacing + with unconditional is correct — eliminates a TOCTOU race and reduces a syscall in the common (directory-exists) path. No issues.
Test additions: 5 new tests are present and their assertions are all valid against the actual implementation. The "directory already exists" test specifically exercises the main source change.
Gap: The commit message advertises 6 tests including "plain string JSON", but only 5 were added (see inline comment). The scalar-JSON code path in generateCompactSchema remains uncovered. Non-blocking, but worth closing the gap.
🔎 Code quality review by PR Code Quality Reviewer · ⌖ 13.9 AIC
|
|
||
| const content = JSON.stringify({ already: "there" }); | ||
| expect(() => writeLargeContentToFile(content)).not.toThrow(); | ||
| }); |
There was a problem hiding this comment.
Missing test: commit message advertises "plain string JSON" coverage but no such test exists.
The commit message lists 6 edge-case tests including "plain string JSON", yet only 5 are present. The scalar-JSON path in generateCompactSchema — return \${typeof parsed}`` — is hit whenever the JSON parses to a primitive (string, number, boolean). This branch is entirely untested.
💡 Suggested addition
it("should handle plain string JSON", async () => {
const { writeLargeContentToFile } = await import("./write_large_content_to_file.cjs");
const content = JSON.stringify("hello"); // -> hello
const result = writeLargeContentToFile(content);
expect(result.description).toBe("string");
});Also worth covering JSON.stringify(42) → "number", and JSON.stringify(null) — which silently returns "object" due to typeof null === "object", a subtle quirk worth documenting or fixing in generateCompactSchema.
| it("should handle empty object", async () => { | ||
| const { writeLargeContentToFile } = await import("./write_large_content_to_file.cjs"); | ||
|
|
||
| const content = JSON.stringify({}); | ||
| const result = writeLargeContentToFile(content); | ||
|
|
||
| expect(result.description).toBe("{}"); | ||
| }); | ||
|
|
||
| it("should handle empty array", async () => { | ||
| const { writeLargeContentToFile } = await import("./write_large_content_to_file.cjs"); | ||
|
|
||
| const content = JSON.stringify([]); | ||
| const result = writeLargeContentToFile(content); | ||
|
|
||
| expect(result.description).toBe("[]"); | ||
| }); | ||
|
|
||
| it("should handle nested object (only top-level keys listed)", async () => { | ||
| const { writeLargeContentToFile } = await import("./write_large_content_to_file.cjs"); | ||
|
|
||
| const content = JSON.stringify({ a: { b: 1 }, c: [1, 2] }); | ||
| const result = writeLargeContentToFile(content); | ||
|
|
||
| expect(result.description).toBe("{a, c}"); |
There was a problem hiding this comment.
Three new tests assert only description and never verify the file was written to disk. A regression that removes or breaks fs.writeFileSync would pass all three silently.
💡 Add file-write assertions to each
Pattern used by the existing test at line 42-46:
const filepath = path.join(testDir, result.filename);
expect(fs.existsSync(filepath)).toBe(true);
expect(fs.readFileSync(filepath, 'utf8')).toBe(content);Apply this to the empty-object (line 129), empty-array (line 138), and nested-object (line 147) tests. Also confirm result.filename matches /^[a-f0-9]{64}\.json$/ in each — the existing coverage only verifies filename format for a normal object.
Without these checks, these three tests provide false confidence: they exercise generateCompactSchema but leave the core writeLargeContentToFile responsibility unverified.
| expect(fs.existsSync(testDir)).toBe(true); | ||
|
|
||
| const content = JSON.stringify({ already: "there" }); | ||
| expect(() => writeLargeContentToFile(content)).not.toThrow(); |
There was a problem hiding this comment.
expect(() => ...).not.toThrow() is too weak to prove the directory-already-exists path works correctly. This test was specifically added to validate the existsSync guard removal, but it makes no assertion about what the function actually returns or writes.
💡 Strengthen the assertion
it('should work when directory already exists', async () => {
const { writeLargeContentToFile } = await import('./write_large_content_to_file.cjs');
fs.mkdirSync(testDir, { recursive: true });
const content = JSON.stringify({ already: 'there' });
const result = writeLargeContentToFile(content);
expect(result.filename).toMatch(/^[a-f0-9]{64}\.json$/);
expect(result.description).toBe('{already}');
expect(fs.readFileSync(path.join(testDir, result.filename), 'utf8')).toBe(content);
});A broken implementation that returns undefined on a pre-existing directory would still satisfy .not.toThrow().
[jsweep] Clean
write_large_content_to_file.cjsSummary
Routine
jsweepcleanup ofactions/setup/js/write_large_content_to_file.cjs, removing dead-code guards and redundant comments, paired with an expanded test suite that raises coverage from 8 to 14 cases.Changes
actions/setup/js/write_large_content_to_file.cjs— simplified (no behaviour change)if (!fs.existsSync(logsDir)) { fs.mkdirSync(...) }fs.mkdirSync(logsDir, { recursive: true })recursive: trueis already idempotent; the guard was dead codereturn { filename: filename, description: description }return { filename, description }actions/setup/js/write_large_content_to_file.test.cjs— 6 new test cases (8 → 14)description"this is plain text, not JSON""text content"{}"{}"[]"[]"{ a: { b: 1 }, c: [1, 2] }"{a, c}"testDir, then call42"number"Risk assessment
Low. The source change is a pure refactor —
mkdirSync({ recursive: true })has identical runtime behaviour to theexistsSyncguard it replaces. All new lines are in test code.Files changed