webRemoteControlRelayAuthStorageProvider.test.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { describe, expect, it, vi } from "vitest";
  2. import type { AppSettings } from "@zcode/shared";
  3. import {
  4. WEB_REMOTE_CONTROL_EXTERNAL_RELAY_PASS_HASH_CREDENTIAL_KEY,
  5. createWebRemoteControlRelayAuthStorageProvider,
  6. } from "../src/main/webRemoteControlRelayAuthStorageProvider.js";
  7. function createHarness(initialSettings: AppSettings = { recentProjects: [], locale: "zh-CN" }) {
  8. let settings = initialSettings;
  9. const credentials: Record<string, string | undefined> = {};
  10. const patches: Partial<AppSettings>[] = [];
  11. const operations: string[] = [];
  12. const logger = {
  13. info: vi.fn(),
  14. warn: vi.fn(),
  15. };
  16. const provider = createWebRemoteControlRelayAuthStorageProvider({
  17. credentialService: {
  18. load: async (key) => credentials[key] ?? null,
  19. save: async (key, value) => {
  20. operations.push(`save:${key}:${value}`);
  21. credentials[key] = value;
  22. },
  23. delete: async (key) => {
  24. operations.push(`delete:${key}`);
  25. credentials[key] = undefined;
  26. },
  27. },
  28. loadSettings: async () => settings,
  29. patchSettings: async (patch) => {
  30. operations.push(`patch:${JSON.stringify(patch)}`);
  31. patches.push(patch);
  32. settings = { ...settings, ...patch };
  33. },
  34. logger,
  35. });
  36. return { credentials, logger, operations, patches, provider };
  37. }
  38. describe("createWebRemoteControlRelayAuthStorageProvider", () => {
  39. it("stores only deviceSid in settings and passHash in credential storage", async () => {
  40. const { credentials, patches, provider } = createHarness();
  41. await provider.save({ deviceSid: "sid-1", passHash: "hash-1" });
  42. expect(patches.at(-1)).toEqual({
  43. webRemoteControlExternalRelayDevice: { deviceSid: "sid-1" },
  44. });
  45. expect(credentials[WEB_REMOTE_CONTROL_EXTERNAL_RELAY_PASS_HASH_CREDENTIAL_KEY]).toBe("hash-1");
  46. expect(JSON.stringify(patches.at(-1))).not.toContain("hash-1");
  47. await expect(provider.load()).resolves.toEqual({
  48. deviceSid: "sid-1",
  49. passHash: "hash-1",
  50. });
  51. });
  52. it("clears partial persisted state and returns undefined", async () => {
  53. const { credentials, operations, patches, provider } = createHarness({
  54. recentProjects: [],
  55. locale: "zh-CN",
  56. webRemoteControlExternalRelayDevice: { deviceSid: "sid-1" },
  57. });
  58. credentials[WEB_REMOTE_CONTROL_EXTERNAL_RELAY_PASS_HASH_CREDENTIAL_KEY] = undefined;
  59. await expect(provider.load()).resolves.toBeUndefined();
  60. expect(operations).toContain(
  61. `delete:${WEB_REMOTE_CONTROL_EXTERNAL_RELAY_PASS_HASH_CREDENTIAL_KEY}`,
  62. );
  63. expect(patches.some((patch) => "webRemoteControlExternalRelayDevice" in patch)).toBe(
  64. true,
  65. );
  66. });
  67. it("rotates by deleting old credential before saving new auth", async () => {
  68. const { credentials, operations, provider } = createHarness({
  69. recentProjects: [],
  70. locale: "zh-CN",
  71. webRemoteControlExternalRelayDevice: { deviceSid: "old-sid" },
  72. });
  73. credentials[WEB_REMOTE_CONTROL_EXTERNAL_RELAY_PASS_HASH_CREDENTIAL_KEY] = "old-hash";
  74. await provider.rotate({ deviceSid: "new-sid", passHash: "new-hash" });
  75. expect(operations).toEqual([
  76. expect.stringContaining("patch:"),
  77. `delete:${WEB_REMOTE_CONTROL_EXTERNAL_RELAY_PASS_HASH_CREDENTIAL_KEY}`,
  78. expect.stringContaining("patch:"),
  79. `save:${WEB_REMOTE_CONTROL_EXTERNAL_RELAY_PASS_HASH_CREDENTIAL_KEY}:new-hash`,
  80. ]);
  81. expect(credentials[WEB_REMOTE_CONTROL_EXTERNAL_RELAY_PASS_HASH_CREDENTIAL_KEY]).toBe("new-hash");
  82. });
  83. it("redacts passHash in logs", async () => {
  84. const { logger, provider } = createHarness();
  85. await provider.save({ deviceSid: "device-sid-abcdef", passHash: "raw-secret-hash" });
  86. await provider.clear();
  87. const loggedText = JSON.stringify([...logger.info.mock.calls, ...logger.warn.mock.calls]);
  88. expect(loggedText).not.toContain("raw-secret-hash");
  89. expect(loggedText).toContain("hasPassHash");
  90. expect(loggedText).toContain("abcdef");
  91. });
  92. });