Recognize imported furniture by normalized classname and local IDs
This commit is contained in:
1 parent
9dc6b8a261
commit
a78d8256e0
7 files changed
+254
-46
No files matched your search
@@ -0,0 +1,55 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { indexLocalFurniture, localFurnitureStatus } from "./local-presence";
|
||||
|
||||
const index = indexLocalFurniture([
|
||||
{ id: 2000037258, itemName: "recycler_kintsugib" },
|
||||
]);
|
||||
describe("local furniture recognition", () => {
|
||||
it("recognizes casing differences and uses the allocated catalog ID", () => {
|
||||
expect(
|
||||
localFurnitureStatus(
|
||||
"recycler_kintsugiB",
|
||||
index,
|
||||
new Set(["recycler_kintsugib"]),
|
||||
new Set([2000037258]),
|
||||
),
|
||||
).toEqual({
|
||||
alreadyImported: true,
|
||||
localId: 2000037258,
|
||||
hasFurnidata: true,
|
||||
hasCatalogEntry: true,
|
||||
});
|
||||
});
|
||||
it("does not confuse the base furniture with its named variant", () => {
|
||||
expect(
|
||||
localFurnitureStatus(
|
||||
"recycler_kintsugi",
|
||||
index,
|
||||
new Set(),
|
||||
new Set([16940]),
|
||||
).alreadyImported,
|
||||
).toBe(false);
|
||||
});
|
||||
it("does not identify a source-ID collision as a matching catalog offer", () => {
|
||||
expect(
|
||||
localFurnitureStatus(
|
||||
"recycler_kintsugiB",
|
||||
index,
|
||||
new Set(),
|
||||
new Set([16940]),
|
||||
).hasCatalogEntry,
|
||||
).toBe(false);
|
||||
});
|
||||
it("retains separate color variants and duplicate SQL records", () => {
|
||||
const rows = indexLocalFurniture([
|
||||
{ id: 1, itemName: "chair*0" },
|
||||
{ id: 2, itemName: "CHAIR*0" },
|
||||
{ id: 3, itemName: "chair*1" },
|
||||
]);
|
||||
expect(rows.get("chair*0")).toHaveLength(2);
|
||||
expect(rows.get("chair*1")).toHaveLength(1);
|
||||
expect(
|
||||
localFurnitureStatus("chair*0", rows, new Set(), new Set()).localId,
|
||||
).toBeUndefined();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,29 @@
|
||||
import { normalizeClassname } from "./classname";
|
||||
|
||||
export function indexLocalFurniture<T extends { id: number; itemName: string }>(
|
||||
rows: T[],
|
||||
) {
|
||||
const index = new Map<string, T[]>();
|
||||
for (const row of rows) {
|
||||
const key = normalizeClassname(row.itemName);
|
||||
const matches = index.get(key) ?? [];
|
||||
matches.push(row);
|
||||
index.set(key, matches);
|
||||
}
|
||||
return index;
|
||||
}
|
||||
export function localFurnitureStatus(
|
||||
classname: string,
|
||||
local: Map<string, Array<{ id: number; itemName: string }>>,
|
||||
furnidata: Set<string>,
|
||||
catalogIds: Set<number>,
|
||||
) {
|
||||
const key = normalizeClassname(classname);
|
||||
const rows = local.get(key) ?? [];
|
||||
return {
|
||||
alreadyImported: rows.length > 0,
|
||||
localId: rows.length === 1 ? rows[0].id : undefined,
|
||||
hasFurnidata: furnidata.has(key),
|
||||
hasCatalogEntry: rows.some((row) => catalogIds.has(row.id)),
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user