Skip to content

A local-first multi-agent app development platform, built on PenguinHarness

4.3 KB
Raw Blame
/**
 * id-suggest-notice.ts unit tests: a model-made id says nothing, a transliteration gets the
 * quiet note, a placeholder gets the attention-toned one with the server's reason spelled out
 * in the reader's language, and a reason this build does not know still produces a sentence
 * rather than dropping the note that tells the user the box holds a placeholder.
 */
import { describe, expect, it } from "vitest";
import type { SemanticIdSuggestReason, SemanticIdSuggestResponse } from "@adelie/server/api";
import { idSuggestLine, idSuggestNotice } from "../src/features/semantic-id/id-suggest-notice";
import type { IdSuggestNotice } from "../src/features/semantic-id/id-suggest-notice";
import { zh } from "../src/lib/strings";
import { en } from "../src/lib/strings-en";

const REASONS = Object.keys({
  no_free_model: true,
  model_failed: true,
  unusable_answer: true,
  no_ascii: true,
} satisfies Record<SemanticIdSuggestReason, true>) as SemanticIdSuggestReason[];

describe("the reason dictionaries", () => {
  for (const [locale, dict] of Object.entries({ zh, en })) {
    it(`${locale} carries a phrase for every reason the contract declares, and no other`, () => {
      expect(Object.keys(dict.semanticId.idSuggest.reasons).sort()).toEqual([...REASONS].sort());
    });
  }
});

describe("idSuggestNotice", () => {
  it("says nothing about an id the model produced", () => {
    const res: SemanticIdSuggestResponse = { id: "co_research_lab", source: "model" };
    expect(idSuggestNotice(res, zh.semanticId.idSuggest)).toBeNull();
    expect(idSuggestNotice(res, en.semanticId.idSuggest)).toBeNull();
  });

  it("notes a transliteration quietly: nothing went wrong, it is just mechanical", () => {
    const res: SemanticIdSuggestResponse = { id: "co_plugin_marketplace", source: "fallback" };
    expect(idSuggestNotice(res, zh.semanticId.idSuggest)).toEqual({
      tone: "muted",
      text: "按名称转写生成",
    });
    expect(idSuggestNotice(res, en.semanticId.idSuggest)?.tone).toBe("muted");
  });

  it("asks for a real name when the id is a placeholder, and names the reason", () => {
    for (const reason of REASONS) {
      const res: SemanticIdSuggestResponse = {
        id: "co_org_20260909",
        source: "placeholder",
        reason,
      };
      const note = idSuggestNotice(res, zh.semanticId.idSuggest);
      expect(note?.tone, reason).toBe("attention");
      expect(note?.text, reason).toContain(zh.semanticId.idSuggest.reasons[reason]);
      expect(idSuggestNotice(res, en.semanticId.idSuggest)?.text, reason).toContain(
        en.semanticId.idSuggest.reasons[reason],
      );
    }
  });

  it("still tells the user it is a placeholder when the reason is one this build cannot read", () => {
    const newer = {
      id: "co_org_20260909",
      source: "placeholder",
      reason: "quota_exhausted",
    } as unknown as SemanticIdSuggestResponse;
    for (const dict of [zh, en]) {
      const note = idSuggestNotice(newer, dict.semanticId.idSuggest);
      expect(note?.tone).toBe("attention");
      expect(note?.text).toContain(dict.semanticId.idSuggest.reasonUnknown);
    }
  });

  it("names no reason it was not given (a placeholder with the field absent)", () => {
    const res = { id: "co_org_20260909", source: "placeholder" } as SemanticIdSuggestResponse;
    expect(idSuggestNotice(res, en.semanticId.idSuggest)?.text).toContain(
      en.semanticId.idSuggest.reasonUnknown,
    );
  });
});

describe("idSuggestLine", () => {
  const QUIET: IdSuggestNotice = { tone: "muted", text: "按名称转写生成" };

  it("says nothing while the box speaks for itself", () => {
    expect(idSuggestLine(null, null)).toBeNull();
  });

  it("passes a note through with the tone it was given", () => {
    expect(idSuggestLine(QUIET, null)).toEqual({ kind: "muted", text: "按名称转写生成" });
    expect(idSuggestLine({ tone: "attention", text: "请填写名称" }, null)).toEqual({
      kind: "attention",
      text: "请填写名称",
    });
  });

  it("draws a failed request as a refusal of its own — the reason the button did nothing", () => {
    expect(idSuggestLine(null, "not_found")).toEqual({ kind: "refusal", text: "not_found" });
  });

  it("lets a refusal outrank a note left over from an earlier proposal", () => {
    expect(idSuggestLine(QUIET, "not_found")).toEqual({ kind: "refusal", text: "not_found" });
  });
});