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