test: replaced exact sleep assertions with tolerance-aware helper

- Added `expectSleepNear` to allow ±100ms variance on sleep duration checks.
- Updated `--thinking` test value from `"extended"` to `Effort.XHigh` enum constant.
This commit is contained in:
can1357
2026-06-02 08:57:29 +02:00
parent f18eb90324
commit 38977b8450
2 changed files with 16 additions and 6 deletions
@@ -1,4 +1,4 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "bun:test";
import { afterEach, beforeEach, describe, expect, it, type Mock, vi } from "bun:test";
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
@@ -92,6 +92,15 @@ const mockPositiveSleepsImmediate = () => {
return realSleep(duration ?? 0);
});
};
const expectSleepNear = (sleepSpy: Mock<typeof Bun.sleep>, targetMs: number) => {
const minMs = targetMs - 100;
expect(
sleepSpy.mock.calls.some(
([duration]) => typeof duration === "number" && duration >= minMs && duration <= targetMs,
),
).toBe(true);
};
const createSession = async (
tempDir: string,
cwd: string,
@@ -415,7 +424,7 @@ describe("AgentSession python cleanup", () => {
const [toolResult] = await Promise.all([toolExecution, disposeSession]);
expect(sleepSpy).toHaveBeenCalledWith(3000);
expectSleepNear(sleepSpy, 3000);
expect(disposed).toBe(true);
expect(toolExecutionSettled).toBe(true);
@@ -460,7 +469,7 @@ describe("AgentSession python cleanup", () => {
firstDisposed = true;
});
await disposeFirst;
expect(sleepSpy).toHaveBeenCalledWith(3000);
expectSleepNear(sleepSpy, 3000);
expect(firstDisposed).toBe(true);
expect(firstExecutionSettled).toBe(false);
@@ -708,7 +717,7 @@ describe("AgentSession python cleanup", () => {
const sleepSpy = mockPositiveSleepsImmediate();
await session.dispose();
expect(sleepSpy).toHaveBeenCalledWith(3000);
expectSleepNear(sleepSpy, 3000);
const [firstResult, secondResult] = await Promise.all([firstExecution, secondExecution]);
expect(firstResult.cancelled).toBe(true);
@@ -1,4 +1,5 @@
import { describe, expect, it } from "bun:test";
import { Effort } from "@oh-my-pi/pi-ai";
import { parseArgs } from "../src/cli/args";
describe("parseArgs — --hide-thinking flag", () => {
@@ -20,9 +21,9 @@ describe("parseArgs — --hide-thinking flag", () => {
});
it("parses --hide-thinking with --thinking flag (both can coexist)", () => {
const result = parseArgs(["--hide-thinking", "--thinking", "extended"]);
const result = parseArgs(["--hide-thinking", "--thinking", "xhigh"]);
expect(result.hideThinking).toBe(true);
expect(result.thinking).toBe("extended");
expect(result.thinking).toBe(Effort.XHigh);
});
it("parses --hide-thinking in any position", () => {