|
1 | 1 | // Matrix tests cover shared plugin behavior. |
2 | 2 | import { createDeferred } from "openclaw/plugin-sdk/extension-shared"; |
3 | 3 | import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; |
| 4 | +import { createMatrixMonitorTaskRunner } from "../monitor/task-runner.js"; |
4 | 5 | import type { MatrixAuth } from "./types.js"; |
5 | 6 |
|
6 | 7 | const resolveMatrixAuthMock = vi.hoisted(() => vi.fn()); |
@@ -585,6 +586,185 @@ describe("shared Matrix client generations", () => { |
585 | 586 | ]); |
586 | 587 | }); |
587 | 588 |
|
| 589 | + it.each( |
| 590 | + ["monitor release", "account stop"].flatMap((retirementOwner) => |
| 591 | + ["release", "late acquisition"].map((operation) => ({ retirementOwner, operation })), |
| 592 | + ), |
| 593 | + )( |
| 594 | + "drains monitor tasks during $retirementOwner with a transient $operation", |
| 595 | + async ({ retirementOwner, operation }) => { |
| 596 | + const client = createMockClient("main"); |
| 597 | + createMatrixClientMock.mockResolvedValue(client); |
| 598 | + const auth = authFor("main"); |
| 599 | + const monitor = await acquireSharedMatrixClient({ |
| 600 | + auth, |
| 601 | + role: "monitor", |
| 602 | + startClient: false, |
| 603 | + }); |
| 604 | + const transient = |
| 605 | + operation === "release" |
| 606 | + ? await acquireSharedMatrixClient({ auth, role: "transient", startClient: false }) |
| 607 | + : undefined; |
| 608 | + const logger = { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() }; |
| 609 | + const tasks = createMatrixMonitorTaskRunner({ logger, logVerboseMessage: vi.fn() }); |
| 610 | + const sendFinished = createDeferred<void>(); |
| 611 | + const releaseTestDrain = createDeferred<void>(); |
| 612 | + const taskAdmissionClosed = createDeferred<void>(); |
| 613 | + const taskFinished = vi.fn(); |
| 614 | + let lateLease: Awaited<ReturnType<typeof acquireSharedMatrixClient>> | undefined; |
| 615 | + let acquisitionError: unknown; |
| 616 | + const task = tasks.runDetachedTask("join introduction", async () => { |
| 617 | + await sendFinished.promise; |
| 618 | + if (transient) { |
| 619 | + await transient.release({ mode: "persist" }); |
| 620 | + } else { |
| 621 | + try { |
| 622 | + lateLease = await acquireSharedMatrixClient({ auth, startClient: false }); |
| 623 | + } catch (error) { |
| 624 | + acquisitionError = error; |
| 625 | + } |
| 626 | + } |
| 627 | + taskFinished(); |
| 628 | + }); |
| 629 | + monitor.registerMonitorRetirement({ |
| 630 | + closeTaskAdmission: () => { |
| 631 | + tasks.close(); |
| 632 | + taskAdmissionClosed.resolve(); |
| 633 | + }, |
| 634 | + detachListeners: vi.fn(), |
| 635 | + // The escape is only released in finally so a failing assertion still joins every task. |
| 636 | + waitForTasks: () => Promise.race([tasks.waitForIdle(), releaseTestDrain.promise]), |
| 637 | + cleanup: vi.fn(), |
| 638 | + }); |
| 639 | + |
| 640 | + const retirement = |
| 641 | + retirementOwner === "account stop" |
| 642 | + ? stopSharedClientForAccount(auth) |
| 643 | + : monitor.release({ mode: "persist" }); |
| 644 | + await taskAdmissionClosed.promise; |
| 645 | + sendFinished.resolve(); |
| 646 | + try { |
| 647 | + await vi.waitFor(() => { |
| 648 | + expect(taskFinished).toHaveBeenCalledOnce(); |
| 649 | + }); |
| 650 | + await retirement; |
| 651 | + if (operation === "late acquisition") { |
| 652 | + expect(acquisitionError).toMatchObject({ name: "AbortError" }); |
| 653 | + expect(lateLease).toBeUndefined(); |
| 654 | + } |
| 655 | + expect(client.stopAndPersist).toHaveBeenCalledOnce(); |
| 656 | + expect(client.stopWithoutPersist).not.toHaveBeenCalled(); |
| 657 | + expect(logger.warn).not.toHaveBeenCalled(); |
| 658 | + } finally { |
| 659 | + releaseTestDrain.resolve(); |
| 660 | + await Promise.all([task, retirement]); |
| 661 | + await lateLease?.release(); |
| 662 | + } |
| 663 | + }, |
| 664 | + ); |
| 665 | + |
| 666 | + it("rejects retained async acquisitions after the owning monitor task settles", async () => { |
| 667 | + const client = createMockClient("main"); |
| 668 | + createMatrixClientMock.mockResolvedValue(client); |
| 669 | + const auth = authFor("main"); |
| 670 | + const monitor = await acquireSharedMatrixClient({ auth, role: "monitor", startClient: false }); |
| 671 | + const tasks = createMatrixMonitorTaskRunner({ |
| 672 | + logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn() }, |
| 673 | + logVerboseMessage: vi.fn(), |
| 674 | + }); |
| 675 | + const resume = createDeferred<void>(); |
| 676 | + let retained: Promise<unknown> | undefined; |
| 677 | + let retainedLease: Awaited<ReturnType<typeof acquireSharedMatrixClient>> | undefined; |
| 678 | + await tasks.runDetachedTask("retained continuation", async () => { |
| 679 | + retained = resume.promise.then(async () => { |
| 680 | + try { |
| 681 | + retainedLease = await acquireSharedMatrixClient({ auth, startClient: false }); |
| 682 | + return null; |
| 683 | + } catch (error) { |
| 684 | + return error; |
| 685 | + } |
| 686 | + }); |
| 687 | + }); |
| 688 | + resume.resolve(); |
| 689 | + try { |
| 690 | + await expect(retained).resolves.toMatchObject({ name: "AbortError" }); |
| 691 | + const unrelated = await acquireSharedMatrixClient({ auth, startClient: false }); |
| 692 | + expect(unrelated.client).toBe(client); |
| 693 | + await unrelated.release(); |
| 694 | + } finally { |
| 695 | + await retained; |
| 696 | + await retainedLease?.release(); |
| 697 | + await monitor.release(); |
| 698 | + } |
| 699 | + }); |
| 700 | + |
| 701 | + it.each( |
| 702 | + ["authentication", "client creation"].flatMap((phase) => |
| 703 | + ["monitor", "caller"].map((abortOwner) => ({ phase, abortOwner })), |
| 704 | + ), |
| 705 | + )( |
| 706 | + "rejects $abortOwner cancellation during $phase before admitting a lease", |
| 707 | + async ({ phase, abortOwner }) => { |
| 708 | + const auth = authFor("main"); |
| 709 | + const client = createMockClient("main"); |
| 710 | + const authReady = createDeferred<MatrixAuth>(); |
| 711 | + const clientReady = createDeferred<typeof client>(); |
| 712 | + resolveMatrixAuthMock.mockReturnValue( |
| 713 | + phase === "authentication" ? authReady.promise : Promise.resolve(auth), |
| 714 | + ); |
| 715 | + createMatrixClientMock.mockReturnValue( |
| 716 | + phase === "client creation" ? clientReady.promise : Promise.resolve(client), |
| 717 | + ); |
| 718 | + const tasks = createMatrixMonitorTaskRunner({ |
| 719 | + logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn() }, |
| 720 | + logVerboseMessage: vi.fn(), |
| 721 | + }); |
| 722 | + const caller = new AbortController(); |
| 723 | + let lease: Awaited<ReturnType<typeof acquireSharedMatrixClient>> | undefined; |
| 724 | + let acquisitionError: unknown; |
| 725 | + const task = tasks.runDetachedTask("pending acquisition", async () => { |
| 726 | + try { |
| 727 | + lease = await acquireSharedMatrixClient({ |
| 728 | + cfg: TEST_CFG, |
| 729 | + accountId: "main", |
| 730 | + startClient: false, |
| 731 | + abortSignal: caller.signal, |
| 732 | + }); |
| 733 | + } catch (error) { |
| 734 | + acquisitionError = error; |
| 735 | + } |
| 736 | + }); |
| 737 | + try { |
| 738 | + await vi.waitFor(() => { |
| 739 | + expect( |
| 740 | + phase === "authentication" ? resolveMatrixAuthMock : createMatrixClientMock, |
| 741 | + ).toHaveBeenCalledOnce(); |
| 742 | + }); |
| 743 | + if (abortOwner === "monitor") { |
| 744 | + tasks.close(); |
| 745 | + } else { |
| 746 | + caller.abort(); |
| 747 | + } |
| 748 | + authReady.resolve(auth); |
| 749 | + clientReady.resolve(client); |
| 750 | + await task; |
| 751 | + expect(acquisitionError).toMatchObject({ name: "AbortError" }); |
| 752 | + expect(lease).toBeUndefined(); |
| 753 | + expect(client.start).not.toHaveBeenCalled(); |
| 754 | + if (phase === "authentication") { |
| 755 | + expect(createMatrixClientMock).not.toHaveBeenCalled(); |
| 756 | + } else { |
| 757 | + expect(client.stopWithoutPersist).toHaveBeenCalledOnce(); |
| 758 | + } |
| 759 | + } finally { |
| 760 | + authReady.resolve(auth); |
| 761 | + clientReady.resolve(client); |
| 762 | + await task; |
| 763 | + await lease?.release(); |
| 764 | + } |
| 765 | + }, |
| 766 | + ); |
| 767 | + |
588 | 768 | it("keeps shared sync open until the final monitor lease is released", async () => { |
589 | 769 | const callOrder: string[] = []; |
590 | 770 | const client = createMockClient("main", callOrder); |
|
0 commit comments