File size: 3,191 Bytes
e249c6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { afterEach, describe, expect, it, vi } from "vitest";
import type { CliBackendResolveExecutionArgsContext } from "../../plugins/cli-backend.types.js";
import { buildPreparedCliRunContext } from "../cli-runner.test-helpers.js";
import { executePreparedCliRun as executePreparedCliRunImpl } from "./execute.js";
import {
  createManagedRun,
  supervisorSpawnMock,
  wrapPreparedCliRunWithTestAdmission,
} from "./execute.test-support.js";

const executePreparedCliRun = wrapPreparedCliRunWithTestAdmission(executePreparedCliRunImpl);

afterEach(() => {
  supervisorSpawnMock.mockReset();
  vi.useRealTimers();
});

describe("CLI backend resolveExecutionArgs fast mode", () => {
  it.each([
    [true, true],
    [false, false],
    ["auto", true],
    [undefined, undefined],
  ] as const)("resolves fastMode=%s to %s at execution", async (fastMode, expected) => {
    const resolveExecutionArgs = vi.fn((context: CliBackendResolveExecutionArgsContext) => [
      ...context.baseArgs,
    ]);
    const context = buildPreparedCliRunContext({
      provider: "codex-cli",
      model: "fixture-model",
      thinkLevel: "high",
      fastMode,
      resolveExecutionArgs,
      backend: {
        command: "/bin/sh",
        args: ["exec", "--json"],
        output: "text",
        systemPromptFileArg: undefined,
        input: "stdin",
      },
    });
    supervisorSpawnMock.mockResolvedValue(
      createManagedRun({
        reason: "exit",
        exitCode: 0,
        exitSignal: null,
        durationMs: 1,
        stdout: "done",
        stderr: "",
        timedOut: false,
        noOutputTimedOut: false,
      }),
    );

    await expect(executePreparedCliRun(context)).resolves.toMatchObject({ text: "done" });

    expect(resolveExecutionArgs).toHaveBeenCalledTimes(1);
    const resolved = resolveExecutionArgs.mock.calls[0]?.[0];
    expect(resolved).toBeDefined();
    expect(resolved?.fastMode).toBe(expected);
    expect(resolved?.thinkingLevel).toBe("high");
    expect(resolved?.baseArgs).toEqual(["exec", "--json"]);
  });

  it("counts awaited backend setup against the automatic cutoff", async () => {
    vi.useFakeTimers({ toFake: ["Date"] });
    vi.setSystemTime(1000);
    const resolveExecutionArgs = vi.fn((context: CliBackendResolveExecutionArgsContext) => [
      ...context.baseArgs,
    ]);
    const context = buildPreparedCliRunContext({
      provider: "codex-cli",
      model: "fixture-model",
      fastMode: "auto",
      resolveExecutionArgs,
      backend: { command: "/bin/sh", args: ["exec"], output: "text", input: "stdin" },
    });
    context.params.fastModeAutoOnSeconds = 1;
    context.preparedBackend.beforeExecution = async () => {
      vi.setSystemTime(2001);
    };
    supervisorSpawnMock.mockResolvedValue(
      createManagedRun({
        reason: "exit",
        exitCode: 0,
        exitSignal: null,
        durationMs: 1,
        stdout: "done",
        stderr: "",
        timedOut: false,
        noOutputTimedOut: false,
      }),
    );

    await expect(executePreparedCliRun(context)).resolves.toMatchObject({ text: "done" });
    expect(resolveExecutionArgs.mock.calls[0]?.[0].fastMode).toBe(false);
  });
});