desktopAppCliConfig.test.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { mkdtemp, mkdir, readFile, rm, stat, writeFile } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { afterEach, describe, expect, it, vi } from "vitest";
  5. const originalHome = process.env.HOME;
  6. const originalUserProfile = process.env.USERPROFILE;
  7. const originalDeferAppDataCleanup = process.env.ZCODE_E2E_DEFER_APP_DATA_CLEANUP;
  8. const tempHomes: string[] = [];
  9. afterEach(async () => {
  10. process.env.HOME = originalHome;
  11. process.env.USERPROFILE = originalUserProfile;
  12. process.env.ZCODE_E2E_DEFER_APP_DATA_CLEANUP = originalDeferAppDataCleanup;
  13. vi.resetModules();
  14. await Promise.all(tempHomes.splice(0).map((home) => rm(home, { recursive: true, force: true })));
  15. });
  16. describe("desktop e2e CLI config helpers", () => {
  17. it("defers app data cleanup to the WDIO afterSession owner", async () => {
  18. process.env.ZCODE_E2E_DEFER_APP_DATA_CLEANUP = "1";
  19. const { clearAppData } = await importDesktopAppHelper();
  20. await expect(clearAppData()).resolves.toBeUndefined();
  21. });
  22. it("restores an existing CLI config after a temporary seed", async () => {
  23. const home = await createTempHome();
  24. const cliConfigFile = join(home, ".zcode", "cli", "config.json");
  25. await mkdir(join(home, ".zcode", "cli"), { recursive: true });
  26. await writeFile(
  27. cliConfigFile,
  28. JSON.stringify({ provider: { existing: true } }, null, 2),
  29. "utf-8",
  30. );
  31. const { restoreCliConfig, seedCliConfig, snapshotCliConfig } = await importDesktopAppHelper();
  32. const snapshot = await snapshotCliConfig();
  33. await seedCliConfig({ network: { httpProxy: "http://127.0.0.1:4321" } });
  34. await restoreCliConfig(snapshot);
  35. expect(JSON.parse(await readFile(cliConfigFile, "utf-8"))).toEqual({
  36. provider: { existing: true },
  37. });
  38. });
  39. it("removes the CLI config when restoring a missing-file snapshot", async () => {
  40. const home = await createTempHome();
  41. const cliConfigFile = join(home, ".zcode", "cli", "config.json");
  42. const { restoreCliConfig, seedCliConfig, snapshotCliConfig } = await importDesktopAppHelper();
  43. const snapshot = await snapshotCliConfig();
  44. await seedCliConfig({ network: { httpProxy: "http://127.0.0.1:4321" } });
  45. await restoreCliConfig(snapshot);
  46. await expect(stat(cliConfigFile)).rejects.toMatchObject({ code: "ENOENT" });
  47. });
  48. });
  49. async function createTempHome() {
  50. const home = await mkdtemp(join(tmpdir(), "zcode-desktop-e2e-home-"));
  51. tempHomes.push(home);
  52. process.env.HOME = home;
  53. process.env.USERPROFILE = home;
  54. vi.resetModules();
  55. return home;
  56. }
  57. async function importDesktopAppHelper() {
  58. return import("./e2e/helpers/desktop-app.js");
  59. }