Files
yuj 0791c282bd test(self-update): cover stream-failure cleanup + tighten error teardown
Review feedback on #158 (thanks @NianJiuZst): the error path needs to
tear the writer down BEFORE unlinking dest, otherwise the writer's
buffered bytes can race the unlink and leave a partial file in the
pagecache. And the function still had no test coverage, so the
`finish`/`drain`/cleanup/`releaseLock()` behaviour could regress
silently.

Changes:

* `downloadFile`: on error, `writer.destroy()` and await `'close'`
  before calling `unlinkSync(dest)`. Skip the wait when the writer
  is already torn down so the rejection's exception isn't masked.
* Export `downloadFile` from `src/update/self-update.ts` so unit
  tests can drive it directly (still not part of the public CLI
  surface — callers go through `applySelfUpdate`).
* Add `test/update/self-update.test.ts` with three tests, matching
  the existing `test/files/download.test.ts` style:
    - mid-stream `ReadableStream` error path: rejects, dest gone,
      tempdir empty (the case the reviewer specifically asked for)
    - happy path: payload written, dest exists
    - 4xx response: rejects with CLIError, dest not created
  `npx tsc --noEmit` passes.
2026-05-22 10:48:02 +08:00
..