* fix(draw-image): use pid-based tmp svg and cleanup in finally * test(draw-image): decouple e2e.optional from leftover tmp svg * test(draw-image): add cleanup unit test for tmp svg lifecycle * docs(draw-image): add handoff and ADR for tmp svg race fix * docs(handoff): set PR number * docs(project-map): update after structural changes --------- Co-authored-by: opencode-agent <agent@opencode.local>
59 lines
No EOL
2.4 KiB
TypeScript
59 lines
No EOL
2.4 KiB
TypeScript
import { describe, test, expect, beforeAll, afterAll } from "vitest"
|
|
import { existsSync, readdirSync, rmSync, mkdirSync } from "node:fs"
|
|
import path from "node:path"
|
|
import os from "node:os"
|
|
import { fileURLToPath } from "node:url"
|
|
import { spawnSync } from "node:child_process"
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
|
const DRAW_IMAGE_DIR = path.resolve(__dirname, "..")
|
|
const TMP = "/tmp/draw-image-cleanup"
|
|
|
|
beforeAll(() => {
|
|
if (existsSync(TMP)) rmSync(TMP, { recursive: true, force: true })
|
|
mkdirSync(TMP, { recursive: true })
|
|
})
|
|
|
|
afterAll(() => {
|
|
if (existsSync(TMP)) rmSync(TMP, { recursive: true, force: true })
|
|
})
|
|
|
|
function runCli(args: string[]): { status: number; stdout: string; stderr: string } {
|
|
return spawnSync("node", ["--experimental-strip-types", path.join(DRAW_IMAGE_DIR, "cli.ts"), ...args], {
|
|
encoding: "utf-8",
|
|
cwd: DRAW_IMAGE_DIR,
|
|
})
|
|
}
|
|
|
|
describe("cleanup — temp SVG after render", () => {
|
|
test("no .tmp-render.svg left in draw-image dir after CLI render", () => {
|
|
const out = path.join(TMP, "cleanup.png")
|
|
const leftoverPath = path.join(DRAW_IMAGE_DIR, ".tmp-render.svg")
|
|
if (existsSync(leftoverPath)) rmSync(leftoverPath, { force: true })
|
|
|
|
const r = runCli(["render", "cover", "--title", "Cleanup", "--out", out])
|
|
expect(r.status, `stderr: ${r.stderr}`).toBe(0)
|
|
|
|
expect(existsSync(leftoverPath), ".tmp-render.svg must not remain in package dir").toBe(false)
|
|
})
|
|
|
|
test("unique temp SVG uses process pid under os.tmpdir and is removed", () => {
|
|
const expectedTmp = path.join(os.tmpdir(), `draw-image-${process.pid}.svg`)
|
|
if (existsSync(expectedTmp)) rmSync(expectedTmp, { force: true })
|
|
|
|
const out = path.join(TMP, "pid.png")
|
|
const r = runCli(["render", "cover", "--title", "Pid Check", "--out", out])
|
|
expect(r.status, `stderr: ${r.stderr}`).toBe(0)
|
|
|
|
expect(existsSync(expectedTmp), `temp svg at ${expectedTmp} must be cleaned up`).toBe(false)
|
|
})
|
|
|
|
test("package dir contains no stray .svg files after render", () => {
|
|
const out = path.join(TMP, "no-stray.png")
|
|
const r = runCli(["render", "cover", "--title", "No Stray", "--out", out])
|
|
expect(r.status, `stderr: ${r.stderr}`).toBe(0)
|
|
|
|
const stray = readdirSync(DRAW_IMAGE_DIR).filter((f) => f.startsWith(".tmp") && f.endsWith(".svg"))
|
|
expect(stray, `stray temp svg files: ${stray.join(", ")}`).toEqual([])
|
|
})
|
|
}) |