From f795951443280e7c11a5f205d8eef1e3364c4778 Mon Sep 17 00:00:00 2001 From: adilallo <39313955+adilallo@users.noreply.github.com> Date: Mon, 23 Nov 2026 10:40:09 -0700 Subject: [PATCH] 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. --- lib/create/deckRecommend.ts | 17 ++++--- lib/create/groupAnswers.ts | 33 +++++++++---- tests/unit/deckRecommend.test.ts | 62 ++++++++++++++++++++++++ tests/unit/groupAnswers.test.ts | 9 ++++ tests/unit/methodRecommendations.test.ts | 11 +++++ 5 files changed, 116 insertions(+), 16 deletions(-) create mode 100644 tests/unit/deckRecommend.test.ts diff --git a/lib/create/deckRecommend.ts b/lib/create/deckRecommend.ts index 805bbd2..d2336c8 100644 --- a/lib/create/deckRecommend.ts +++ b/lib/create/deckRecommend.ts @@ -12,6 +12,7 @@ import jobsFile from "../../data/create/customRule/_jobs.json"; import membershipFacets from "../../data/create/customRule/membership.json"; import type { CardEngineEntry, + CardFacets, DeckEngine, DeckId, DeckRecommendation, @@ -53,20 +54,24 @@ interface Evaluated { otherKindHits: number; } -/** Normalizes setup answers. A group with other kinds and no main kind gets the first as main. */ +/** A typed or missing main kind stays unset so eligibility falls back to size. */ export function normalizeAnswers(answers: GroupAnswers): Required { - const others = [...(answers.otherKinds ?? [])]; - const mainKind = answers.mainKind ?? others.shift() ?? null; + const mainKind = answers.mainKind ?? null; return { size: answers.size ?? null, mainKind, - otherKinds: others.filter((k) => k !== mainKind), + otherKinds: [...(answers.otherKinds ?? [])].filter((k) => k !== mainKind), scale: [...new Set(answers.scale ?? [])], stage: answers.stage ?? null, traits: answers.traits ?? {}, }; } +function kindFits(orgType: CardFacets["orgType"], kind: Kind | null): boolean { + if (!kind || !(kind in orgType)) return true; + return orgType[kind]; +} + function sizeBandOf(size: string | null, data: DeckData): SizeBand | null { if (!size) return null; const band = data.config.sizeAnswers[size]; @@ -86,7 +91,7 @@ function evaluate( if (!f || !entry) throw new Error(`Card ${id} is missing from the facet or jobs file`); const traits = entry.traits ?? {}; const fitsSize = band ? f.size[band] : true; - const fitsKind = a.mainKind ? f.orgType[a.mainKind] : true; + const fitsKind = kindFits(f.orgType, a.mainKind); const openedBy = (traits.opens ?? []).find((k) => ticked.has(k)); const isBeside = (traits.beside ?? []).length > 0; // Each facet group counts once. Scale earns its point only if the card fits @@ -96,7 +101,7 @@ function evaluate( const fitsAnswer = (traits.fits ?? []).find((k) => ticked.has(k)); const matched: MatchedFacet[] = []; if (band && fitsSize) matched.push("size"); - if (a.mainKind && fitsKind) matched.push("kind"); + if (a.mainKind && a.mainKind in f.orgType && fitsKind) matched.push("kind"); if (fitsScale) matched.push("scale"); if (fitsStage) matched.push("stage"); return { diff --git a/lib/create/groupAnswers.ts b/lib/create/groupAnswers.ts index 188e172..88df8a5 100644 --- a/lib/create/groupAnswers.ts +++ b/lib/create/groupAnswers.ts @@ -9,8 +9,21 @@ import { import type { GroupAnswers, Kind, Scale, Stage, TraitKey } from "./deckTypes"; import { KINDS, SCALES, STAGES, TRAIT_KEYS } from "./deckTypes"; -function isKind(value: string): value is Kind { - return (KINDS as readonly string[]).includes(value); +function isKind(value: string | null | undefined): value is Kind { + return value != null && (KINDS as readonly string[]).includes(value); +} + +/** First id is the main kind only when it is a catalog kind. A typed kind is no kind. */ +function kindsFromOrderedIds(ids: readonly string[]): { + mainKind: Kind | null; + otherKinds: Kind[]; +} { + const mainKind = isKind(ids[0]) ? ids[0] : null; + const otherKinds = ids + .slice(1) + .filter(isKind) + .filter((kind, index, all) => kind !== mainKind && all.indexOf(kind) === index); + return { mainKind, otherKinds }; } function isScale(value: string): value is Scale { @@ -36,15 +49,15 @@ export function groupAnswersFromRequestedFacets( facets: RequestedFacets, traits?: Partial>, ): GroupAnswers { - const kinds = (facets.orgType ?? []).filter(isKind); + const { mainKind, otherKinds } = kindsFromOrderedIds(facets.orgType ?? []); const scales = (facets.scale ?? []).filter(isScale); const maturity = (facets.maturity ?? []) .map((value) => stageForScoring(value)) .filter((value): value is Stage => value !== null); return { size: facets.size?.[0] ?? null, - mainKind: kinds[0] ?? null, - otherKinds: kinds.slice(1), + mainKind, + otherKinds, scale: scales, stage: maturity[0] ?? null, traits, @@ -66,9 +79,9 @@ export function groupAnswersFromCreateFlowState( state: CreateFlowState, ): GroupAnswers { const size = (state.selectedCommunitySizeIds ?? []).map(resolveSizeChipId)[0] ?? null; - const kinds = (state.selectedOrganizationTypeIds ?? []) - .map((id) => resolveOrgChipId(id)) - .filter(isKind); + const { mainKind, otherKinds } = kindsFromOrderedIds( + (state.selectedOrganizationTypeIds ?? []).map((id) => resolveOrgChipId(id)), + ); const scale = (state.selectedScaleIds ?? []) .map(resolveScaleChipId) .filter(isScale); @@ -78,8 +91,8 @@ export function groupAnswersFromCreateFlowState( const uniqueScale = [...new Set(scale)]; return { size, - mainKind: kinds[0] ?? null, - otherKinds: kinds.slice(1).filter((kind, index, all) => all.indexOf(kind) === index), + mainKind, + otherKinds, scale: uniqueScale, stage: maturity[0] ?? null, traits: traitRecord(state.setupTraits), diff --git a/tests/unit/deckRecommend.test.ts b/tests/unit/deckRecommend.test.ts new file mode 100644 index 0000000..a4598c0 --- /dev/null +++ b/tests/unit/deckRecommend.test.ts @@ -0,0 +1,62 @@ +import { describe, expect, it } from "vitest"; +import { deckData, recommendDeck } from "../../lib/create/deckRecommend"; +import type { Kind } from "../../lib/create/deckTypes"; + +describe("recommendDeck typed main kind", () => { + const sizeOnly = { + size: "sixToTwelve", + mainKind: null, + }; + + it("treats a typed main kind as no kind so eligibility falls back to size", () => { + const typed = recommendDeck(deckData("decisionApproaches"), { + size: "sixToTwelve", + mainKind: "streetMedicCrew" as Kind, + }); + const none = recommendDeck(deckData("decisionApproaches"), sizeOnly); + const coop = recommendDeck(deckData("decisionApproaches"), { + size: "sixToTwelve", + mainKind: "workersCoop", + }); + + expect(typed.fallback).toBe(false); + expect(typed.recommended.map((card) => card.id)).toEqual( + none.recommended.map((card) => card.id), + ); + expect( + typed.recommended.some( + (card) => + (card.reason.kind === "fit" || card.reason.kind === "opened") && + card.reason.matched.includes("kind"), + ), + ).toBe(false); + expect( + coop.recommended.some( + (card) => + (card.reason.kind === "fit" || card.reason.kind === "opened") && + card.reason.matched.includes("kind"), + ), + ).toBe(true); + }); + + it("keeps other catalog kinds as ranking hints, not as the main kind", () => { + const typedWithOther = recommendDeck(deckData("decisionApproaches"), { + size: "sixToTwelve", + mainKind: null, + otherKinds: ["workersCoop"], + }); + const none = recommendDeck(deckData("decisionApproaches"), sizeOnly); + + expect(typedWithOther.fallback).toBe(false); + expect(new Set(typedWithOther.recommended.map((card) => card.job))).toEqual( + new Set(none.recommended.map((card) => card.job)), + ); + expect( + typedWithOther.recommended.some( + (card) => + (card.reason.kind === "fit" || card.reason.kind === "opened") && + card.reason.matched.includes("kind"), + ), + ).toBe(false); + }); +}); diff --git a/tests/unit/groupAnswers.test.ts b/tests/unit/groupAnswers.test.ts index 557dde5..5264f27 100644 --- a/tests/unit/groupAnswers.test.ts +++ b/tests/unit/groupAnswers.test.ts @@ -19,6 +19,15 @@ describe("maturity and chip aliases", () => { expect(answers.mainKind).toBe("workersCoop"); }); + it("treats a typed main kind as no kind and keeps catalog kinds as other kinds", () => { + const answers = groupAnswersFromCreateFlowState({ + selectedCommunitySizeIds: ["sixToTwelve"], + selectedOrganizationTypeIds: ["custom-tenants-union", "workersCoop"], + }); + expect(answers.mainKind).toBeNull(); + expect(answers.otherKinds).toEqual(["workersCoop"]); + }); + it("passes ticked setup answers through as traits", () => { const answers = groupAnswersFromCreateFlowState({ setupTraits: { employer: true, records: false }, diff --git a/tests/unit/methodRecommendations.test.ts b/tests/unit/methodRecommendations.test.ts index 3e9dda0..58b52bb 100644 --- a/tests/unit/methodRecommendations.test.ts +++ b/tests/unit/methodRecommendations.test.ts @@ -55,6 +55,17 @@ describe("listMethodRecommendations", () => { 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",