mods-model.test.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { describe, expect, it } from "vitest";
  2. import type { EditorModsList } from "../src/ipc.js";
  3. import { enabledModsCount, isModEnabled, toggleEnabled } from "../src/renderer/mods-model.js";
  4. function list(enabled: string[] | null, ids: string[]): EditorModsList {
  5. return { enabled, mods: ids.map((id) => ({ id, version: "1.0.0", hooks: ["onDayStart"], loaded: true })) };
  6. }
  7. describe("mods-model toggle logic (F1)", () => {
  8. it("a null enabled list means every known mod is enabled", () => {
  9. const l = list(null, ["a-mod", "b-mod"]);
  10. expect(isModEnabled(l, "a-mod")).toBe(true);
  11. expect(isModEnabled(l, "b-mod")).toBe(true);
  12. expect(enabledModsCount(l)).toBe(2);
  13. });
  14. it("disabling a mod with no explicit list makes the rest explicit", () => {
  15. const l = list(null, ["a-mod", "b-mod", "c-mod"]);
  16. const next = toggleEnabled(l, "b-mod", false);
  17. expect(next).toEqual(["a-mod", "c-mod"]);
  18. const l2 = list(next, ["a-mod", "b-mod", "c-mod"]);
  19. expect(isModEnabled(l2, "b-mod")).toBe(false);
  20. expect(enabledModsCount(l2)).toBe(2);
  21. });
  22. it("re-enabling appends the id back", () => {
  23. const l = list(["a-mod", "c-mod"], ["a-mod", "b-mod", "c-mod"]);
  24. const next = toggleEnabled(l, "b-mod", true);
  25. expect(next).toEqual(["a-mod", "c-mod", "b-mod"]);
  26. });
  27. it("disabling with an explicit list removes only that id", () => {
  28. const l = list(["a-mod", "b-mod", "c-mod"], ["a-mod", "b-mod", "c-mod"]);
  29. expect(toggleEnabled(l, "a-mod", false)).toEqual(["b-mod", "c-mod"]);
  30. });
  31. it("disabling the last mod yields an empty list (all disabled), never a deletion", () => {
  32. const l = list(null, ["a-mod"]);
  33. expect(toggleEnabled(l, "a-mod", false)).toEqual([]);
  34. const l2 = list([], ["a-mod"]);
  35. expect(toggleEnabled(l2, "a-mod", true)).toEqual(["a-mod"]);
  36. expect(enabledModsCount(list([], ["a-mod"]))).toBe(0);
  37. });
  38. it("an unknown id in the enabled list is counted but never toggles others", () => {
  39. const l = list(["ghost"], ["a-mod"]);
  40. expect(enabledModsCount(l)).toBe(0);
  41. expect(isModEnabled(l, "a-mod")).toBe(false);
  42. });
  43. });