autoArchiveScheduler.test.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
  2. import type { AutoArchiveInput } from "../src/project-storage/contract.js";
  3. import type { ProjectTaskRef } from "../src/project/contract.js";
  4. import {
  5. createTaskAutoArchiveScheduler,
  6. shouldRescanTaskAutoArchive,
  7. } from "../src/project/autoArchiveScheduler.js";
  8. const policy: AutoArchiveInput = { cutoff: 100, activeTasks: [] };
  9. const stale: ProjectTaskRef = { workspacePath: "/cwd", taskId: "stale" };
  10. function harness(overrides?: { readPolicy?: () => Promise<AutoArchiveInput | null> }) {
  11. const archiveInactive = vi.fn<(input: AutoArchiveInput) => ProjectTaskRef[]>(() => []);
  12. const afterChange = vi.fn(async () => {});
  13. const readPolicy = vi.fn(overrides?.readPolicy ?? (async () => policy));
  14. const scheduler = createTaskAutoArchiveScheduler({
  15. readPolicy,
  16. repository: async () => ({ archiveInactive }),
  17. afterChange,
  18. intervalMs: 1_000,
  19. });
  20. return { scheduler, archiveInactive, afterChange, readPolicy };
  21. }
  22. describe("task auto archive scheduler", () => {
  23. beforeEach(() => {
  24. vi.useFakeTimers();
  25. });
  26. afterEach(() => {
  27. vi.useRealTimers();
  28. });
  29. it("scans once on start and then on every interval until disposed", async () => {
  30. const { scheduler, archiveInactive } = harness();
  31. scheduler.start();
  32. scheduler.start();
  33. await vi.advanceTimersByTimeAsync(0);
  34. expect(archiveInactive).toHaveBeenCalledTimes(1);
  35. await vi.advanceTimersByTimeAsync(2_000);
  36. expect(archiveInactive).toHaveBeenCalledTimes(3);
  37. scheduler.dispose();
  38. await vi.advanceTimersByTimeAsync(5_000);
  39. expect(archiveInactive).toHaveBeenCalledTimes(3);
  40. await expect(scheduler.scan()).resolves.toEqual([]);
  41. expect(archiveInactive).toHaveBeenCalledTimes(3);
  42. });
  43. it("skips the repository when the policy is disabled and broadcasts only actual changes", async () => {
  44. let enabled = false;
  45. const { scheduler, archiveInactive, afterChange } = harness({
  46. readPolicy: async () => (enabled ? policy : null),
  47. });
  48. await expect(scheduler.scan()).resolves.toEqual([]);
  49. expect(archiveInactive).not.toHaveBeenCalled();
  50. enabled = true;
  51. await expect(scheduler.scan()).resolves.toEqual([]);
  52. expect(archiveInactive).toHaveBeenCalledWith(policy);
  53. expect(afterChange).not.toHaveBeenCalled();
  54. archiveInactive.mockReturnValueOnce([stale]);
  55. await expect(scheduler.scan()).resolves.toEqual([stale]);
  56. expect(afterChange).toHaveBeenCalledTimes(1);
  57. });
  58. it("serializes overlapping scans and coalesces waiters into one follow-up scan", async () => {
  59. const release: Array<() => void> = [];
  60. const { scheduler, archiveInactive } = harness({
  61. readPolicy: () =>
  62. new Promise<AutoArchiveInput>((resolve) => release.push(() => resolve(policy))),
  63. });
  64. const first = scheduler.scan();
  65. const second = scheduler.scan();
  66. const third = scheduler.scan();
  67. expect(third).toBe(second);
  68. expect(release).toHaveLength(1);
  69. release[0]!();
  70. await first;
  71. expect(archiveInactive).toHaveBeenCalledTimes(1);
  72. await vi.advanceTimersByTimeAsync(0);
  73. expect(release).toHaveLength(2);
  74. archiveInactive.mockReturnValueOnce([stale]);
  75. release[1]!();
  76. await expect(second).resolves.toEqual([stale]);
  77. expect(archiveInactive).toHaveBeenCalledTimes(2);
  78. });
  79. it("keeps the interval alive after a failed scan", async () => {
  80. const { scheduler, archiveInactive, readPolicy } = harness();
  81. readPolicy.mockRejectedValueOnce(new Error("settings unavailable"));
  82. scheduler.start();
  83. await vi.advanceTimersByTimeAsync(0);
  84. expect(archiveInactive).not.toHaveBeenCalled();
  85. await vi.advanceTimersByTimeAsync(1_000);
  86. expect(archiveInactive).toHaveBeenCalledTimes(1);
  87. scheduler.dispose();
  88. });
  89. it("only rescans for the two auto archive setting keys", () => {
  90. expect(shouldRescanTaskAutoArchive(["taskAutoArchiveEnabled"])).toBe(true);
  91. expect(shouldRescanTaskAutoArchive(["theme", "taskAutoArchiveOlderThanDays"])).toBe(true);
  92. expect(shouldRescanTaskAutoArchive(["theme", "recentProjects"])).toBe(false);
  93. expect(shouldRescanTaskAutoArchive([])).toBe(false);
  94. });
  95. });