|
| 1 | +import { describe, expect, test } from "bun:test" |
| 2 | +import { CancelTaskTool } from "../../src/tool/cancel_task" |
| 3 | +import { Instance } from "../../src/project/instance" |
| 4 | +import { Session } from "../../src/session" |
| 5 | +import { tmpdir } from "../fixture/fixture" |
| 6 | + |
| 7 | +const ctx = { |
| 8 | + sessionID: "test", |
| 9 | + messageID: "", |
| 10 | + callID: "", |
| 11 | + agent: "build", |
| 12 | + abort: AbortSignal.any([]), |
| 13 | + metadata: () => {}, |
| 14 | + ask: async () => {}, |
| 15 | +} |
| 16 | + |
| 17 | +describe("tool.cancel_task", () => { |
| 18 | + test("returns not_found for non-existent task", async () => { |
| 19 | + await Instance.provide({ |
| 20 | + directory: "/tmp/test", |
| 21 | + fn: async () => { |
| 22 | + const tool = await CancelTaskTool.init() |
| 23 | + const result = await tool.execute({ task_id: "non-existent" }, ctx) |
| 24 | + |
| 25 | + const output = JSON.parse(result.output) |
| 26 | + expect(output.status).toBe("not_found") |
| 27 | + expect(output.task_id).toBe("non-existent") |
| 28 | + expect(output.message).toContain("not found") |
| 29 | + expect(result.metadata.status).toBe("not_found") |
| 30 | + }, |
| 31 | + }) |
| 32 | + }) |
| 33 | + |
| 34 | + test("returns already_completed for completed task", async () => { |
| 35 | + await using tmp = await tmpdir({ git: true }) |
| 36 | + await Instance.provide({ |
| 37 | + directory: tmp.path, |
| 38 | + fn: async () => { |
| 39 | + const tool = await CancelTaskTool.init() |
| 40 | + const taskId = "test-task-completed" |
| 41 | + |
| 42 | + Session.trackBackgroundTask(taskId, Promise.resolve("Task result"), undefined, { |
| 43 | + agent_type: "test", |
| 44 | + description: "Test task", |
| 45 | + session_id: ctx.sessionID, |
| 46 | + start_time: Date.now(), |
| 47 | + }) |
| 48 | + |
| 49 | + await new Promise((r) => setTimeout(r, 100)) |
| 50 | + |
| 51 | + const result = await tool.execute({ task_id: taskId }, ctx) |
| 52 | + |
| 53 | + const output = JSON.parse(result.output) |
| 54 | + expect(output.status).toBe("already_completed") |
| 55 | + expect(output.task_id).toBe(taskId) |
| 56 | + expect(output.message).toContain("cannot be cancelled") |
| 57 | + expect(result.metadata.status).toBe("already_completed") |
| 58 | + }, |
| 59 | + }) |
| 60 | + }) |
| 61 | + |
| 62 | + test("cancels a running task", async () => { |
| 63 | + await using tmp = await tmpdir({ git: true }) |
| 64 | + await Instance.provide({ |
| 65 | + directory: tmp.path, |
| 66 | + fn: async () => { |
| 67 | + const tool = await CancelTaskTool.init() |
| 68 | + const taskId = "test-task-running" |
| 69 | + let taskStarted = false |
| 70 | + |
| 71 | + const promise = new Promise<string>((resolve) => { |
| 72 | + taskStarted = true |
| 73 | + setTimeout(() => resolve("Should not complete"), 10000) |
| 74 | + }) |
| 75 | + |
| 76 | + Session.trackBackgroundTask(taskId, promise, undefined, { |
| 77 | + agent_type: "test", |
| 78 | + description: "Test task", |
| 79 | + session_id: ctx.sessionID, |
| 80 | + start_time: Date.now(), |
| 81 | + }) |
| 82 | + |
| 83 | + await new Promise((r) => setTimeout(r, 50)) |
| 84 | + |
| 85 | + const result = await tool.execute({ task_id: taskId }, ctx) |
| 86 | + |
| 87 | + const output = JSON.parse(result.output) |
| 88 | + expect(output.status).toBe("cancelled") |
| 89 | + expect(output.task_id).toBe(taskId) |
| 90 | + expect(output.message).toContain("cancelled") |
| 91 | + expect(result.metadata.status).toBe("cancelled") |
| 92 | + }, |
| 93 | + }) |
| 94 | + }) |
| 95 | + |
| 96 | + test("validates input with Zod schema", async () => { |
| 97 | + await Instance.provide({ |
| 98 | + directory: "/tmp/test", |
| 99 | + fn: async () => { |
| 100 | + const tool = await CancelTaskTool.init() |
| 101 | + |
| 102 | + const parameters = tool.parameters |
| 103 | + expect(parameters).toBeDefined() |
| 104 | + |
| 105 | + const valid = parameters.safeParse({ task_id: "test-id" }) |
| 106 | + expect(valid.success).toBe(true) |
| 107 | + |
| 108 | + const invalidEmpty = parameters.safeParse({ task_id: "" }) |
| 109 | + expect(invalidEmpty.success).toBe(false) |
| 110 | + |
| 111 | + const invalidSpecialChars = parameters.safeParse({ task_id: "test@id" }) |
| 112 | + expect(invalidSpecialChars.success).toBe(false) |
| 113 | + |
| 114 | + const validWithHyphen = parameters.safeParse({ task_id: "test-id-123" }) |
| 115 | + expect(validWithHyphen.success).toBe(true) |
| 116 | + |
| 117 | + const validWithUnderscore = parameters.safeParse({ task_id: "test_id_123" }) |
| 118 | + expect(validWithUnderscore.success).toBe(true) |
| 119 | + }, |
| 120 | + }) |
| 121 | + }) |
| 122 | + |
| 123 | + test("has correct tool description", async () => { |
| 124 | + await Instance.provide({ |
| 125 | + directory: "/tmp/test", |
| 126 | + fn: async () => { |
| 127 | + const tool = await CancelTaskTool.init() |
| 128 | + expect(tool.description).toContain("Stop a running background task") |
| 129 | + expect(tool.description).toContain("cancelled") |
| 130 | + }, |
| 131 | + }) |
| 132 | + }) |
| 133 | +}) |
0 commit comments