Keep beta Rules at cutover, fall back when a kind is typed, and replace the terms license claim #115
No files matched your search
@@ -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<GroupAnswers> {
|
||||
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 {
|
||||
|
||||
+23
-10
@@ -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<Record<TraitKey, boolean>>,
|
||||
): 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),
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
@@ -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 },
|
||||
|
||||
@@ -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",
|
||||
|
||||
Reference in new issue
Block a user