fix: stabilize stale timeout tests with fixed Date.now()
Tests 'should use default timeout when config not provided' (manager.test.ts) and 'should use DEFAULT_MESSAGE_STALENESS_TIMEOUT_MS when not configured' (task-poller.test.ts) failed in CI because Date.now() drifted between test setup (when creating timestamps like Date.now() - 46*60*1000) and actual execution inside checkAndInterruptStaleTasks(). On slower CI machines, this drift pushed borderline values across the threshold, causing tasks that should be stale to remain 'running'. Fix: Mock Date.now with spyOn to return a fixed time, ensuring consistent timeout calculations regardless of execution speed.
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
declare const require: (name: string) => any
|
declare const require: (name: string) => any
|
||||||
const { describe, test, expect, beforeEach, afterEach } = require("bun:test")
|
const { describe, test, expect, beforeEach, afterEach, spyOn } = require("bun:test")
|
||||||
import { tmpdir } from "node:os"
|
import { tmpdir } from "node:os"
|
||||||
import type { PluginInput } from "@opencode-ai/plugin"
|
import type { PluginInput } from "@opencode-ai/plugin"
|
||||||
import type { BackgroundTask, ResumeInput } from "./types"
|
import type { BackgroundTask, ResumeInput } from "./types"
|
||||||
@@ -2781,6 +2781,18 @@ describe("BackgroundManager - Non-blocking Queue Integration", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe("BackgroundManager.checkAndInterruptStaleTasks", () => {
|
describe("BackgroundManager.checkAndInterruptStaleTasks", () => {
|
||||||
|
const originalDateNow = Date.now
|
||||||
|
let fixedTime: number
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
fixedTime = Date.now()
|
||||||
|
spyOn(globalThis.Date, "now").mockReturnValue(fixedTime)
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
Date.now = originalDateNow
|
||||||
|
})
|
||||||
|
|
||||||
test("should NOT interrupt task running less than 30 seconds (min runtime guard)", async () => {
|
test("should NOT interrupt task running less than 30 seconds (min runtime guard)", async () => {
|
||||||
const client = {
|
const client = {
|
||||||
session: {
|
session: {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
declare const require: (name: string) => any
|
declare const require: (name: string) => any
|
||||||
const { describe, it, expect, mock } = require("bun:test")
|
const { describe, it, expect, mock, spyOn, beforeEach, afterEach } = require("bun:test")
|
||||||
|
|
||||||
import { checkAndInterruptStaleTasks, pruneStaleTasksAndNotifications } from "./task-poller"
|
import { checkAndInterruptStaleTasks, pruneStaleTasksAndNotifications } from "./task-poller"
|
||||||
import type { BackgroundTask } from "./types"
|
import type { BackgroundTask } from "./types"
|
||||||
@@ -29,6 +29,18 @@ describe("checkAndInterruptStaleTasks", () => {
|
|||||||
...overrides,
|
...overrides,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
const originalDateNow = Date.now
|
||||||
|
let fixedTime: number
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
fixedTime = Date.now()
|
||||||
|
spyOn(globalThis.Date, "now").mockReturnValue(fixedTime)
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
Date.now = originalDateNow
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
it("should interrupt tasks with lastUpdate exceeding stale timeout", async () => {
|
it("should interrupt tasks with lastUpdate exceeding stale timeout", async () => {
|
||||||
//#given
|
//#given
|
||||||
|
|||||||
Reference in New Issue
Block a user