Backend modules from tastefully 8bd93d15: core-libs, core-session, api-gateway, mobile-api, services-auth, services-system, services-counter, services-member, services-log, services-email. solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal from tastefully 8bd93d15. Copied as tracked files only. Literal secrets replaced with environment placeholders: - services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY, MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default) - services-system: JUMIO_TOKEN, JUMIO_SECRET (optional) - services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed - core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default removed, ipgeolocation key read from IPGEOLOCATION_API_KEY - services-system EkycAppConfig: commented Jumio credentials removed Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
183 lines
6.5 KiB
TypeScript
183 lines
6.5 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
||
import { tEn } from "@/test/i18nCatalog";
|
||
import {
|
||
collectionBody,
|
||
collectionFromView,
|
||
emptyCollection,
|
||
moveProduct,
|
||
previewBody,
|
||
slugify,
|
||
validateCollection,
|
||
withTitle,
|
||
withType,
|
||
type CollectionDraft,
|
||
type CollectionView,
|
||
} from "./collections";
|
||
|
||
function draft(overrides: Partial<CollectionDraft> = {}): CollectionDraft {
|
||
return { ...emptyCollection("MANUAL"), title: "Our picks", slug: "our-picks", ...overrides };
|
||
}
|
||
|
||
describe("slugify", () => {
|
||
it("lowercases, joins words with single hyphens and drops other characters", () => {
|
||
expect(slugify(" 6–18 Months & Up! ")).toBe("6-18-months-up");
|
||
expect(slugify("Café Crème")).toBe("cafe-creme");
|
||
expect(slugify("---")).toBe("");
|
||
});
|
||
|
||
it("is at most 80 characters and never ends with a hyphen", () => {
|
||
const slug = slugify(`${"a".repeat(79)} b`);
|
||
expect(slug.length).toBeLessThanOrEqual(80);
|
||
expect(slug.endsWith("-")).toBe(false);
|
||
});
|
||
});
|
||
|
||
describe("drafts", () => {
|
||
it("a new collection is active, manual order, with nothing in it", () => {
|
||
expect(emptyCollection("MANUAL")).toMatchObject({ status: "Y", type: "MANUAL", sort: "MANUAL", match: "ALL", rules: [], products: [] });
|
||
});
|
||
|
||
it("a new automatic collection starts newest-first with one empty tag rule", () => {
|
||
expect(emptyCollection("AUTOMATIC")).toMatchObject({ type: "AUTOMATIC", sort: "NEWEST", rules: [{ field: "TAG", value: "" }] });
|
||
});
|
||
|
||
it("the slug follows the title until it is edited by hand", () => {
|
||
const followed = withTitle(emptyCollection("MANUAL"), "Newborn Essentials");
|
||
expect(followed.slug).toBe("newborn-essentials");
|
||
const touched = withTitle({ ...followed, slug: "newborn", slugTouched: true }, "Newborn Gear");
|
||
expect(touched.slug).toBe("newborn");
|
||
expect(touched.title).toBe("Newborn Gear");
|
||
});
|
||
|
||
it("switching to automatic leaves the manual order; switching to manual uses it", () => {
|
||
expect(withType(emptyCollection("MANUAL"), "AUTOMATIC")).toMatchObject({ type: "AUTOMATIC", sort: "NEWEST" });
|
||
expect(withType({ ...emptyCollection("AUTOMATIC"), sort: "PRICE_ASC" }, "AUTOMATIC").sort).toBe("PRICE_ASC");
|
||
expect(withType({ ...emptyCollection("AUTOMATIC"), sort: "PRICE_ASC" }, "MANUAL").sort).toBe("MANUAL");
|
||
});
|
||
|
||
it("loads a saved collection with its slug treated as hand-edited", () => {
|
||
const view: CollectionView = {
|
||
id: 5,
|
||
slug: "newborn",
|
||
title: "Newborn",
|
||
description: null,
|
||
imageUrl: "https://cdn/n.jpg",
|
||
type: "AUTOMATIC",
|
||
match: "ANY",
|
||
sort: "PRICE_ASC",
|
||
status: "N",
|
||
metaDescription: "Meta",
|
||
rules: [{ field: "BRAND", value: "3" }],
|
||
products: [],
|
||
};
|
||
|
||
expect(collectionFromView(view)).toMatchObject({
|
||
id: 5,
|
||
slug: "newborn",
|
||
slugTouched: true,
|
||
description: "",
|
||
imageUrl: "https://cdn/n.jpg",
|
||
match: "ANY",
|
||
status: "N",
|
||
metaDescription: "Meta",
|
||
rules: [{ field: "BRAND", value: "3" }],
|
||
});
|
||
});
|
||
|
||
it("moves a product up or down and ignores moves past either end", () => {
|
||
const products = [
|
||
{ id: 1, code: "A", name: "A" },
|
||
{ id: 2, code: "B", name: "B" },
|
||
{ id: 3, code: "C", name: "C" },
|
||
];
|
||
expect(moveProduct(products, 2, -1).map((p) => p.code)).toEqual(["A", "C", "B"]);
|
||
expect(moveProduct(products, 0, 1).map((p) => p.code)).toEqual(["B", "A", "C"]);
|
||
expect(moveProduct(products, 0, -1)).toBe(products);
|
||
expect(moveProduct(products, 2, 1)).toBe(products);
|
||
});
|
||
});
|
||
|
||
describe("request bodies", () => {
|
||
it("a manual collection sends its product ids in order and no rules", () => {
|
||
const body = collectionBody(
|
||
draft({
|
||
id: 9,
|
||
title: " Our picks ",
|
||
description: " Hand picked ",
|
||
metaDescription: "",
|
||
rules: [{ field: "TAG", value: "x" }],
|
||
products: [
|
||
{ id: 3, code: "C", name: "C" },
|
||
{ id: 1, code: "A", name: "A" },
|
||
],
|
||
}),
|
||
);
|
||
|
||
expect(body).toEqual({
|
||
id: 9,
|
||
slug: "our-picks",
|
||
title: "Our picks",
|
||
description: "Hand picked",
|
||
imageUrl: undefined,
|
||
type: "MANUAL",
|
||
match: "ALL",
|
||
sort: "MANUAL",
|
||
status: "Y",
|
||
metaDescription: undefined,
|
||
rules: [],
|
||
productIds: [3, 1],
|
||
});
|
||
});
|
||
|
||
it("an automatic collection sends its rules and no products", () => {
|
||
const body = collectionBody(
|
||
draft({
|
||
type: "AUTOMATIC",
|
||
sort: "NAME",
|
||
match: "ANY",
|
||
rules: [
|
||
{ field: "BRAND", value: "3" },
|
||
{ field: "TAG", value: " Newborn " },
|
||
],
|
||
products: [{ id: 1, code: "A", name: "A" }],
|
||
}),
|
||
);
|
||
|
||
expect(body.rules).toEqual([
|
||
{ field: "BRAND", value: "3" },
|
||
{ field: "TAG", value: "Newborn" },
|
||
]);
|
||
expect(body.productIds).toEqual([]);
|
||
expect(body.match).toBe("ANY");
|
||
});
|
||
|
||
it("the preview leaves out rules that have no value yet", () => {
|
||
const body = previewBody(draft({ type: "AUTOMATIC", sort: "NAME", rules: [{ field: "BRAND", value: "" }, { field: "TAG", value: "newborn" }] }));
|
||
|
||
expect(body.rules).toEqual([{ field: "TAG", value: "newborn" }]);
|
||
});
|
||
});
|
||
|
||
describe("client validation", () => {
|
||
it("a complete manual draft has no errors", () => {
|
||
expect(validateCollection(draft(), tEn)).toEqual({});
|
||
});
|
||
|
||
it("title, slug and meta description are checked", () => {
|
||
expect(validateCollection(draft({ title: " " }), tEn).title).toBe("Title is required");
|
||
expect(validateCollection(draft({ title: "x".repeat(121) }), tEn).title).toMatch(/120/);
|
||
expect(validateCollection(draft({ slug: "Bad Slug" }), tEn).slug).toMatch(/lowercase/);
|
||
expect(validateCollection(draft({ slug: "" }), tEn).slug).toMatch(/lowercase/);
|
||
expect(validateCollection(draft({ slug: "all" }), tEn).slug).toMatch(/reserved/);
|
||
expect(validateCollection(draft({ metaDescription: "x".repeat(161) }), tEn).metaDescription).toMatch(/160/);
|
||
});
|
||
|
||
it("an automatic draft needs a value on every rule and one to ten rules", () => {
|
||
const missing = validateCollection(draft({ type: "AUTOMATIC", sort: "NAME", rules: [{ field: "TAG", value: "a" }, { field: "BRAND", value: "" }] }), tEn);
|
||
expect(missing["rules[1].value"]).toBe("Choose a value");
|
||
expect(validateCollection(draft({ type: "AUTOMATIC", sort: "NAME", rules: [] }), tEn).rules).toMatch(/at least one/);
|
||
const eleven = Array.from({ length: 11 }, (_, i) => ({ field: "TAG" as const, value: `t${i}` }));
|
||
expect(validateCollection(draft({ type: "AUTOMATIC", sort: "NAME", rules: eleven }), tEn).rules).toMatch(/10/);
|
||
});
|
||
});
|