mirror of
https://github.com/EchoSixHIYA/WebSpeak-client-for-TeamSpeak.git
synced 2026-12-03 12:02:50 +08:00
91 lines
3.1 KiB
TypeScript
91 lines
3.1 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
import { createSkinPluginAssetUrlCache } from "./skin-plugin-asset-urls.js";
|
|
|
|
test("plugin asset URLs are reused only for the same Blob and released on replacement or skin change", () => {
|
|
const originalCreate = URL.createObjectURL;
|
|
const originalRevoke = URL.revokeObjectURL;
|
|
const created: string[] = [];
|
|
const revoked: string[] = [];
|
|
URL.createObjectURL = () => {
|
|
const url = `blob:skin-test-${created.length + 1}`;
|
|
created.push(url);
|
|
return url;
|
|
};
|
|
URL.revokeObjectURL = (url) => { revoked.push(url); };
|
|
|
|
try {
|
|
const cache = createSkinPluginAssetUrlCache();
|
|
const path = "assets/icon.png";
|
|
const firstAsset = new Blob(["first skin"]);
|
|
const secondAsset = new Blob(["second skin"]);
|
|
|
|
const firstUrl = cache.resolve(path, { [path]: firstAsset });
|
|
assert.equal(firstUrl, "blob:skin-test-1");
|
|
assert.equal(cache.resolve(path, { [path]: firstAsset }), firstUrl);
|
|
assert.equal(cache.size, 1);
|
|
|
|
const secondUrl = cache.resolve(path, { [path]: secondAsset });
|
|
assert.equal(secondUrl, "blob:skin-test-2");
|
|
assert.deepEqual(revoked, [firstUrl]);
|
|
assert.equal(cache.size, 1);
|
|
|
|
cache.clear();
|
|
assert.deepEqual(revoked, [firstUrl, secondUrl]);
|
|
assert.equal(cache.size, 0);
|
|
} finally {
|
|
URL.createObjectURL = originalCreate;
|
|
URL.revokeObjectURL = originalRevoke;
|
|
}
|
|
});
|
|
|
|
test("a missing plugin asset releases a cached URL instead of returning stale artwork", () => {
|
|
const originalCreate = URL.createObjectURL;
|
|
const originalRevoke = URL.revokeObjectURL;
|
|
const revoked: string[] = [];
|
|
URL.createObjectURL = () => "blob:skin-test";
|
|
URL.revokeObjectURL = (url) => { revoked.push(url); };
|
|
|
|
try {
|
|
const cache = createSkinPluginAssetUrlCache();
|
|
const path = "assets/icon.png";
|
|
cache.resolve(path, { [path]: new Blob(["old"]) });
|
|
assert.equal(cache.resolve(path, {}), undefined);
|
|
assert.deepEqual(revoked, ["blob:skin-test"]);
|
|
assert.equal(cache.size, 0);
|
|
} finally {
|
|
URL.createObjectURL = originalCreate;
|
|
URL.revokeObjectURL = originalRevoke;
|
|
}
|
|
});
|
|
|
|
test("plugin asset URLs are released when their final component reference is removed", () => {
|
|
const originalCreate = URL.createObjectURL;
|
|
const originalRevoke = URL.revokeObjectURL;
|
|
const created: string[] = [];
|
|
const revoked: string[] = [];
|
|
URL.createObjectURL = () => {
|
|
const url = `blob:skin-retain-${created.length + 1}`;
|
|
created.push(url);
|
|
return url;
|
|
};
|
|
URL.revokeObjectURL = (url) => { revoked.push(url); };
|
|
|
|
try {
|
|
const cache = createSkinPluginAssetUrlCache();
|
|
cache.resolve("assets/used.png", { "assets/used.png": new Blob(["used"]) });
|
|
cache.resolve("assets/removed.png", { "assets/removed.png": new Blob(["removed"]) });
|
|
|
|
cache.retain(["assets/used.png"]);
|
|
assert.deepEqual(revoked, ["blob:skin-retain-2"]);
|
|
assert.equal(cache.size, 1);
|
|
|
|
cache.retain([]);
|
|
assert.deepEqual(revoked, ["blob:skin-retain-2", "blob:skin-retain-1"]);
|
|
assert.equal(cache.size, 0);
|
|
} finally {
|
|
URL.createObjectURL = originalCreate;
|
|
URL.revokeObjectURL = originalRevoke;
|
|
}
|
|
});
|