|
1 | 1 | // @vitest-environment node |
2 | 2 | // Control UI tests cover operator question parsing and lifecycle state. |
3 | 3 | import { afterEach, describe, expect, it, vi } from "vitest"; |
| 4 | +import { GatewayRequestError } from "../api/gateway.ts"; |
4 | 5 | import { waitForFast } from "../test-helpers/wait-for.ts"; |
5 | 6 | import { |
6 | 7 | cancelQuestionPrompt, |
@@ -46,12 +47,42 @@ function requestedPayload(overrides: Record<string, unknown> = {}) { |
46 | 47 | } |
47 | 48 |
|
48 | 49 | function questionNotFoundError() { |
49 | | - return Object.assign(new Error("question was not found"), { |
50 | | - name: "GatewayClientRequestError", |
| 50 | + return new GatewayRequestError({ |
| 51 | + code: "INVALID_REQUEST", |
| 52 | + message: "question was not found", |
51 | 53 | details: { reason: "QUESTION_NOT_FOUND" }, |
52 | 54 | }); |
53 | 55 | } |
54 | 56 |
|
| 57 | +function createDeferredQuestionRequest() { |
| 58 | + let finishRequest: (value: unknown) => void = () => {}; |
| 59 | + let failRequest: (error: Error) => void = () => {}; |
| 60 | + const request = vi.fn<RequestFn>( |
| 61 | + () => |
| 62 | + new Promise((resolve, reject) => { |
| 63 | + finishRequest = resolve; |
| 64 | + failRequest = reject; |
| 65 | + }), |
| 66 | + ); |
| 67 | + return { |
| 68 | + request, |
| 69 | + resolve: () => finishRequest({ status: "answered" }), |
| 70 | + reject: () => failRequest(new Error("stale gateway unavailable")), |
| 71 | + }; |
| 72 | +} |
| 73 | + |
| 74 | +const questionResolutionCases = [ |
| 75 | + { |
| 76 | + action: "answer", |
| 77 | + resolve: (state: QuestionPromptState) => |
| 78 | + submitQuestionPrompt(state, "question-1", { format: ["Compact"] }), |
| 79 | + }, |
| 80 | + { |
| 81 | + action: "cancel", |
| 82 | + resolve: (state: QuestionPromptState) => cancelQuestionPrompt(state, "question-1"), |
| 83 | + }, |
| 84 | +] as const; |
| 85 | + |
55 | 86 | afterEach(() => { |
56 | 87 | for (const state of states.splice(0)) { |
57 | 88 | disposeQuestionPromptState(state); |
@@ -382,6 +413,226 @@ describe("question RPC helpers", () => { |
382 | 413 | }); |
383 | 414 | }); |
384 | 415 |
|
| 416 | +describe("question resolution connection ownership", () => { |
| 417 | + it.each(questionResolutionCases)( |
| 418 | + "ignores an old $action success after the gateway client changes", |
| 419 | + async ({ resolve }) => { |
| 420 | + const stale = createDeferredQuestionRequest(); |
| 421 | + const current = createDeferredQuestionRequest(); |
| 422 | + const state = createState(); |
| 423 | + setQuestionPromptClient(state, { request: stale.request }); |
| 424 | + handleQuestionPromptEvent(state, { |
| 425 | + event: "question.requested", |
| 426 | + payload: requestedPayload(), |
| 427 | + }); |
| 428 | + |
| 429 | + const staleResolution = resolve(state); |
| 430 | + setQuestionPromptClient(state, { request: current.request }); |
| 431 | + expect(state.prompts.has("question-1")).toBe(false); |
| 432 | + handleQuestionPromptEvent(state, { |
| 433 | + event: "question.requested", |
| 434 | + payload: requestedPayload(), |
| 435 | + }); |
| 436 | + |
| 437 | + const currentResolution = resolve(state); |
| 438 | + stale.resolve(); |
| 439 | + await staleResolution; |
| 440 | + |
| 441 | + expect(state.prompts.get("question-1")).toMatchObject({ |
| 442 | + status: "pending", |
| 443 | + submitting: true, |
| 444 | + localResolutionConfirmed: false, |
| 445 | + error: null, |
| 446 | + }); |
| 447 | + |
| 448 | + current.resolve(); |
| 449 | + await currentResolution; |
| 450 | + expect(state.prompts.get("question-1")?.localResolutionConfirmed).toBe(true); |
| 451 | + }, |
| 452 | + ); |
| 453 | + |
| 454 | + it.each(questionResolutionCases)( |
| 455 | + "ignores an old $action error after the gateway client changes", |
| 456 | + async ({ resolve }) => { |
| 457 | + const stale = createDeferredQuestionRequest(); |
| 458 | + const current = createDeferredQuestionRequest(); |
| 459 | + const state = createState(); |
| 460 | + setQuestionPromptClient(state, { request: stale.request }); |
| 461 | + handleQuestionPromptEvent(state, { |
| 462 | + event: "question.requested", |
| 463 | + payload: requestedPayload(), |
| 464 | + }); |
| 465 | + |
| 466 | + const staleResolution = resolve(state); |
| 467 | + setQuestionPromptClient(state, { request: current.request }); |
| 468 | + expect(state.prompts.has("question-1")).toBe(false); |
| 469 | + handleQuestionPromptEvent(state, { |
| 470 | + event: "question.requested", |
| 471 | + payload: requestedPayload(), |
| 472 | + }); |
| 473 | + |
| 474 | + const currentResolution = resolve(state); |
| 475 | + stale.reject(); |
| 476 | + await staleResolution; |
| 477 | + |
| 478 | + expect(state.prompts.get("question-1")).toMatchObject({ |
| 479 | + status: "pending", |
| 480 | + submitting: true, |
| 481 | + localResolutionConfirmed: false, |
| 482 | + error: null, |
| 483 | + }); |
| 484 | + |
| 485 | + current.resolve(); |
| 486 | + await currentResolution; |
| 487 | + expect(state.prompts.get("question-1")?.localResolutionConfirmed).toBe(true); |
| 488 | + }, |
| 489 | + ); |
| 490 | + |
| 491 | + it.each(questionResolutionCases)( |
| 492 | + "retires an old $action when the same gateway client reconnects", |
| 493 | + async ({ resolve }) => { |
| 494 | + const stale = createDeferredQuestionRequest(); |
| 495 | + const current = createDeferredQuestionRequest(); |
| 496 | + let requestCount = 0; |
| 497 | + const client = { |
| 498 | + request: vi.fn<RequestFn>((method, params) => { |
| 499 | + requestCount += 1; |
| 500 | + return requestCount === 1 |
| 501 | + ? stale.request(method, params) |
| 502 | + : current.request(method, params); |
| 503 | + }), |
| 504 | + }; |
| 505 | + const state = createState(); |
| 506 | + setQuestionPromptClient(state, client); |
| 507 | + handleQuestionPromptEvent(state, { |
| 508 | + event: "question.requested", |
| 509 | + payload: requestedPayload(), |
| 510 | + }); |
| 511 | + |
| 512 | + const staleResolution = resolve(state); |
| 513 | + setQuestionPromptClient(state, null); |
| 514 | + expect(state.prompts.get("question-1")?.submitting).toBe(false); |
| 515 | + setQuestionPromptClient(state, client); |
| 516 | + |
| 517 | + const currentResolution = resolve(state); |
| 518 | + stale.resolve(); |
| 519 | + await staleResolution; |
| 520 | + |
| 521 | + expect(state.prompts.get("question-1")).toMatchObject({ |
| 522 | + status: "pending", |
| 523 | + submitting: true, |
| 524 | + localResolutionConfirmed: false, |
| 525 | + error: null, |
| 526 | + }); |
| 527 | + |
| 528 | + current.resolve(); |
| 529 | + await currentResolution; |
| 530 | + expect(state.prompts.get("question-1")?.localResolutionConfirmed).toBe(true); |
| 531 | + }, |
| 532 | + ); |
| 533 | + |
| 534 | + it.each(questionResolutionCases)( |
| 535 | + "keeps a current $action after same-connection prompt hydration", |
| 536 | + async ({ resolve }) => { |
| 537 | + const pending = createDeferredQuestionRequest(); |
| 538 | + const request = vi.fn<RequestFn>((method, params) => |
| 539 | + method === "question.list" |
| 540 | + ? Promise.resolve({ questions: [requestedPayload()] }) |
| 541 | + : pending.request(method, params), |
| 542 | + ); |
| 543 | + const state = createState(); |
| 544 | + const client = { request }; |
| 545 | + setQuestionPromptClient(state, client); |
| 546 | + handleQuestionPromptEvent(state, { |
| 547 | + event: "question.requested", |
| 548 | + payload: requestedPayload(), |
| 549 | + }); |
| 550 | + |
| 551 | + const resolution = resolve(state); |
| 552 | + const original = state.prompts.get("question-1"); |
| 553 | + refreshPendingQuestionsWithRetry(state, client); |
| 554 | + await waitForFast(() => expect(state.prompts.get("question-1")).not.toBe(original)); |
| 555 | + |
| 556 | + pending.resolve(); |
| 557 | + await resolution; |
| 558 | + |
| 559 | + expect(state.prompts.get("question-1")).toMatchObject({ |
| 560 | + status: "pending", |
| 561 | + localResolutionConfirmed: true, |
| 562 | + error: null, |
| 563 | + }); |
| 564 | + }, |
| 565 | + ); |
| 566 | + |
| 567 | + it.each(["pending", "answered", "cancelled", "expired"] as const)( |
| 568 | + "does not carry a %s question into a replacement gateway", |
| 569 | + (status) => { |
| 570 | + const firstClient = { request: vi.fn<RequestFn>(async () => ({})) }; |
| 571 | + const secondClient = { request: vi.fn<RequestFn>(async () => ({})) }; |
| 572 | + const state = createState(); |
| 573 | + setQuestionPromptClient(state, firstClient); |
| 574 | + handleQuestionPromptEvent(state, { |
| 575 | + event: "question.requested", |
| 576 | + payload: requestedPayload(), |
| 577 | + }); |
| 578 | + if (status !== "pending") { |
| 579 | + handleQuestionPromptEvent(state, { |
| 580 | + event: "question.resolved", |
| 581 | + payload: |
| 582 | + status === "answered" |
| 583 | + ? { |
| 584 | + id: "question-1", |
| 585 | + status, |
| 586 | + answers: { answers: { format: ["Detailed"] } }, |
| 587 | + } |
| 588 | + : { id: "question-1", status }, |
| 589 | + }); |
| 590 | + } |
| 591 | + handleQuestionPromptEvent(state, { |
| 592 | + event: "question.resolved", |
| 593 | + payload: { id: "unmatched-first-gateway", status: "cancelled" }, |
| 594 | + }); |
| 595 | + setQuestionPromptClient(state, null); |
| 596 | + |
| 597 | + setQuestionPromptClient(state, secondClient); |
| 598 | + |
| 599 | + expect(state.prompts.size).toBe(0); |
| 600 | + expect(state.unmatchedResolutions.size).toBe(0); |
| 601 | + }, |
| 602 | + ); |
| 603 | + |
| 604 | + it("preserves authoritative question records when the same gateway reconnects", () => { |
| 605 | + const client = { request: vi.fn<RequestFn>(async () => ({})) }; |
| 606 | + const state = createState(); |
| 607 | + setQuestionPromptClient(state, client); |
| 608 | + handleQuestionPromptEvent(state, { |
| 609 | + event: "question.requested", |
| 610 | + payload: requestedPayload(), |
| 611 | + }); |
| 612 | + handleQuestionPromptEvent(state, { |
| 613 | + event: "question.resolved", |
| 614 | + payload: { |
| 615 | + id: "question-1", |
| 616 | + status: "answered", |
| 617 | + answers: { answers: { format: ["Detailed"] } }, |
| 618 | + }, |
| 619 | + }); |
| 620 | + handleQuestionPromptEvent(state, { |
| 621 | + event: "question.resolved", |
| 622 | + payload: { id: "unmatched-same-gateway", status: "cancelled" }, |
| 623 | + }); |
| 624 | + |
| 625 | + setQuestionPromptClient(state, null); |
| 626 | + setQuestionPromptClient(state, client); |
| 627 | + |
| 628 | + expect(state.prompts.get("question-1")).toMatchObject({ |
| 629 | + status: "answered", |
| 630 | + answers: { answers: { format: ["Detailed"] } }, |
| 631 | + }); |
| 632 | + expect(state.unmatchedResolutions.has("unmatched-same-gateway")).toBe(true); |
| 633 | + }); |
| 634 | +}); |
| 635 | + |
385 | 636 | describe("refreshPendingQuestions", () => { |
386 | 637 | it("hydrates pending questions after connect", async () => { |
387 | 638 | const request = vi.fn<RequestFn>(async () => ({ questions: [requestedPayload()] })); |
|
0 commit comments