Files
community-rule/tests/unit/methodRecommendations.test.ts
T
adilallo f795951443 Treat a typed main kind as no kind so recommendations still fall back to size.
A custom kind no longer empties a job of eligible cards, and other catalog kinds stay ranking hints instead of becoming the main kind.
2026-11-23 10:40:09 -07:00

157 lines
4.3 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from "vitest";
const findManyMock = vi.fn();
vi.mock("../../lib/server/env", () => ({
isDatabaseConfigured: () => true,
}));
vi.mock("../../lib/server/db", () => ({
prisma: {
methodFacet: {
findMany: (...args: unknown[]) => findManyMock(...args),
},
},
}));
import {
listMethodRecommendations,
scoreTemplatesByFacets,
} from "../../lib/server/methodRecommendations";
beforeEach(() => {
findManyMock.mockReset();
});
describe("listMethodRecommendations", () => {
it("returns empty rankings when no facets are requested", async () => {
const result = await listMethodRecommendations({
section: "communication",
facets: {},
});
expect(result).toEqual({ rankedSlugs: [], matchesBySlug: {} });
expect(findManyMock).not.toHaveBeenCalled();
});
it("ranks from the job rule and scores maturity once", async () => {
const once = await listMethodRecommendations({
section: "decisionApproaches",
facets: {
size: ["thirteenToOneHundred"],
orgType: ["workersCoop"],
maturity: ["established"],
},
});
const several = await listMethodRecommendations({
section: "decisionApproaches",
facets: {
size: ["thirteenToOneHundred"],
orgType: ["workersCoop"],
maturity: ["established", "earlyStage", "enterprise"],
},
});
expect(once?.rankedSlugs).toEqual(several?.rankedSlugs);
expect(once?.rankedSlugs[0]).toBeTruthy();
expect(findManyMock).not.toHaveBeenCalled();
});
it("still ranks when only size is set and the main kind is missing", async () => {
const result = await listMethodRecommendations({
section: "decisionApproaches",
facets: {
size: ["sixToTwelve"],
},
});
expect(result?.rankedSlugs.length).toBeGreaterThan(0);
expect(findManyMock).not.toHaveBeenCalled();
});
it("uses only the main kind for eligibility", async () => {
const result = await listMethodRecommendations({
section: "decisionApproaches",
facets: {
size: ["thirteenToOneHundred"],
orgType: ["workersCoop", "forProfit"],
scale: ["local"],
maturity: ["established"],
},
});
expect(result?.rankedSlugs).toContain("consent-sociocracy");
expect(findManyMock).not.toHaveBeenCalled();
});
});
describe("scoreTemplatesByFacets (CR-88 §9.1)", () => {
it("aggregates per-method matches per template", async () => {
findManyMock.mockResolvedValueOnce([
{
section: "communication",
slug: "loomio",
group: "size",
value: "twoToFive",
},
{
section: "decisionApproaches",
slug: "consensus-decision-making",
group: "orgType",
value: "workersCoop",
},
]);
const result = await scoreTemplatesByFacets({
facets: {
size: ["twoToFive"],
orgType: ["workersCoop"],
},
templateMethods: [
{
templateSlug: "consensus",
methods: [
{ section: "communication", slug: "loomio" },
{
section: "decisionApproaches",
slug: "consensus-decision-making",
},
],
},
{
templateSlug: "monarch",
methods: [
{
section: "decisionApproaches",
slug: "benevolent-dictator",
},
],
},
],
});
expect(result).toEqual([
{
templateSlug: "consensus",
score: 2,
matchedFacets: [
"communication:loomio:size:twoToFive",
"decisionApproaches:consensus-decision-making:orgType:workersCoop",
],
},
{ templateSlug: "monarch", score: 0, matchedFacets: [] },
]);
});
it("returns 0-score entries for every template when facets are empty", async () => {
const result = await scoreTemplatesByFacets({
facets: {},
templateMethods: [
{ templateSlug: "consensus", methods: [] },
{ templateSlug: "monarch", methods: [] },
],
});
expect(result).toEqual([
{ templateSlug: "consensus", score: 0, matchedFacets: [] },
{ templateSlug: "monarch", score: 0, matchedFacets: [] },
]);
expect(findManyMock).not.toHaveBeenCalled();
});
});