scheduler.test.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { describe, expect, test } from "bun:test"
  2. import { Scheduler } from "../src/scheduler"
  3. import { Instance } from "../src/project/instance"
  4. import { tmpdir } from "./fixture/fixture"
  5. describe("Scheduler.register", () => {
  6. const hour = 60 * 60 * 1000
  7. test("defaults to instance scope per directory", async () => {
  8. await using one = await tmpdir({ git: true })
  9. await using two = await tmpdir({ git: true })
  10. const runs = { count: 0 }
  11. const id = "scheduler.instance." + Math.random().toString(36).slice(2)
  12. const task = {
  13. id,
  14. interval: hour,
  15. run: async () => {
  16. runs.count += 1
  17. },
  18. }
  19. await Instance.provide({
  20. directory: one.path,
  21. fn: async () => {
  22. Scheduler.register(task)
  23. await Instance.dispose()
  24. },
  25. })
  26. expect(runs.count).toBe(1)
  27. await Instance.provide({
  28. directory: two.path,
  29. fn: async () => {
  30. Scheduler.register(task)
  31. await Instance.dispose()
  32. },
  33. })
  34. expect(runs.count).toBe(2)
  35. })
  36. test("global scope runs once across instances", async () => {
  37. await using one = await tmpdir({ git: true })
  38. await using two = await tmpdir({ git: true })
  39. const runs = { count: 0 }
  40. const id = "scheduler.global." + Math.random().toString(36).slice(2)
  41. const task = {
  42. id,
  43. interval: hour,
  44. run: async () => {
  45. runs.count += 1
  46. },
  47. scope: "global" as const,
  48. }
  49. await Instance.provide({
  50. directory: one.path,
  51. fn: async () => {
  52. Scheduler.register(task)
  53. await Instance.dispose()
  54. },
  55. })
  56. expect(runs.count).toBe(1)
  57. await Instance.provide({
  58. directory: two.path,
  59. fn: async () => {
  60. Scheduler.register(task)
  61. await Instance.dispose()
  62. },
  63. })
  64. expect(runs.count).toBe(1)
  65. })
  66. })