fix(catalog): check source assets and offer reachable import alternatives
CI / check (push) Failing after 19s
CI / deploy (push) Skipped
CI / e2e (push) Skipped

This commit is contained in:
Simo committed 2026-09-05 14:56:04 +02:00
1 parent 8c1efae296
commit 159b1f7d1b
10 files changed
+646 -56

No files matched your search

+40
View File
@@ -0,0 +1,40 @@
export interface SourceAssetItem {
id: number;
classname: string;
name: string;
description: string;
type: string;
revision: number;
category: string;
}
export type AssetAvailability = "available" | "missing" | "unknown";
export interface SourceAssetCheck {
classname: string;
state: AssetAvailability;
revision: number;
alternatives: SourceAssetItem[];
}
export function relatedFurniture(
item: SourceAssetItem,
candidates: SourceAssetItem[],
) {
const tail = (name: string) =>
name.toLowerCase().split("_").slice(-2).join("_");
const label = (name: string) => (name ?? "").trim().toLowerCase();
return candidates
.filter(
(candidate) =>
candidate.classname !== item.classname &&
candidate.type === item.type &&
((label(item.name) && label(candidate.name) === label(item.name)) ||
(item.classname.split("_").length >= 3 &&
tail(candidate.classname) === tail(item.classname))),
)
.sort(
(a, b) =>
Number(label(b.name) === label(item.name)) -
Number(label(a.name) === label(item.name)) ||
a.classname.localeCompare(b.classname),
)
.slice(0, 5);
}
@@ -178,3 +178,22 @@ it("imports recycler_kintsugiB with an allocated ID throughout SQL and furnidata
).toContain("2000037258");
expect(result.warnings.join(" ")).toContain("16940");
});
it("does not create or delete SQL rows when a new furniture asset is unavailable", async () => {
vi.clearAllMocks();
mocks.official.mockResolvedValue(null);
mocks.download.mockResolvedValue(false);
mocks.select.mockImplementation(() => ({
from: () => ({ where: () => ({ limit: async () => [] }) }),
}));
mocks.stat.mockRejectedValue(new Error("missing"));
mocks.conflict.mockResolvedValue(null);
const result = await importSingleFurni({
...input,
classname: "missing_new_asset",
});
expect(result.ok).toBe(false);
expect(result.error).toContain("Import not started");
expect(mocks.execute).not.toHaveBeenCalled();
expect(mocks.append).not.toHaveBeenCalled();
});
+42 -49
View File
@@ -619,45 +619,6 @@ export async function importSingleFurni(params: {
const bestRevision = revision || officialHabbo?.revision || 0;
const bestCategory = category || officialHabbo?.category || "unknown";
// ── Insert into DB with enriched data ──────────────────────────────
onProgress?.("writing_db");
try {
if (preservingExisting) {
warnings.push(
`Existing furniture #${itemId}: SQL settings and catalog offers preserved`,
);
} else if (isUpdate) {
// NOTE: live Arcturus `items_base` has no description/revision/rare columns —
// those metadata fields are persisted in FurnitureData.json (see entry below),
// not in the DB. Only refresh the columns this import manages.
await db.execute(sql`
UPDATE items_base SET sprite_id = ${spriteId}, public_name = ${bestName}, item_name = ${classname}, type = ${itemType}
WHERE id = ${spriteId}
`);
} else {
itemId = await reserveFurnitureId(
originalId,
classname,
async (allocatedId) => {
await db.execute(
sql`INSERT INTO items_base (id, sprite_id, public_name, item_name, type) VALUES (${allocatedId}, ${allocatedId}, ${bestName}, ${classname}, ${itemType})`,
);
},
);
spriteId = itemId;
if (spriteId !== originalId)
warnings.push(
`Source ID ${originalId} is occupied; imported with local ID ${spriteId}`,
);
}
} catch (err) {
return {
ok: false,
warnings,
error: `DB ${isUpdate ? "update" : "insert"} failed: ${(err as Error).message}`,
};
}
// ── Derive file-safe names ────────────────────────────────────────
const rev = bestRevision;
const assetTargets = await getFurniAssetWriteTargets();
@@ -828,7 +789,7 @@ export async function importSingleFurni(params: {
.catch(() => null);
const nitroValid = nitroStat && nitroStat.size >= 128;
if (!nitroValid && !conversionResult) {
if (!nitroValid) {
if (nitroStat) {
warnings.push(
`Nitro file too small/corrupt (${nitroStat.size} bytes) — removing`,
@@ -839,24 +800,56 @@ export async function importSingleFurni(params: {
/* ignore */
}
}
try {
if (!isUpdate)
await db.execute(
sql`DELETE FROM items_base WHERE id = ${itemId} AND item_name = ${classname}`,
);
} catch {
/* best effort cleanup */
}
return {
ok: false,
warnings,
error: `${isUpdate ? "No .nitro file could be created; existing furniture was preserved" : "Import rolled back: no .nitro file could be created"} (${classname}, revision ${rev}). ${warnings
error: `${isUpdate ? "No .nitro file could be created; existing furniture was preserved" : "Import not started: no .nitro file could be created"} (${classname}, revision ${rev}). ${warnings
.filter((warning) => /conversion|download|SWF/.test(warning))
.slice(-3)
.join(". ")}`,
};
}
// ── Insert into DB with enriched data ──────────────────────────────
onProgress?.("writing_db");
try {
if (preservingExisting) {
warnings.push(
`Existing furniture #${itemId}: SQL settings and catalog offers preserved`,
);
} else if (isUpdate) {
// NOTE: live Arcturus `items_base` has no description/revision/rare columns —
// those metadata fields are persisted in FurnitureData.json (see entry below),
// not in the DB. Only refresh the columns this import manages.
await db.execute(sql`
UPDATE items_base SET sprite_id = ${spriteId}, public_name = ${bestName}, item_name = ${classname}, type = ${itemType}
WHERE id = ${spriteId}
`);
} else {
itemId = await reserveFurnitureId(
originalId,
classname,
async (allocatedId) => {
await db.execute(
sql`INSERT INTO items_base (id, sprite_id, public_name, item_name, type) VALUES (${allocatedId}, ${allocatedId}, ${bestName}, ${classname}, ${itemType})`,
);
},
);
spriteId = itemId;
if (spriteId !== originalId)
warnings.push(
`Source ID ${originalId} is occupied; imported with local ID ${spriteId}`,
);
}
} catch (err) {
return {
ok: false,
warnings,
error: `DB ${isUpdate ? "update" : "insert"} failed: ${(err as Error).message}`,
};
}
await Promise.all([
mirrorFileToDirs(swfPath, `${safeSwfName}.swf`, mirrorSwfDirs, warnings),
mirrorFileToDirs(
@@ -0,0 +1,99 @@
// @vitest-environment node
import { afterEach, describe, expect, it, vi } from "vitest";
import {
relatedFurniture,
type SourceAssetItem,
} from "@/lib/furni/source-assets";
import { inspectSourceAssets, probeAsset } from "./furniture-source-assets";
const source: SourceAssetItem = {
id: 1,
classname: "h25_monkeybzn_pole",
name: "Jungle flags",
description: "Flags",
type: "flooritem",
revision: 73253,
category: "other",
};
const alternative = {
...source,
id: 2,
classname: "br_c25_monkeybzn_pole",
revision: 73257,
};
afterEach(() => vi.unstubAllGlobals());
describe("source asset preflight", () => {
it("suggests reachable alternatives for unavailable source files", async () => {
const fetchMock = vi.fn(
async (url: string) =>
new Response(null, { status: url.includes("/73257/") ? 200 : 404 }),
);
vi.stubGlobal("fetch", fetchMock);
const result = await inspectSourceAssets(source, [source, alternative]);
expect(result.state).toBe("missing");
expect(result.alternatives).toEqual([alternative]);
});
it("does not describe blocked or unreachable servers as missing files", async () => {
vi.stubGlobal(
"fetch",
vi.fn(async () => new Response(null, { status: 403 })),
);
const result = await inspectSourceAssets(
{ ...source, classname: "blocked_test" },
[alternative],
);
expect(result.state).toBe("unknown");
expect(result.alternatives).toEqual([]);
});
it("accepts an available configured Nitro source", async () => {
vi.stubGlobal(
"fetch",
vi.fn(
async (url: string) =>
new Response(null, { status: url.endsWith(".nitro") ? 200 : 404 }),
),
);
const result = await inspectSourceAssets(
{ ...source, classname: "custom_test" },
[],
{
id: "custom",
name: "Custom",
hotel: "",
furnidataUrl: "https://source.example/data.json",
nitroBaseUrl: "https://source.example/furniture",
iconBaseUrl: "",
},
);
expect(result.state).toBe("available");
});
it("bounds suggestions and excludes different furniture types", () => {
expect(
relatedFurniture(source, [source, { ...alternative, type: "wallitem" }]),
).toEqual([]);
expect(
relatedFurniture(
source,
Array.from({ length: 20 }, (_, id) => ({
...alternative,
id,
classname: `f${id}_monkeybzn_pole`,
})),
),
).toHaveLength(5);
});
it("handles unnamed source entries while finding related assets", () => {
expect(
relatedFurniture(source, [
{ ...alternative, name: null as unknown as string },
]),
).toHaveLength(1);
});
it("caches a probe to avoid repeating CDN requests", async () => {
const fetchMock = vi.fn(async () => new Response(null, { status: 200 }));
vi.stubGlobal("fetch", fetchMock);
await probeAsset("https://example.test/cache.nitro");
await probeAsset("https://example.test/cache.nitro");
expect(fetchMock).toHaveBeenCalledTimes(1);
});
});
@@ -0,0 +1,98 @@
import type {
AssetAvailability,
SourceAssetCheck,
SourceAssetItem,
} from "@/lib/furni/source-assets";
import { relatedFurniture } from "@/lib/furni/source-assets";
import type { CloneSource } from "./clone-sources";
import { browserHeaders } from "./import/core/browser-headers";
const cache = new Map<string, { expires: number; state: AssetAvailability }>();
export async function probeAsset(url: string): Promise<AssetAvailability> {
const cached = cache.get(url);
if (cached && cached.expires > Date.now()) return cached.state;
let state: AssetAvailability = "unknown";
try {
const response = await fetch(url, {
method: "HEAD",
signal: AbortSignal.timeout(6000),
headers: browserHeaders(),
});
if (
response.ok &&
!response.headers.get("content-type")?.includes("text/html")
)
state = "available";
else if (response.status === 404 || response.status === 410)
state = "missing";
} catch {
/* A network problem is not proof the asset is absent. */
}
if (cache.size >= 2000) cache.clear();
cache.set(url, { expires: Date.now() + 60000, state });
return state;
}
export function furnitureAssetUrls(
item: SourceAssetItem,
source?: CloneSource | null,
) {
const base = encodeURIComponent(item.classname.split("*")[0]);
const urls: string[] = [];
if (source?.nitroBaseUrl)
urls.push(`${source.nitroBaseUrl.replace(/\/+$/, "")}/${base}.nitro`);
if (source?.sourceSwfBaseUrl) {
const root = source.sourceSwfBaseUrl.replace(/\/+$/, "");
urls.push(
`${root}/hof_furni/${item.revision}/${base}.swf`,
`${root}/hof_furni/${base}.swf`,
);
}
urls.push(
`https://images.habbo.com/dcr/hof_furni/${item.revision}/${base}.swf`,
);
return urls;
}
async function available(
item: SourceAssetItem,
source?: CloneSource | null,
): Promise<AssetAvailability> {
const states = await Promise.all(
furnitureAssetUrls(item, source).map(probeAsset),
);
return states.includes("available")
? "available"
: states.every((state) => state === "missing")
? "missing"
: "unknown";
}
export async function inspectSourceAssets(
item: SourceAssetItem,
official: SourceAssetItem[],
source?: CloneSource | null,
): Promise<SourceAssetCheck> {
const canonical = official.find(
(entry) => entry.classname === item.classname,
);
const effective = {
...item,
revision: item.revision || canonical?.revision || 0,
};
const state = await available(effective, source);
const alternatives: SourceAssetItem[] = [];
if (state === "missing") {
for (const candidate of relatedFurniture(
{ ...effective, name: canonical?.name || item.name },
official,
)) {
if ((await available(candidate)) === "available")
alternatives.push(candidate);
if (alternatives.length === 3) break;
}
}
return {
classname: item.classname,
revision: effective.revision,
state,
alternatives,
};
}