Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

fix(presets): substitute the default categories on emptiness, as Python's `or` does

Aashish70b32ae34deb
diff --git a/laya-ts/src/presets.ts b/laya-ts/src/presets.ts
index 0acd426b..bbd007c9 100644
--- a/laya-ts/src/presets.ts
+++ b/laya-ts/src/presets.ts
@@ -38,7 +38,14 @@ export function triageQuestions(): Record<string, unknown> {
}
export function emailQuestions(categories?: Record<string, string>): Record<string, unknown> {
- const cats: Record<string, string> = categories ?? {
+ // Python substitutes on truthiness -- `categories or {…defaults}` -- so a caller whose
+ // categories come out empty gets the six default teams, not a choice question with no
+ // options to choose from. An empty object is *truthy* in JavaScript, so neither `??` nor `||`
+ // ports that test: emptiness has to be asked about directly. `laya-java/fixtures/presets.json`
+ // records `email_questions_empty_categories` as literally `email_questions({})`, and the Java
+ // port already holds the reference to it.
+ const cats: Record<string, string> =
+ categories && Object.keys(categories).length > 0 ? categories : {
billing: "invoices, payments, refunds",
technical: "bugs, outages, integrations",
sales: "pricing, demos, new purchases",
diff --git a/laya-ts/tests/presets-parity.test.ts b/laya-ts/tests/presets-parity.test.ts
new file mode 100644
index 00000000..6ccbe2da
--- /dev/null
+++ b/laya-ts/tests/presets-parity.test.ts
@@ -0,0 +1,74 @@
+import { readFileSync } from "node:fs";
+import { describe, expect, it } from "vitest";
+import {
+ emailQuestions,
+ guardQuestions,
+ moderationQuestions,
+ routerQuestions,
+ triageQuestions,
+} from "../src/presets.js";
+
+// `laya-java/fixtures/presets.json` is the shared preset oracle: the Java port reads every entry
+// out of it, and `laya-java/scripts/gen_fixtures.py` regenerates it by calling `laya/presets.py`, so
+// it is the reference's own output rather than a transcription of it. The TypeScript port had no
+// test that consulted it, which is how `emailQuestions({})` came to disagree with the reference:
+// Python's `categories or {…defaults}` substitutes on *truthiness*, so an empty map gets the six
+// default categories -- the fixture entry `email_questions_empty_categories` is literally
+// `mod.email_questions({})` -- while the port's `categories ?? {…defaults}` substitutes only on
+// null and undefined and so builds a choice question with no options to choose from.
+//
+// The fixture also carries `state_field`, `state_field_hostile` and `hostile_specs` entries for
+// `laya.presets.state_field`, which this port has no counterpart for. Those are out of scope here
+// and deliberately not asserted.
+const fixture = JSON.parse(
+ readFileSync(new URL("../../laya-java/fixtures/presets.json", import.meta.url), "utf8"),
+) as Record<string, Record<string, unknown>>;
+
+const BUILDERS: Record<string, () => Record<string, unknown>> = {
+ triage_questions: triageQuestions,
+ guard_questions: guardQuestions,
+ moderation_questions: moderationQuestions,
+ router_questions: routerQuestions,
+ email_questions: () => emailQuestions(),
+};
+
+describe("presets against the shared Java fixture", () => {
+ for (const [name, build] of Object.entries(BUILDERS)) {
+ it(`${name} matches the committed oracle`, () => {
+ expect(build()).toEqual(fixture[name]);
+ });
+ }
+
+ it("email_questions uses a non-empty category map verbatim", () => {
+ const cats = JSON.parse(
+ JSON.stringify({
+ ops: "incidents and deploys",
+ legal: "contracts and compliance",
+ }),
+ ) as Record<string, string>;
+ expect(emailQuestions(cats)).toEqual(fixture.email_questions_custom);
+ expect((emailQuestions(cats).category as { criteria: Record<string, string> }).criteria).toEqual(cats);
+ });
+
+ it("email_questions falls back to the defaults on null and undefined", () => {
+ expect(emailQuestions(undefined)).toEqual(fixture.email_questions);
+ expect(emailQuestions(null as unknown as Record<string, string>)).toEqual(fixture.email_questions);
+ });
+
+ it("email_questions falls back to the defaults on an empty map, as Python's `or` does", () => {
+ expect(emailQuestions({})).toEqual(fixture.email_questions_empty_categories);
+ const category = emailQuestions({}).category as { criteria: Record<string, string> };
+ expect(Object.keys(category.criteria)).toHaveLength(6);
+ });
+
+ it("every form the reference's `or` treats as empty builds the default question set", () => {
+ // Measured against `laya.presets.email_questions(v)` for each of these: `None`, `{}`, `""`,
+ // `0`, `False` and `[]` all substitute to the defaults, and only a non-empty mapping is used.
+ const empties: unknown[] = [{}, "", 0, false, [], undefined, null, 0.0];
+ for (const empty of empties) {
+ expect(emailQuestions(empty as Record<string, string>), JSON.stringify(empty)).toEqual(
+ fixture.email_questions_empty_categories,
+ );
+ }
+ });
+});