From 6cdbb6b4eb06c71d7726d463cd0ee8333d97f8d0 Mon Sep 17 00:00:00 2001 From: Aiden Cline Date: Sun, 18 Jan 2026 22:08:08 -0600 Subject: [PATCH] core: ensure shared scheduler tasks run once with multiple projects open --- packages/opencode/test/scheduler.test.ts | 73 ++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 packages/opencode/test/scheduler.test.ts diff --git a/packages/opencode/test/scheduler.test.ts b/packages/opencode/test/scheduler.test.ts new file mode 100644 index 0000000000..328daad9b8 --- /dev/null +++ b/packages/opencode/test/scheduler.test.ts @@ -0,0 +1,73 @@ +import { describe, expect, test } from "bun:test" +import { Scheduler } from "../src/scheduler" +import { Instance } from "../src/project/instance" +import { tmpdir } from "./fixture/fixture" + +describe("Scheduler.register", () => { + const hour = 60 * 60 * 1000 + + test("defaults to instance scope per directory", async () => { + await using one = await tmpdir({ git: true }) + await using two = await tmpdir({ git: true }) + const runs = { count: 0 } + const id = "scheduler.instance." + Math.random().toString(36).slice(2) + const task = { + id, + interval: hour, + run: async () => { + runs.count += 1 + }, + } + + await Instance.provide({ + directory: one.path, + fn: async () => { + Scheduler.register(task) + await Instance.dispose() + }, + }) + expect(runs.count).toBe(1) + + await Instance.provide({ + directory: two.path, + fn: async () => { + Scheduler.register(task) + await Instance.dispose() + }, + }) + expect(runs.count).toBe(2) + }) + + test("global scope runs once across instances", async () => { + await using one = await tmpdir({ git: true }) + await using two = await tmpdir({ git: true }) + const runs = { count: 0 } + const id = "scheduler.global." + Math.random().toString(36).slice(2) + const task = { + id, + interval: hour, + run: async () => { + runs.count += 1 + }, + scope: "global" as const, + } + + await Instance.provide({ + directory: one.path, + fn: async () => { + Scheduler.register(task) + await Instance.dispose() + }, + }) + expect(runs.count).toBe(1) + + await Instance.provide({ + directory: two.path, + fn: async () => { + Scheduler.register(task) + await Instance.dispose() + }, + }) + expect(runs.count).toBe(1) + }) +})