pack-options.test.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. DEFAULT_PACK_SUBDIR,
  4. dialogDefaultsFromProject,
  5. packHints,
  6. projectYamlFromPack,
  7. resolvePackRequest,
  8. } from "../src/pack-options.js";
  9. const norm = (p: string) => p.split("\\").join("/").replace(/^([A-Z]):/i, "");
  10. describe("dialogDefaultsFromProject", () => {
  11. it("pre-fills the dialog from project.yaml pack fields", () => {
  12. const options = dialogDefaultsFromProject(
  13. { name: "My Farm", icon: "icon.png", pack: { name: "Pinefield Valley", installer: true, mods: true, splitAssets: true } },
  14. "/proj",
  15. "fallback",
  16. );
  17. expect(options.name).toBe("Pinefield Valley");
  18. expect(options.icon).toBe("icon.png");
  19. expect(options.desktop).toBe(true);
  20. expect(options.installer).toBe(true);
  21. expect(options.mods).toBe(true);
  22. expect(options.splitAssets).toBe(true);
  23. expect(norm(options.outDir)).toBe(`/proj/${DEFAULT_PACK_SUBDIR}`);
  24. });
  25. it("falls back to the top-level name then the project display name with defaults", () => {
  26. expect(dialogDefaultsFromProject({ name: "My Farm" }, "/proj", "fallback").name).toBe("My Farm");
  27. const none = dialogDefaultsFromProject(null, "/proj", "fallback");
  28. expect(none.name).toBe("fallback");
  29. expect(none.icon).toBe("");
  30. expect(none.desktop).toBe(false);
  31. expect(none.installer).toBe(false);
  32. expect(none.mods).toBe(false);
  33. expect(none.splitAssets).toBe(false);
  34. });
  35. it("defaults desktop from pack.installer (installer implies desktop; desktop is not persisted)", () => {
  36. const options = dialogDefaultsFromProject({ pack: { installer: true } }, "/proj", "x");
  37. expect(options.desktop).toBe(true);
  38. expect(options.installer).toBe(true);
  39. });
  40. });
  41. describe("projectYamlFromPack", () => {
  42. it("writes pack fields + icon and preserves the prev display name", () => {
  43. const data = projectYamlFromPack(
  44. { name: "Pinefield Valley", icon: "icon.png", desktop: true, installer: true, mods: false, splitAssets: true, outDir: "/proj/dist-pack" },
  45. { name: "My Farm" },
  46. );
  47. expect(data).toEqual({
  48. name: "My Farm",
  49. icon: "icon.png",
  50. pack: { name: "Pinefield Valley", installer: true, mods: false, splitAssets: true },
  51. });
  52. });
  53. it("removes empty name/icon and keeps desktop out of project.yaml", () => {
  54. const data = projectYamlFromPack(
  55. { name: "", icon: "", desktop: true, installer: false, mods: false, splitAssets: false, outDir: "" },
  56. { name: "My Farm", pack: { name: "Old", mods: true } },
  57. );
  58. expect(data.pack?.name).toBeUndefined();
  59. expect(data.pack?.installer).toBe(false);
  60. expect(data.pack?.mods).toBe(false);
  61. expect(data.pack?.splitAssets).toBe(false);
  62. expect(data.icon).toBeUndefined();
  63. expect(data.name).toBe("My Farm");
  64. });
  65. });
  66. describe("resolvePackRequest", () => {
  67. it("maps dialog options to a pack request (content/outDir, installer implies desktop)", () => {
  68. const request = resolvePackRequest(
  69. "/proj",
  70. { name: "Pinefield Valley", icon: "", desktop: false, installer: true, mods: true, splitAssets: false, outDir: "" },
  71. true,
  72. );
  73. expect(norm(request.contentDir)).toBe("/proj/content");
  74. expect(norm(request.outDir)).toBe(`/proj/${DEFAULT_PACK_SUBDIR}`);
  75. expect(request.desktop).toBe(true);
  76. expect(request.installer).toBe(true);
  77. expect(request.splitAssets).toBe(false);
  78. expect(norm(request.modsDir ?? "")).toBe("/proj/mods");
  79. expect(request.name).toBe("Pinefield Valley");
  80. });
  81. it("honors a custom outDir, plain desktop, and omits mods when off or the dir is absent", () => {
  82. const custom = resolvePackRequest(
  83. "/proj",
  84. { name: "", icon: "", desktop: true, installer: false, mods: false, splitAssets: true, outDir: "/tmp/out" },
  85. true,
  86. );
  87. expect(norm(custom.outDir)).toBe("/tmp/out");
  88. expect(custom.splitAssets).toBe(true);
  89. expect(custom.desktop).toBe(true);
  90. expect(custom.name).toBeUndefined();
  91. expect(custom.modsDir).toBeUndefined();
  92. const noModsDir = resolvePackRequest(
  93. "/proj",
  94. { name: "", icon: "", desktop: false, installer: false, mods: true, splitAssets: false, outDir: "" },
  95. false,
  96. );
  97. expect(noModsDir.modsDir).toBeUndefined();
  98. });
  99. });
  100. describe("packHints", () => {
  101. it("includes desktop and installer commands when those modes are on", () => {
  102. const hints = packHints({
  103. defCount: 1,
  104. kinds: 1,
  105. assetCount: 0,
  106. modCount: 0,
  107. desktop: true,
  108. installer: true,
  109. });
  110. const text = hints.join("\n");
  111. expect(text).toContain("npm install && npm start");
  112. expect(text).toContain("npm install && npm run dist");
  113. });
  114. it("is site-only without desktop/installer", () => {
  115. const hints = packHints({ defCount: 1, kinds: 1, assetCount: 0, modCount: 0, desktop: false, installer: false });
  116. expect(hints).toHaveLength(1);
  117. expect(hints[0]).toContain("serve the output folder");
  118. });
  119. });