hostCapability.test.ts 1.0 KB

123456789101112131415161718192021222324252627282930
  1. import { describe, expect, it } from "vitest";
  2. describe("desktop host websocket capability store", () => {
  3. it("capability 有 TTL 且只能成功消费一次,无效值不会赋予权限", async () => {
  4. const modulePromise = import("../src/hostCapability.js");
  5. await expect(modulePromise).resolves.toBeDefined();
  6. const { createHostCapabilityStore } = await modulePromise;
  7. let now = 1_000;
  8. let serial = 0;
  9. const store = createHostCapabilityStore({
  10. ttlMs: 100,
  11. now: () => now,
  12. createCapability: () => `capability-${++serial}`,
  13. });
  14. const first = store.issue();
  15. expect(first).toEqual({
  16. capability: "capability-1",
  17. expiresAt: 1_100,
  18. });
  19. expect(store.consume("unknown")).toBe(false);
  20. expect(store.consume(first.capability)).toBe(true);
  21. expect(store.consume(first.capability)).toBe(false);
  22. const expired = store.issue();
  23. now = expired.expiresAt + 1;
  24. expect(store.consume(expired.capability)).toBe(false);
  25. expect(store.consume(undefined)).toBe(false);
  26. });
  27. });