cancel-without-active-task.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { runStreamCase, StreamEvent } from "../lib/stream-harness"
  2. async function main() {
  3. const cancelRequestId = `cancel-${Date.now()}`
  4. const shutdownRequestId = `shutdown-${Date.now()}`
  5. let initSeen = false
  6. let cancelAckSeen = false
  7. let cancelDoneSeen = false
  8. let shutdownSent = false
  9. await runStreamCase({
  10. onEvent(event: StreamEvent, context) {
  11. if (event.type === "system" && event.subtype === "init" && !initSeen) {
  12. initSeen = true
  13. context.sendCommand({
  14. command: "cancel",
  15. requestId: cancelRequestId,
  16. })
  17. return
  18. }
  19. if (
  20. event.type === "control" &&
  21. event.subtype === "ack" &&
  22. event.command === "cancel" &&
  23. event.requestId === cancelRequestId
  24. ) {
  25. cancelAckSeen = true
  26. return
  27. }
  28. if (
  29. event.type === "control" &&
  30. event.subtype === "done" &&
  31. event.command === "cancel" &&
  32. event.requestId === cancelRequestId
  33. ) {
  34. cancelDoneSeen = true
  35. if (event.code !== "no_active_task") {
  36. throw new Error(`cancel without task should return no_active_task, got "${event.code ?? "none"}"`)
  37. }
  38. if (event.success !== true) {
  39. throw new Error("cancel without task should be treated as successful no-op")
  40. }
  41. if (!shutdownSent) {
  42. context.sendCommand({
  43. command: "shutdown",
  44. requestId: shutdownRequestId,
  45. })
  46. shutdownSent = true
  47. }
  48. return
  49. }
  50. if (event.type === "control" && event.subtype === "error") {
  51. throw new Error(
  52. `unexpected control error command=${event.command ?? "unknown"} code=${event.code ?? "unknown"} content=${event.content ?? ""}`,
  53. )
  54. }
  55. },
  56. onTimeoutMessage() {
  57. return `timed out waiting for cancel-without-active-task validation (initSeen=${initSeen}, cancelAckSeen=${cancelAckSeen}, cancelDoneSeen=${cancelDoneSeen}, shutdownSent=${shutdownSent})`
  58. },
  59. })
  60. }
  61. main().catch((error) => {
  62. console.error(`[FAIL] ${error instanceof Error ? error.message : String(error)}`)
  63. process.exit(1)
  64. })