fix(media-use): clean failed asset reservations (#2627)

* fix(media-use): clean failed asset reservations

* test(media-use): pin installer guidance exactly
This commit is contained in:
Miguel Ángel
2026-07-18 04:36:36 -04:00
committed by GitHub
parent 7f76170956
commit 49113eb084
8 changed files with 306 additions and 95 deletions
+42 -1
View File
@@ -1,5 +1,13 @@
import { strict as assert } from "node:assert";
import { mkdtempSync, rmSync, readFileSync, writeFileSync, mkdirSync, existsSync } from "node:fs";
import {
mkdtempSync,
rmSync,
readFileSync,
writeFileSync,
mkdirSync,
existsSync,
readdirSync,
} from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";
import {
@@ -9,6 +17,7 @@ import {
findByEntity,
nextId,
allocateId,
withReservedFileSync,
normalizePrompt,
manifestPath,
mediaDir,
@@ -164,6 +173,38 @@ function runTests() {
cleanup();
});
test("failed reservation rollback preserves another completed reservation", () => {
setup();
const kept = withReservedFileSync(tmp, "bgm", ".wav", (reservation) => {
writeFileSync(reservation.fullPath, "completed asset");
return reservation;
});
assert.throws(
() =>
withReservedFileSync(tmp, "bgm", ".wav", () => {
throw new Error("populate failed");
}),
/populate failed/,
);
assert.equal(kept.id, "bgm_001");
assert.equal(readFileSync(kept.fullPath, "utf8"), "completed asset");
assert.deepStrictEqual(readdirSync(typeDirPath(tmp, "bgm")), ["bgm_001.wav"]);
assert.equal(allocateId(tmp, "bgm", ".wav").id, "bgm_002");
cleanup();
});
test("empty reservation result releases the placeholder", () => {
setup();
assert.equal(
withReservedFileSync(tmp, "image", ".jpg", () => null),
null,
);
assert.deepStrictEqual(readdirSync(typeDirPath(tmp, "image")), []);
cleanup();
});
test("findByEntity matches case-insensitively", () => {
setup();
appendRecord(tmp, makeRecord({ entity: "GitHub", type: "icon" }));