hostCapability.test.ts 961 B

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