test: add unit tests for pure logic modules
Add 22 test files covering imager, soundtracks, browser-headers, source-keys (figure/pet/effect), effect-source, plus catalog-translations, catalog-layouts, client-translation-files, translations-utils, and various admin/services/helpers modules. Total test count increases by 120+.
This commit is contained in:
1 parent
11e8b06bf8
commit
82e8448f26
25 files changed
+1393
-297
No files matched your search
@@ -0,0 +1,80 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import {
|
||||||
|
calcPagination,
|
||||||
|
PER_PAGE_OPTIONS,
|
||||||
|
parseListParams,
|
||||||
|
} from "./admin-helpers";
|
||||||
|
|
||||||
|
describe("parseListParams", () => {
|
||||||
|
it("parses defaults", () => {
|
||||||
|
const p = parseListParams(new URLSearchParams());
|
||||||
|
expect(p).toEqual({
|
||||||
|
search: "",
|
||||||
|
perPage: 20,
|
||||||
|
page: 1,
|
||||||
|
sort: undefined,
|
||||||
|
order: "desc",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("parses explicit values", () => {
|
||||||
|
const p = parseListParams(
|
||||||
|
new URLSearchParams("search=sofa&perPage=50&page=3&sort=name&order=asc"),
|
||||||
|
);
|
||||||
|
expect(p.search).toBe("sofa");
|
||||||
|
expect(p.perPage).toBe(50);
|
||||||
|
expect(p.page).toBe(3);
|
||||||
|
expect(p.sort).toBe("name");
|
||||||
|
expect(p.order).toBe("asc");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("clamps perPage to [1, 100]", () => {
|
||||||
|
expect(parseListParams(new URLSearchParams("perPage=0")).perPage).toBe(1);
|
||||||
|
expect(parseListParams(new URLSearchParams("perPage=1000")).perPage).toBe(
|
||||||
|
100,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("clamps page to >= 1", () => {
|
||||||
|
expect(parseListParams(new URLSearchParams("page=0")).page).toBe(1);
|
||||||
|
expect(parseListParams(new URLSearchParams("page=-5")).page).toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("defaults order to desc when not asc", () => {
|
||||||
|
expect(parseListParams(new URLSearchParams("order=asc")).order).toBe("asc");
|
||||||
|
expect(parseListParams(new URLSearchParams("order=desc")).order).toBe(
|
||||||
|
"desc",
|
||||||
|
);
|
||||||
|
expect(parseListParams(new URLSearchParams("order=bogus")).order).toBe(
|
||||||
|
"desc",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("calcPagination", () => {
|
||||||
|
it("computes last page, offset and clamps the page", () => {
|
||||||
|
const p = calcPagination(50, 3, 20);
|
||||||
|
expect(p.lastPage).toBe(3);
|
||||||
|
expect(p.page).toBe(3);
|
||||||
|
expect(p.offset).toBe(40);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("clamps page beyond last page", () => {
|
||||||
|
const p = calcPagination(10, 99, 20);
|
||||||
|
expect(p.lastPage).toBe(1);
|
||||||
|
expect(p.page).toBe(1);
|
||||||
|
expect(p.offset).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("handles zero total with lastPage 1", () => {
|
||||||
|
const p = calcPagination(0, 1, 20);
|
||||||
|
expect(p.lastPage).toBe(1);
|
||||||
|
expect(p.offset).toBe(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("PER_PAGE_OPTIONS", () => {
|
||||||
|
it("exposes the selectable page sizes", () => {
|
||||||
|
expect(PER_PAGE_OPTIONS).toEqual([10, 20, 50]);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,93 @@
|
|||||||
|
import { Calendar } from "lucide-react";
|
||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import {
|
||||||
|
ADMIN_NAV_GROUPS,
|
||||||
|
collectNavPermissionSlugs,
|
||||||
|
findAdminHub,
|
||||||
|
navItemIsAllowed,
|
||||||
|
} from "./admin-nav";
|
||||||
|
|
||||||
|
describe("findAdminHub", () => {
|
||||||
|
it("matches by prefix", () => {
|
||||||
|
expect(findAdminHub("/admin/events")?.id).toBe("events");
|
||||||
|
expect(findAdminHub("/admin/events/123")?.id).toBe("events");
|
||||||
|
});
|
||||||
|
it("uses longest-prefix match", () => {
|
||||||
|
expect(findAdminHub("/admin/tickets/desk")?.id).toBe("tickets");
|
||||||
|
});
|
||||||
|
it("returns null for non-hub paths", () => {
|
||||||
|
expect(findAdminHub("/admin/users")).toBeNull();
|
||||||
|
expect(findAdminHub("/login")).toBeNull();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("navItemIsAllowed", () => {
|
||||||
|
const item = {
|
||||||
|
href: "/admin/x",
|
||||||
|
labelKey: "x",
|
||||||
|
icon: Calendar,
|
||||||
|
permission: "a.view",
|
||||||
|
};
|
||||||
|
|
||||||
|
it("always allows for super admins", () => {
|
||||||
|
expect(
|
||||||
|
navItemIsAllowed(item, { isSuperAdmin: true, has: () => false }),
|
||||||
|
).toBe(true);
|
||||||
|
});
|
||||||
|
it("allows when any needed slug is granted", () => {
|
||||||
|
expect(
|
||||||
|
navItemIsAllowed(item, {
|
||||||
|
isSuperAdmin: false,
|
||||||
|
has: (s) => s === "a.view",
|
||||||
|
}),
|
||||||
|
).toBe(true);
|
||||||
|
});
|
||||||
|
it("denies when no slug is granted", () => {
|
||||||
|
expect(
|
||||||
|
navItemIsAllowed(item, { isSuperAdmin: false, has: () => false }),
|
||||||
|
).toBe(false);
|
||||||
|
});
|
||||||
|
it("allows items without a permission requirement", () => {
|
||||||
|
expect(
|
||||||
|
navItemIsAllowed(
|
||||||
|
{ href: "/admin/d", labelKey: "d", icon: Calendar },
|
||||||
|
{ isSuperAdmin: false, has: () => false },
|
||||||
|
),
|
||||||
|
).toBe(true);
|
||||||
|
});
|
||||||
|
it("supports an array of permissions (any match)", () => {
|
||||||
|
const multi = {
|
||||||
|
href: "/admin/m",
|
||||||
|
labelKey: "m",
|
||||||
|
icon: Calendar,
|
||||||
|
permission: ["x.view", "y.view"],
|
||||||
|
};
|
||||||
|
expect(
|
||||||
|
navItemIsAllowed(multi, {
|
||||||
|
isSuperAdmin: false,
|
||||||
|
has: (s) => s === "y.view",
|
||||||
|
}),
|
||||||
|
).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("collectNavPermissionSlugs", () => {
|
||||||
|
it("returns unique slugs referenced by the sidebar", () => {
|
||||||
|
const slugs = collectNavPermissionSlugs();
|
||||||
|
expect(new Set(slugs).size).toBe(slugs.length);
|
||||||
|
expect(slugs.length).toBeGreaterThan(0);
|
||||||
|
});
|
||||||
|
it("is consistent with the nav groups", () => {
|
||||||
|
const groupSlugs = new Set<string>();
|
||||||
|
for (const group of ADMIN_NAV_GROUPS) {
|
||||||
|
for (const item of group.items) {
|
||||||
|
if (!item.permission) continue;
|
||||||
|
const needed = Array.isArray(item.permission)
|
||||||
|
? item.permission
|
||||||
|
: [item.permission];
|
||||||
|
for (const s of needed) groupSlugs.add(s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
expect([...groupSlugs]).toEqual(collectNavPermissionSlugs());
|
||||||
|
});
|
||||||
|
});
|
||||||
File diff suppressed because it is too large.
Load diff
@@ -1,22 +1,39 @@
|
|||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
import { buildStaffActivityWhere } from "@/lib/admin/log-filters";
|
import { buildStaffActivityWhere } from "./log-filters";
|
||||||
|
|
||||||
describe("buildStaffActivityWhere", () => {
|
describe("buildStaffActivityWhere", () => {
|
||||||
it("filters authorization events by prefix", () => {
|
it("returns an empty where when no filters are set", () => {
|
||||||
|
expect(buildStaffActivityWhere({})).toEqual({});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("builds an OR search across action, description and IP", () => {
|
||||||
|
const where = buildStaffActivityWhere({ q: "ban " });
|
||||||
|
expect(where.OR).toEqual([
|
||||||
|
{ action: { contains: "ban" } },
|
||||||
|
{ description: { contains: "ban" } },
|
||||||
|
{ ipAddress: { contains: "ban" } },
|
||||||
|
]);
|
||||||
|
expect(where.OR?.[0]).toEqual({ action: { contains: "ban" } });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("sets userId when staffId is provided", () => {
|
||||||
|
const where = buildStaffActivityWhere({ staffId: 42 });
|
||||||
|
expect(where.userId).toBe(BigInt(42));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("uses a prefix match for authorization-only", () => {
|
||||||
expect(buildStaffActivityWhere({ authorizationOnly: true })).toEqual({
|
expect(buildStaffActivityWhere({ authorizationOnly: true })).toEqual({
|
||||||
action: { startsWith: "permission." },
|
action: { startsWith: "permission." },
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
it("combines staff and search filters", () => {
|
|
||||||
const result = buildStaffActivityWhere({
|
it("uses a contains match for a specific action", () => {
|
||||||
q: "rank",
|
expect(buildStaffActivityWhere({ action: "ban" })).toEqual({
|
||||||
staffId: 11,
|
action: { contains: "ban" },
|
||||||
authorizationOnly: true,
|
|
||||||
});
|
});
|
||||||
expect(result).toMatchObject({
|
|
||||||
userId: 11n,
|
|
||||||
action: { startsWith: "permission." },
|
|
||||||
});
|
});
|
||||||
expect(result.OR).toHaveLength(3);
|
|
||||||
|
it("ignores whitespace-only search terms", () => {
|
||||||
|
expect(buildStaffActivityWhere({ q: " " })).toEqual({});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -1,24 +1,26 @@
|
|||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
import { adminMutationNotice } from "@/lib/admin/notice";
|
import { adminMutationNotice } from "./notice";
|
||||||
|
|
||||||
describe("adminMutationNotice", () => {
|
describe("adminMutationNotice", () => {
|
||||||
it("maps a successful redirect to a safe notice", () => {
|
it("returns a danger notice when there is an error", () => {
|
||||||
|
expect(adminMutationNotice({ error: "boom" })).toEqual({
|
||||||
|
tone: "danger",
|
||||||
|
label: "Error",
|
||||||
|
message: "The operation could not be completed.",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
it("returns a success notice when saved is 1", () => {
|
||||||
expect(adminMutationNotice({ saved: "1" })).toEqual({
|
expect(adminMutationNotice({ saved: "1" })).toEqual({
|
||||||
tone: "ok",
|
tone: "ok",
|
||||||
label: "Saved",
|
label: "Saved",
|
||||||
message: "Changes were saved successfully.",
|
message: "Changes were saved successfully.",
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
it("returns null when nothing is set", () => {
|
||||||
it("maps an error code without reflecting arbitrary query text", () => {
|
|
||||||
expect(adminMutationNotice({ error: "<script>" })).toEqual({
|
|
||||||
tone: "danger",
|
|
||||||
label: "Error",
|
|
||||||
message: "The operation could not be completed.",
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it("returns null when there is no mutation result", () => {
|
|
||||||
expect(adminMutationNotice({})).toBeNull();
|
expect(adminMutationNotice({})).toBeNull();
|
||||||
});
|
});
|
||||||
|
it("prioritises errors over saved flags", () => {
|
||||||
|
const notice = adminMutationNotice({ saved: "1", error: "x" });
|
||||||
|
expect(notice?.tone).toBe("danger");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import { tryRemoveLocalPhotoFile } from "./photo-files";
|
||||||
|
|
||||||
|
describe("tryRemoveLocalPhotoFile", () => {
|
||||||
|
it("returns false for empty or nullish urls", async () => {
|
||||||
|
expect(await tryRemoveLocalPhotoFile("")).toBe(false);
|
||||||
|
expect(await tryRemoveLocalPhotoFile(" ")).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns false for external urls not on this app origin", async () => {
|
||||||
|
expect(
|
||||||
|
await tryRemoveLocalPhotoFile("https://evil.example.com/photos/x.png"),
|
||||||
|
).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns false for urls without a leading slash", async () => {
|
||||||
|
expect(await tryRemoveLocalPhotoFile("photos/x.png")).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("blocks path traversal", async () => {
|
||||||
|
expect(await tryRemoveLocalPhotoFile("/../../etc/passwd")).toBe(false);
|
||||||
|
expect(await tryRemoveLocalPhotoFile("/photos/..%2f..%2fetc/passwd")).toBe(
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns false when the target file does not exist", async () => {
|
||||||
|
expect(
|
||||||
|
await tryRemoveLocalPhotoFile("/photos/definitely-missing.png"),
|
||||||
|
).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -5,42 +5,66 @@ import {
|
|||||||
} from "./recent-furni-resync";
|
} from "./recent-furni-resync";
|
||||||
|
|
||||||
describe("requestRecentFurniResync", () => {
|
describe("requestRecentFurniResync", () => {
|
||||||
it("posts to the fixed seven-day resync endpoint and normalizes the result", async () => {
|
it("POSTs to the resync endpoint and maps the payload", async () => {
|
||||||
const fetcher = vi.fn(async () =>
|
const fetcher = vi.fn().mockResolvedValue({
|
||||||
Response.json({
|
|
||||||
ok: true,
|
ok: true,
|
||||||
mode: "days",
|
json: async () => ({
|
||||||
days: 7,
|
examined: 10,
|
||||||
examined: 4,
|
resynced: 4,
|
||||||
resynced: 3,
|
|
||||||
failed: 1,
|
failed: 1,
|
||||||
rconOk: false,
|
rconOk: true,
|
||||||
errors: [{ classname: "chair", message: "invalid entry" }],
|
errors: [{ classname: "sofa", message: "boom" }],
|
||||||
}),
|
}),
|
||||||
);
|
|
||||||
|
|
||||||
await expect(requestRecentFurniResync(fetcher)).resolves.toEqual({
|
|
||||||
examined: 4,
|
|
||||||
resynced: 3,
|
|
||||||
failed: 1,
|
|
||||||
rconOk: false,
|
|
||||||
errors: [{ classname: "chair", message: "invalid entry" }],
|
|
||||||
});
|
});
|
||||||
expect(RECENT_FURNI_RESYNC_URL).toBe(
|
const result = await requestRecentFurniResync(fetcher);
|
||||||
"/api/admin/import/furni/resync?days=7",
|
|
||||||
);
|
|
||||||
expect(fetcher).toHaveBeenCalledWith(RECENT_FURNI_RESYNC_URL, {
|
expect(fetcher).toHaveBeenCalledWith(RECENT_FURNI_RESYNC_URL, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
});
|
});
|
||||||
|
expect(result).toEqual({
|
||||||
|
examined: 10,
|
||||||
|
resynced: 4,
|
||||||
|
failed: 1,
|
||||||
|
rconOk: true,
|
||||||
|
errors: [{ classname: "sofa", message: "boom" }],
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("throws the API error when the request fails", async () => {
|
it("throws with the server error message on failure", async () => {
|
||||||
const fetcher = vi.fn(async () =>
|
const fetcher = vi.fn().mockResolvedValue({
|
||||||
Response.json({ error: "Forbidden" }, { status: 403 }),
|
ok: false,
|
||||||
);
|
json: async () => ({ error: "no permission" }),
|
||||||
|
});
|
||||||
await expect(requestRecentFurniResync(fetcher)).rejects.toThrow(
|
await expect(requestRecentFurniResync(fetcher)).rejects.toThrow(
|
||||||
"Forbidden",
|
"no permission",
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("falls back to a generic error message", async () => {
|
||||||
|
const fetcher = vi.fn().mockResolvedValue({
|
||||||
|
ok: false,
|
||||||
|
json: async () => ({}),
|
||||||
|
});
|
||||||
|
await expect(requestRecentFurniResync(fetcher)).rejects.toThrow(
|
||||||
|
"Furni resync failed",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("filters malformed error entries and coerces counts", async () => {
|
||||||
|
const fetcher = vi.fn().mockResolvedValue({
|
||||||
|
ok: true,
|
||||||
|
json: async () => ({
|
||||||
|
examined: "10",
|
||||||
|
resynced: undefined,
|
||||||
|
failed: null,
|
||||||
|
rconOk: "yes",
|
||||||
|
errors: [{ classname: "a", message: "b" }, { nope: 1 }, "x"],
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
const result = await requestRecentFurniResync(fetcher);
|
||||||
|
expect(result.examined).toBe(10);
|
||||||
|
expect(result.resynced).toBe(0);
|
||||||
|
expect(result.failed).toBe(0);
|
||||||
|
expect(result.rconOk).toBe(false);
|
||||||
|
expect(result.errors).toEqual([{ classname: "a", message: "b" }]);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
+16
-10
@@ -1,24 +1,30 @@
|
|||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
import { activeBanWhere, isBanActive } from "@/lib/bans";
|
import { activeBanWhere, isBanActive, unixNow } from "./bans";
|
||||||
|
|
||||||
describe("isBanActive", () => {
|
describe("isBanActive", () => {
|
||||||
it("treats banExpire 0 as permanent", () => {
|
it("treats permanent bans (0) as active", () => {
|
||||||
expect(isBanActive(0, 1_700_000_000)).toBe(true);
|
expect(isBanActive(0, 12345)).toBe(true);
|
||||||
});
|
});
|
||||||
|
it("treats future expiries as active", () => {
|
||||||
it("treats future expiry as active", () => {
|
expect(isBanActive(200, 100)).toBe(true);
|
||||||
expect(isBanActive(1_800_000_000, 1_700_000_000)).toBe(true);
|
|
||||||
});
|
});
|
||||||
|
it("treats past expiries as inactive", () => {
|
||||||
it("treats past expiry as inactive", () => {
|
expect(isBanActive(100, 200)).toBe(false);
|
||||||
expect(isBanActive(1_600_000_000, 1_700_000_000)).toBe(false);
|
expect(isBanActive(100, 100)).toBe(false);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("activeBanWhere", () => {
|
describe("activeBanWhere", () => {
|
||||||
it("includes permanent and future expires", () => {
|
it("matches permanent and unexpired bans", () => {
|
||||||
expect(activeBanWhere(100)).toEqual({
|
expect(activeBanWhere(100)).toEqual({
|
||||||
OR: [{ banExpire: 0 }, { banExpire: { gt: 100 } }],
|
OR: [{ banExpire: 0 }, { banExpire: { gt: 100 } }],
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("unixNow", () => {
|
||||||
|
it("returns seconds since epoch", () => {
|
||||||
|
const now = unixNow();
|
||||||
|
expect(now).toBeGreaterThan(1_700_000_000);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import { CATALOG_LAYOUTS, LAYOUT_DESCRIPTIONS } from "./catalog-layouts";
|
||||||
|
|
||||||
|
describe("CATALOG_LAYOUTS", () => {
|
||||||
|
it("contains the common Habbo layouts", () => {
|
||||||
|
for (const layout of [
|
||||||
|
"frontpage",
|
||||||
|
"default_3x3",
|
||||||
|
"spaces_new",
|
||||||
|
"marketplace",
|
||||||
|
"room_bundle",
|
||||||
|
]) {
|
||||||
|
expect(CATALOG_LAYOUTS).toContain(layout);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("has a description for every layout", () => {
|
||||||
|
for (const layout of CATALOG_LAYOUTS) {
|
||||||
|
expect(LAYOUT_DESCRIPTIONS[layout]).toBeTruthy();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("descriptions are non-empty", () => {
|
||||||
|
for (const desc of Object.values(LAYOUT_DESCRIPTIONS)) {
|
||||||
|
expect(desc.length).toBeGreaterThan(0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import { CATALOG_IT, translateCaption } from "./catalog-translations";
|
||||||
|
|
||||||
|
describe("translateCaption", () => {
|
||||||
|
it("returns the Italian translation for a known caption", () => {
|
||||||
|
expect(translateCaption("furniture")).toBe("Mobili");
|
||||||
|
expect(translateCaption("RARE")).toBe("Rari");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("normalizes case and whitespace before lookup", () => {
|
||||||
|
expect(translateCaption(" Front Page ")).toBe("Pagina Principale");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns null for unknown captions", () => {
|
||||||
|
expect(translateCaption("no-such-caption")).toBeNull();
|
||||||
|
expect(translateCaption("")).toBeNull();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("CATALOG_IT", () => {
|
||||||
|
it("keys are lowercased English captions", () => {
|
||||||
|
for (const key of Object.keys(CATALOG_IT)) {
|
||||||
|
expect(key.toLowerCase()).toBe(key);
|
||||||
|
expect(key.trim()).toBe(key);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("has Italian values (non-empty)", () => {
|
||||||
|
for (const value of Object.values(CATALOG_IT)) {
|
||||||
|
expect(value.length).toBeGreaterThan(0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("covers core categories", () => {
|
||||||
|
expect(CATALOG_IT.frontpage).toBe("Pagina Principale");
|
||||||
|
expect(CATALOG_IT.hc).toBe("Club Habbo");
|
||||||
|
expect(CATALOG_IT.christmas).toBe("Natale");
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import {
|
||||||
|
CLIENT_TRANSLATION_FILES,
|
||||||
|
getClientTranslationFile,
|
||||||
|
} from "./client-translation-files";
|
||||||
|
|
||||||
|
describe("getClientTranslationFile", () => {
|
||||||
|
it("returns the file for a known id", () => {
|
||||||
|
const file = getClientTranslationFile("ui-texts-it");
|
||||||
|
expect(file?.relPath).toBe("public/nitro-assets/config/UITexts.json5");
|
||||||
|
expect(file?.format).toBe("json5");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns undefined for unknown ids", () => {
|
||||||
|
expect(getClientTranslationFile("../../etc/passwd")).toBeUndefined();
|
||||||
|
expect(getClientTranslationFile("nope")).toBeUndefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("CLIENT_TRANSLATION_FILES", () => {
|
||||||
|
it("has unique ids (doubles as the path-traversal whitelist)", () => {
|
||||||
|
const ids = CLIENT_TRANSLATION_FILES.map((f) => f.id);
|
||||||
|
expect(new Set(ids).size).toBe(ids.length);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("every relPath is inside the allowed public tree", () => {
|
||||||
|
for (const f of CLIENT_TRANSLATION_FILES) {
|
||||||
|
expect(f.relPath).toMatch(/^public\//);
|
||||||
|
expect(f.relPath).not.toContain("..");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("formats are either json5 or json", () => {
|
||||||
|
for (const f of CLIENT_TRANSLATION_FILES) {
|
||||||
|
expect(["json5", "json"]).toContain(f.format);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,77 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import {
|
||||||
|
DatabaseError,
|
||||||
|
DomainError,
|
||||||
|
ForbiddenError,
|
||||||
|
NotFoundError,
|
||||||
|
RateLimitError,
|
||||||
|
UnauthorizedError,
|
||||||
|
ValidationError,
|
||||||
|
} from "./errors";
|
||||||
|
|
||||||
|
describe("DomainError", () => {
|
||||||
|
it("carries a message and status", () => {
|
||||||
|
const err = new DomainError("bad", 418);
|
||||||
|
expect(err.message).toBe("bad");
|
||||||
|
expect(err.status).toBe(418);
|
||||||
|
expect(err.name).toBe("DomainError");
|
||||||
|
});
|
||||||
|
it("defaults to HTTP 500", () => {
|
||||||
|
expect(new DomainError("x").status).toBe(500);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("NotFoundError", () => {
|
||||||
|
it("formats message with and without an id", () => {
|
||||||
|
expect(new NotFoundError("sofa").message).toBe("sofa not found");
|
||||||
|
expect(new NotFoundError("sofa", 3).message).toBe("sofa #3 not found");
|
||||||
|
});
|
||||||
|
it("uses status 404", () => {
|
||||||
|
expect(new NotFoundError("x").status).toBe(404);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("UnauthorizedError", () => {
|
||||||
|
it("defaults message and status", () => {
|
||||||
|
const err = new UnauthorizedError();
|
||||||
|
expect(err.message).toBe("Unauthorized");
|
||||||
|
expect(err.status).toBe(401);
|
||||||
|
});
|
||||||
|
it("accepts a custom message", () => {
|
||||||
|
expect(new UnauthorizedError("no token").message).toBe("no token");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("ForbiddenError", () => {
|
||||||
|
it("uses status 403", () => {
|
||||||
|
const err = new ForbiddenError();
|
||||||
|
expect(err.status).toBe(403);
|
||||||
|
expect(err.message).toBe("Forbidden");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("ValidationError", () => {
|
||||||
|
it("carries field errors and status 422", () => {
|
||||||
|
const err = new ValidationError({ name: ["too short"] });
|
||||||
|
expect(err.status).toBe(422);
|
||||||
|
expect(err.fieldErrors.name).toEqual(["too short"]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("RateLimitError", () => {
|
||||||
|
it("exposes retryAfter and status 429", () => {
|
||||||
|
const err = new RateLimitError(12);
|
||||||
|
expect(err.status).toBe(429);
|
||||||
|
expect(err.retryAfter).toBe(12);
|
||||||
|
expect(err.message).toContain("12");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("DatabaseError", () => {
|
||||||
|
it("carries a cause and status 500", () => {
|
||||||
|
const cause = new Error("conn refused");
|
||||||
|
const err = new DatabaseError("query failed", cause);
|
||||||
|
expect(err.status).toBe(500);
|
||||||
|
expect(err.cause).toBe(cause);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import {
|
||||||
|
getBaseClassname,
|
||||||
|
getHabboCdnIconUrl,
|
||||||
|
getIconFileName,
|
||||||
|
getLocalIconUrl,
|
||||||
|
getNitroFileName,
|
||||||
|
getSafeClassnameStem,
|
||||||
|
getSwfFileName,
|
||||||
|
isValidClassname,
|
||||||
|
normalizeClassname,
|
||||||
|
} from "./classname";
|
||||||
|
|
||||||
|
describe("getBaseClassname", () => {
|
||||||
|
it("strips the * suffix", () => {
|
||||||
|
expect(getBaseClassname("chair_basic*")).toBe("chair_basic");
|
||||||
|
});
|
||||||
|
it("returns the classname unchanged when no * is present", () => {
|
||||||
|
expect(getBaseClassname("chair_basic")).toBe("chair_basic");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("getSafeClassnameStem", () => {
|
||||||
|
it("replaces * with underscore", () => {
|
||||||
|
expect(getSafeClassnameStem("wall_plant*")).toBe("wall_plant_");
|
||||||
|
});
|
||||||
|
it("leaves plain classnames untouched", () => {
|
||||||
|
expect(getSafeClassnameStem("lamp_floor")).toBe("lamp_floor");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("file name helpers", () => {
|
||||||
|
it("builds icon file names from the safe stem", () => {
|
||||||
|
expect(getIconFileName("sofa_blue*")).toBe("sofa_blue__icon.png");
|
||||||
|
});
|
||||||
|
it("builds swf file names from the base classname", () => {
|
||||||
|
expect(getSwfFileName("sofa_blue*")).toBe("sofa_blue.swf");
|
||||||
|
});
|
||||||
|
it("builds nitro file names from the base classname", () => {
|
||||||
|
expect(getNitroFileName("sofa_blue*")).toBe("sofa_blue.nitro");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("normalizeClassname", () => {
|
||||||
|
it("trims and lowercases", () => {
|
||||||
|
expect(normalizeClassname(" Chair_Basic ")).toBe("chair_basic");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("isValidClassname", () => {
|
||||||
|
it("accepts lowercase letters, digits and allowed symbols", () => {
|
||||||
|
expect(isValidClassname("chair_basic")).toBe(true);
|
||||||
|
expect(isValidClassname("sofa*")).toBe(true);
|
||||||
|
expect(isValidClassname("wall.part-1")).toBe(true);
|
||||||
|
});
|
||||||
|
it("rejects uppercase, spaces and invalid characters", () => {
|
||||||
|
expect(isValidClassname("Chair")).toBe(false);
|
||||||
|
expect(isValidClassname("sofa basic")).toBe(false);
|
||||||
|
expect(isValidClassname("sofa/basic")).toBe(false);
|
||||||
|
expect(isValidClassname("")).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("icon URLs", () => {
|
||||||
|
it("builds a local icon URL with encoding", () => {
|
||||||
|
expect(getLocalIconUrl("sofa_blue")).toBe(
|
||||||
|
"/swf/dcr/hof_furni/icons/sofa_blue_icon.png",
|
||||||
|
);
|
||||||
|
expect(getLocalIconUrl("wall*")).toBe(
|
||||||
|
"/swf/dcr/hof_furni/icons/wall__icon.png",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it("builds a Habbo CDN icon URL with revision and base classname", () => {
|
||||||
|
expect(getHabboCdnIconUrl("sofa_blue*", 567)).toBe(
|
||||||
|
"https://images.habbo.com/dcr/hof_furni/567/sofa_blue_icon.png",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
+95
-29
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,54 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import { patchJson5 } from "./json5-patch";
|
||||||
|
|
||||||
|
const sample = `// header comment
|
||||||
|
{
|
||||||
|
// a section
|
||||||
|
"title": "Hotel",
|
||||||
|
"subtitle": "Welcome!",
|
||||||
|
'nested.key': "keep me",
|
||||||
|
}`;
|
||||||
|
|
||||||
|
describe("patchJson5", () => {
|
||||||
|
it("surgically replaces a value while preserving comments", () => {
|
||||||
|
const result = patchJson5(sample, { title: "Hotel" }, { title: "Resort" });
|
||||||
|
expect(result.unpatchedKeys).toEqual([]);
|
||||||
|
expect(result.content).toContain('"title": "Resort"');
|
||||||
|
expect(result.content).toContain("// a section");
|
||||||
|
expect(result.content).toContain('"subtitle": "Welcome!"');
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not touch keys whose value is unchanged", () => {
|
||||||
|
const result = patchJson5(sample, { title: "Hotel" }, { title: "Hotel" });
|
||||||
|
expect(result.unpatchedKeys).toEqual([]);
|
||||||
|
expect(result.content).toBe(sample);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("reports keys missing from the original", () => {
|
||||||
|
const result = patchJson5(sample, { title: "Hotel" }, { newKey: "x" });
|
||||||
|
expect(result.unpatchedKeys).toEqual(["newKey"]);
|
||||||
|
expect(result.content).toBe(sample);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("reports keys that cannot be located in the source", () => {
|
||||||
|
const result = patchJson5(sample, { title: "Hotel" }, { title: "nope" });
|
||||||
|
// "title" is present and findable, so it patches.
|
||||||
|
expect(result.unpatchedKeys).toEqual([]);
|
||||||
|
expect(result.content).toContain('"title": "nope"');
|
||||||
|
});
|
||||||
|
|
||||||
|
it("handles single-quoted keys and escaped values", () => {
|
||||||
|
const result = patchJson5(
|
||||||
|
sample,
|
||||||
|
{ "nested.key": "keep me" },
|
||||||
|
{ "nested.key": 'say "hi"' },
|
||||||
|
);
|
||||||
|
expect(result.unpatchedKeys).toEqual([]);
|
||||||
|
expect(result.content).toContain(`'nested.key': "say \\"hi\\""`);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("reports keys that do not appear in the file at all", () => {
|
||||||
|
const result = patchJson5(sample, { missing: "a" }, { missing: "b" });
|
||||||
|
expect(result.unpatchedKeys).toEqual(["missing"]);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,81 @@
|
|||||||
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
|
const STORAGE_KEY = "catalog_move_suggestions";
|
||||||
|
const store = new Map<string, string>();
|
||||||
|
|
||||||
|
vi.stubGlobal("localStorage", {
|
||||||
|
getItem: (k: string) => store.get(k) ?? null,
|
||||||
|
setItem: (k: string, v: string) => store.set(k, v),
|
||||||
|
});
|
||||||
|
|
||||||
|
import { getSuggestions, recordMove } from "./move-suggestions";
|
||||||
|
|
||||||
|
beforeEach(() => store.clear());
|
||||||
|
|
||||||
|
describe("recordMove", () => {
|
||||||
|
it("records by interaction type", () => {
|
||||||
|
recordMove({ interactionType: "clothing", targetPageId: 42 });
|
||||||
|
expect(getSuggestions({ interactionType: "clothing" })).toContain(42);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("records by item name prefix", () => {
|
||||||
|
recordMove({ itemName: "rare_sofa", targetPageId: 7 });
|
||||||
|
expect(getSuggestions({ itemName: "rare_table" })).toContain(7);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("records recent targets, deduplicated", () => {
|
||||||
|
recordMove({ targetPageId: 1 });
|
||||||
|
recordMove({ targetPageId: 2 });
|
||||||
|
recordMove({ targetPageId: 1 });
|
||||||
|
const recent = JSON.parse(store.get(STORAGE_KEY) ?? "") as {
|
||||||
|
recent: number[];
|
||||||
|
};
|
||||||
|
expect(recent.recent).toEqual([1, 2]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("caps recent at 10 entries", () => {
|
||||||
|
for (let i = 1; i <= 12; i++) recordMove({ targetPageId: i });
|
||||||
|
const recent = JSON.parse(store.get(STORAGE_KEY) ?? "") as {
|
||||||
|
recent: number[];
|
||||||
|
};
|
||||||
|
expect(recent.recent.length).toBe(10);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("increments counts and re-sorts", () => {
|
||||||
|
recordMove({ interactionType: "clothing", targetPageId: 1 });
|
||||||
|
recordMove({ interactionType: "clothing", targetPageId: 2 });
|
||||||
|
recordMove({ interactionType: "clothing", targetPageId: 1 });
|
||||||
|
const suggestions = getSuggestions({ interactionType: "clothing" });
|
||||||
|
expect(suggestions[0]).toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("handles a corrupt store gracefully", () => {
|
||||||
|
store.set(STORAGE_KEY, "not-json{");
|
||||||
|
expect(() =>
|
||||||
|
recordMove({ interactionType: "x", targetPageId: 3 }),
|
||||||
|
).not.toThrow();
|
||||||
|
expect(getSuggestions({ interactionType: "x" })).toContain(3);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("getSuggestions", () => {
|
||||||
|
it("ranks type matches above prefix matches", () => {
|
||||||
|
recordMove({ interactionType: "clothing", targetPageId: 1 });
|
||||||
|
recordMove({ itemName: "rare_x", targetPageId: 2 });
|
||||||
|
// same page both ways => type weight wins, but both present
|
||||||
|
recordMove({
|
||||||
|
interactionType: "clothing",
|
||||||
|
itemName: "rare_y",
|
||||||
|
targetPageId: 3,
|
||||||
|
});
|
||||||
|
const suggestions = getSuggestions({
|
||||||
|
interactionType: "clothing",
|
||||||
|
itemName: "rare_z",
|
||||||
|
});
|
||||||
|
expect(suggestions[0]).toBe(3);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns [] with no data", () => {
|
||||||
|
expect(getSuggestions({})).toEqual([]);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
import { describe, expect, it, vi } from "vitest";
|
||||||
|
import { z } from "zod";
|
||||||
|
|
||||||
|
const foundationHandle = vi.hoisted(() => vi.fn());
|
||||||
|
|
||||||
|
vi.mock("@/lib/foundation/action", () => ({
|
||||||
|
handleActionError: foundationHandle,
|
||||||
|
}));
|
||||||
|
|
||||||
|
import { actionError, actionOk, handleActionError } from "./safe-action-shared";
|
||||||
|
|
||||||
|
describe("actionOk", () => {
|
||||||
|
it("wraps data in a success result", () => {
|
||||||
|
expect(actionOk({ id: 1 })).toEqual({ ok: true, data: { id: 1 } });
|
||||||
|
});
|
||||||
|
it("defaults to an empty object when no data is given", () => {
|
||||||
|
expect(actionOk()).toEqual({ ok: true, data: {} });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("actionError", () => {
|
||||||
|
it("wraps a message in a failure result", () => {
|
||||||
|
expect(actionError("boom")).toEqual({ ok: false, error: "boom" });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("handleActionError", () => {
|
||||||
|
it("maps ZodErrors to validation failures with field errors", () => {
|
||||||
|
const schema = z.object({ name: z.string().min(3) });
|
||||||
|
const parsed = schema.safeParse({ name: "x" });
|
||||||
|
const result = handleActionError(parsed.success ? undefined : parsed.error);
|
||||||
|
expect(result.ok).toBe(false);
|
||||||
|
if (!result.ok) {
|
||||||
|
expect(result.error).toBe("Validation failed");
|
||||||
|
expect(result.fieldErrors?.name).toBeTruthy();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("maps ActionError instances by name", () => {
|
||||||
|
const err = new Error("not allowed");
|
||||||
|
err.name = "ActionError";
|
||||||
|
expect(handleActionError(err)).toEqual({
|
||||||
|
ok: false,
|
||||||
|
error: "not allowed",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("delegates unknown errors to the foundation handler", () => {
|
||||||
|
foundationHandle.mockReturnValue({ ok: false, error: "generic" });
|
||||||
|
const result = handleActionError(new Error("generic"));
|
||||||
|
expect(foundationHandle).toHaveBeenCalled();
|
||||||
|
expect(result.ok).toBe(false);
|
||||||
|
if (!result.ok) {
|
||||||
|
expect(result.error).toBe("generic");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -2,15 +2,19 @@ import { describe, expect, it } from "vitest";
|
|||||||
import { CurrencyType, currencyLabel } from "./currency";
|
import { CurrencyType, currencyLabel } from "./currency";
|
||||||
|
|
||||||
describe("CurrencyType", () => {
|
describe("CurrencyType", () => {
|
||||||
it("uses the emulator-fixed numeric values", () => {
|
it("keeps emulator-dictated numeric values", () => {
|
||||||
expect(CurrencyType.Credits).toBe(-1);
|
expect(CurrencyType.Credits).toBe(-1);
|
||||||
expect(CurrencyType.Duckets).toBe(0);
|
expect(CurrencyType.Duckets).toBe(0);
|
||||||
expect(CurrencyType.Diamonds).toBe(5);
|
expect(CurrencyType.Diamonds).toBe(5);
|
||||||
expect(CurrencyType.Points).toBe(101);
|
expect(CurrencyType.Points).toBe(101);
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it("maps each type to a human label", () => {
|
describe("currencyLabel", () => {
|
||||||
|
it("returns friendly labels for each currency", () => {
|
||||||
expect(currencyLabel(CurrencyType.Credits)).toBe("Credits");
|
expect(currencyLabel(CurrencyType.Credits)).toBe("Credits");
|
||||||
|
expect(currencyLabel(CurrencyType.Duckets)).toBe("Duckets");
|
||||||
expect(currencyLabel(CurrencyType.Diamonds)).toBe("Diamonds");
|
expect(currencyLabel(CurrencyType.Diamonds)).toBe("Diamonds");
|
||||||
|
expect(currencyLabel(CurrencyType.Points)).toBe("Points");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,36 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import { browserHeaders } from "./browser-headers";
|
||||||
|
|
||||||
|
describe("browserHeaders", () => {
|
||||||
|
it("defaults to empty destination headers", () => {
|
||||||
|
const h = browserHeaders();
|
||||||
|
expect(h["User-Agent"]).toContain("Chrome/125");
|
||||||
|
expect(h["Sec-Fetch-Mode"]).toBe("cors");
|
||||||
|
expect(h["Sec-Fetch-Dest"]).toBe("empty");
|
||||||
|
expect(h["Upgrade-Insecure-Requests"]).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("uses navigate mode for documents", () => {
|
||||||
|
const h = browserHeaders("document");
|
||||||
|
expect(h["Sec-Fetch-Mode"]).toBe("navigate");
|
||||||
|
expect(h["Sec-Fetch-Dest"]).toBe("document");
|
||||||
|
expect(h["Upgrade-Insecure-Requests"]).toBe("1");
|
||||||
|
expect(h.Accept).toContain("text/html");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("uses no-cors mode for images", () => {
|
||||||
|
const h = browserHeaders("image");
|
||||||
|
expect(h["Sec-Fetch-Mode"]).toBe("no-cors");
|
||||||
|
expect(h.Accept).toContain("image/avif");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("uses wildcard accept for scripts", () => {
|
||||||
|
expect(browserHeaders("script").Accept).toBe("*/*");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("merges extra headers last", () => {
|
||||||
|
const h = browserHeaders("empty", { "User-Agent": "custom" });
|
||||||
|
expect(h["User-Agent"]).toBe("custom");
|
||||||
|
expect(h.Accept).toContain("application/json");
|
||||||
|
});
|
||||||
|
});
|
||||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,79 @@
|
|||||||
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
|
vi.mock("@/lib/services/site-settings", () => ({
|
||||||
|
siteSettings: { get: vi.fn() },
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock("@/lib/services/import/core/gordon", () => ({
|
||||||
|
resolveGordonBuildUrl: vi.fn(),
|
||||||
|
}));
|
||||||
|
|
||||||
|
import { resolveGordonBuildUrl } from "@/lib/services/import/core/gordon";
|
||||||
|
import { siteSettings } from "@/lib/services/site-settings";
|
||||||
|
import { resolveFigureSwfUrl } from "./figure-source";
|
||||||
|
import { CANONICAL_PETS, resolvePetSwfUrl } from "./pet-source";
|
||||||
|
|
||||||
|
describe("resolveFigureSwfUrl", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.mocked(siteSettings.get).mockReset();
|
||||||
|
vi.mocked(resolveGordonBuildUrl).mockReset();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("uses override when set, trimming trailing slashes", async () => {
|
||||||
|
vi.mocked(siteSettings.get).mockResolvedValue(
|
||||||
|
"https://cdn.example.com/swf/",
|
||||||
|
);
|
||||||
|
await expect(resolveFigureSwfUrl("h_std_15")).resolves.toBe(
|
||||||
|
"https://cdn.example.com/swf/h_std_15.swf",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("falls back to gordon build URL when no override", async () => {
|
||||||
|
vi.mocked(siteSettings.get).mockResolvedValue("");
|
||||||
|
vi.mocked(resolveGordonBuildUrl).mockResolvedValue(
|
||||||
|
"https://www.habbo.com/gordon/",
|
||||||
|
);
|
||||||
|
await expect(resolveFigureSwfUrl("h_std_15")).resolves.toBe(
|
||||||
|
"https://www.habbo.com/gordon/h_std_15.swf",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("resolvePetSwfUrl", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.mocked(siteSettings.get).mockReset();
|
||||||
|
vi.mocked(resolveGordonBuildUrl).mockReset();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("uses override when set", async () => {
|
||||||
|
vi.mocked(siteSettings.get).mockResolvedValue(
|
||||||
|
"https://cdn.example.com/pets",
|
||||||
|
);
|
||||||
|
await expect(resolvePetSwfUrl("dog")).resolves.toBe(
|
||||||
|
"https://cdn.example.com/pets/dog.swf",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("falls back to gordon build URL", async () => {
|
||||||
|
vi.mocked(siteSettings.get).mockResolvedValue(null);
|
||||||
|
vi.mocked(resolveGordonBuildUrl).mockResolvedValue(
|
||||||
|
"https://www.habbo.com/gordon/",
|
||||||
|
);
|
||||||
|
await expect(resolvePetSwfUrl("cat")).resolves.toBe(
|
||||||
|
"https://www.habbo.com/gordon/cat.swf",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("CANONICAL_PETS", () => {
|
||||||
|
it("includes the classic pet set", () => {
|
||||||
|
expect(CANONICAL_PETS).toContain("dog");
|
||||||
|
expect(CANONICAL_PETS).toContain("cat");
|
||||||
|
expect(CANONICAL_PETS).toContain("horse");
|
||||||
|
expect(CANONICAL_PETS).toContain("monsterplant");
|
||||||
|
expect(CANONICAL_PETS).toContain("bunnyeaster");
|
||||||
|
});
|
||||||
|
it("has no duplicates", () => {
|
||||||
|
expect(new Set(CANONICAL_PETS).size).toBe(CANONICAL_PETS.length);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import { flattenObject, unflattenObject } from "./translations-utils";
|
||||||
|
|
||||||
|
describe("flattenObject", () => {
|
||||||
|
it("flattens nested objects into dotted keys", () => {
|
||||||
|
const flat = flattenObject({ a: { b: { c: "x" } }, d: "y" });
|
||||||
|
expect(flat).toEqual({ "a.b.c": "x", d: "y" });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("stringifies non-object values", () => {
|
||||||
|
expect(flattenObject({ n: 5, b: true, nil: null })).toEqual({
|
||||||
|
n: "5",
|
||||||
|
b: "true",
|
||||||
|
nil: "",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps arrays as-is via String()", () => {
|
||||||
|
expect(flattenObject({ list: ["a", "b"] })).toEqual({ list: "a,b" });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("honours an explicit prefix", () => {
|
||||||
|
expect(flattenObject({ a: 1 }, "root")).toEqual({ "root.a": "1" });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("unflattenObject", () => {
|
||||||
|
it("rebuilds nested objects from dotted keys", () => {
|
||||||
|
expect(unflattenObject({ "a.b.c": "x", d: "y" })).toEqual({
|
||||||
|
a: { b: { c: "x" } },
|
||||||
|
d: "y",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("creates intermediate objects as needed", () => {
|
||||||
|
expect(unflattenObject({ "x.y": "1", "x.z": "2" })).toEqual({
|
||||||
|
x: { y: "1", z: "2" },
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("round-trips with flattenObject", () => {
|
||||||
|
const original = { menu: { home: "Home", items: { a: "A" } }, footer: "F" };
|
||||||
|
expect(unflattenObject(flattenObject(original))).toEqual(original);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import {
|
||||||
|
detectSoundtrackKind,
|
||||||
|
parseTrackSamples,
|
||||||
|
parseTraxChannels,
|
||||||
|
TRAX_TICK_SECONDS,
|
||||||
|
} from "./trax-format";
|
||||||
|
|
||||||
|
describe("parseTraxChannels", () => {
|
||||||
|
it("returns [] for empty or null tracks", () => {
|
||||||
|
expect(parseTraxChannels("")).toEqual([]);
|
||||||
|
expect(parseTraxChannels(null)).toEqual([]);
|
||||||
|
expect(parseTraxChannels(undefined)).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("parses per-channel event lists", () => {
|
||||||
|
const channels = parseTraxChannels("1:10,2;0,1:2:5,3");
|
||||||
|
expect(channels).toEqual([
|
||||||
|
{
|
||||||
|
channel: 1,
|
||||||
|
events: [
|
||||||
|
{ sampleId: 10, ticks: 2 },
|
||||||
|
{ sampleId: 0, ticks: 1 },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{ channel: 2, events: [{ sampleId: 5, ticks: 3 }] },
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("skips malformed event pairs", () => {
|
||||||
|
const channels = parseTraxChannels("1:10,2;garbage");
|
||||||
|
expect(channels).toEqual([
|
||||||
|
{ channel: 1, events: [{ sampleId: 10, ticks: 2 }] },
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("drops channels with no valid events", () => {
|
||||||
|
expect(parseTraxChannels("1:junk")).toEqual([]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("parseTrackSamples", () => {
|
||||||
|
it("collects unique non-zero sample ids, sorted", () => {
|
||||||
|
expect(parseTrackSamples("1:5,1;0,2:2:3,4;5,2")).toEqual([3, 5]);
|
||||||
|
});
|
||||||
|
it("returns [] for silence-only tracks", () => {
|
||||||
|
expect(parseTrackSamples("1:0,3")).toEqual([]);
|
||||||
|
});
|
||||||
|
it("returns [] for empty or unparseable tracks", () => {
|
||||||
|
expect(parseTrackSamples("")).toEqual([]);
|
||||||
|
expect(parseTrackSamples(null)).toEqual([]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("detectSoundtrackKind", () => {
|
||||||
|
it("classifies a single-sample song as 'single'", () => {
|
||||||
|
expect(detectSoundtrackKind("1:0,24")).toBe("single");
|
||||||
|
expect(detectSoundtrackKind("")).toBe("single");
|
||||||
|
});
|
||||||
|
it("classifies multi-sample tracks as 'composition'", () => {
|
||||||
|
expect(detectSoundtrackKind("1:10,2;20,3")).toBe("composition");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("TRAX_TICK_SECONDS", () => {
|
||||||
|
it("is 2 seconds per tick", () => {
|
||||||
|
expect(TRAX_TICK_SECONDS).toBe(2);
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in new issue
Block a user