|
| 1 | +import { LitElement, html, nothing } from "lit"; |
| 2 | +import { html as staticHtml, unsafeStatic } from "lit/static-html.js"; |
| 3 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 4 | +import { McpAppUnmountGate } from "./mcp-app-unmount.ts"; |
| 5 | + |
| 6 | +type Deferred = { |
| 7 | + promise: Promise<void>; |
| 8 | + resolve: () => void; |
| 9 | +}; |
| 10 | + |
| 11 | +function deferred(): Deferred { |
| 12 | + let resolve!: () => void; |
| 13 | + const promise = new Promise<void>((promiseResolve) => { |
| 14 | + resolve = promiseResolve; |
| 15 | + }); |
| 16 | + return { promise, resolve }; |
| 17 | +} |
| 18 | + |
| 19 | +const targetTag = `test-mcp-app-unmount-target-${crypto.randomUUID()}`; |
| 20 | +const ownerTag = `test-mcp-app-unmount-owner-${crypto.randomUUID()}`; |
| 21 | +const siblingOwnerTag = `test-mcp-app-unmount-sibling-owner-${crypto.randomUUID()}`; |
| 22 | +const staticTargetTag = unsafeStatic(targetTag); |
| 23 | +const teardown = vi.fn<() => Promise<void>>(); |
| 24 | + |
| 25 | +class TestMcpAppUnmountTarget extends HTMLElement { |
| 26 | + restartCalls = 0; |
| 27 | + |
| 28 | + restartAfterTeardown() { |
| 29 | + this.restartCalls += 1; |
| 30 | + } |
| 31 | + |
| 32 | + teardown() { |
| 33 | + return teardown(); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +class TestMcpAppUnmountOwner extends LitElement { |
| 38 | + key = "initial"; |
| 39 | + private readonly gate = new McpAppUnmountGate(this, targetTag); |
| 40 | + |
| 41 | + show(key: string) { |
| 42 | + this.key = key; |
| 43 | + this.requestUpdate(); |
| 44 | + } |
| 45 | + |
| 46 | + override render() { |
| 47 | + const value = |
| 48 | + this.key === "initial" |
| 49 | + ? staticHtml`<${staticTargetTag}></${staticTargetTag}><span data-value="initial">initial</span>` |
| 50 | + : html`<span data-value=${this.key}>${this.key}</span>`; |
| 51 | + return this.gate.render(this.key, value, () => [this.renderRoot]); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +class TestMcpAppUnmountSiblingOwner extends LitElement { |
| 56 | + private includeLeaving = true; |
| 57 | + private readonly gate = new McpAppUnmountGate(this, targetTag); |
| 58 | + |
| 59 | + removeLeaving() { |
| 60 | + this.includeLeaving = false; |
| 61 | + this.requestUpdate(); |
| 62 | + } |
| 63 | + |
| 64 | + override render() { |
| 65 | + const value = staticHtml` |
| 66 | + ${ |
| 67 | + this.includeLeaving |
| 68 | + ? staticHtml`<div class="leaving"><${staticTargetTag}></${staticTargetTag}></div>` |
| 69 | + : nothing |
| 70 | + } |
| 71 | + <${staticTargetTag} class="retained"></${staticTargetTag}> |
| 72 | + `; |
| 73 | + return this.gate.render(this.includeLeaving ? "both" : "retained", value, () => |
| 74 | + this.renderRoot.querySelectorAll(".leaving"), |
| 75 | + ); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +customElements.define(targetTag, TestMcpAppUnmountTarget); |
| 80 | +customElements.define(ownerTag, TestMcpAppUnmountOwner); |
| 81 | +customElements.define(siblingOwnerTag, TestMcpAppUnmountSiblingOwner); |
| 82 | + |
| 83 | +afterEach(() => { |
| 84 | + document.body.replaceChildren(); |
| 85 | + teardown.mockReset(); |
| 86 | +}); |
| 87 | + |
| 88 | +describe("McpAppUnmountGate", () => { |
| 89 | + it("keeps the old subtree connected and coalesces replacements until teardown resolves", async () => { |
| 90 | + const pending = deferred(); |
| 91 | + teardown.mockReturnValue(pending.promise); |
| 92 | + const owner = document.createElement(ownerTag) as TestMcpAppUnmountOwner; |
| 93 | + document.body.append(owner); |
| 94 | + await owner.updateComplete; |
| 95 | + |
| 96 | + const target = owner.shadowRoot!.querySelector(targetTag)!; |
| 97 | + owner.show("intermediate"); |
| 98 | + await owner.updateComplete; |
| 99 | + expect(teardown).toHaveBeenCalledOnce(); |
| 100 | + expect(target.isConnected).toBe(true); |
| 101 | + expect(owner.shadowRoot!.querySelector("[data-value='initial']")).not.toBeNull(); |
| 102 | + |
| 103 | + owner.show("latest"); |
| 104 | + await owner.updateComplete; |
| 105 | + expect(teardown).toHaveBeenCalledOnce(); |
| 106 | + expect(owner.shadowRoot!.querySelector("[data-value='latest']")).toBeNull(); |
| 107 | + |
| 108 | + pending.resolve(); |
| 109 | + await expect |
| 110 | + .poll(() => owner.shadowRoot!.querySelector("[data-value='latest']")) |
| 111 | + .not.toBeNull(); |
| 112 | + expect(owner.shadowRoot!.querySelector(targetTag)).toBeNull(); |
| 113 | + expect(owner.shadowRoot!.querySelector("[data-value='intermediate']")).toBeNull(); |
| 114 | + }); |
| 115 | + |
| 116 | + it("restarts the original target when a pending transition rebounds", async () => { |
| 117 | + const pending = deferred(); |
| 118 | + teardown.mockReturnValueOnce(pending.promise).mockResolvedValue(undefined); |
| 119 | + const owner = document.createElement(ownerTag) as TestMcpAppUnmountOwner; |
| 120 | + document.body.append(owner); |
| 121 | + await owner.updateComplete; |
| 122 | + const original = owner.shadowRoot!.querySelector<TestMcpAppUnmountTarget>(targetTag)!; |
| 123 | + |
| 124 | + owner.show("intermediate"); |
| 125 | + await owner.updateComplete; |
| 126 | + owner.show("initial"); |
| 127 | + await owner.updateComplete; |
| 128 | + expect(original.isConnected).toBe(true); |
| 129 | + |
| 130 | + pending.resolve(); |
| 131 | + await expect.poll(() => original.restartCalls).toBe(1); |
| 132 | + expect(owner.shadowRoot!.querySelector(targetTag)).toBe(original); |
| 133 | + expect(teardown).toHaveBeenCalledOnce(); |
| 134 | + }); |
| 135 | + |
| 136 | + it("preserves retained siblings while removing a torn-down target", async () => { |
| 137 | + const pending = deferred(); |
| 138 | + teardown.mockReturnValue(pending.promise); |
| 139 | + const owner = document.createElement(siblingOwnerTag) as TestMcpAppUnmountSiblingOwner; |
| 140 | + document.body.append(owner); |
| 141 | + await owner.updateComplete; |
| 142 | + const leaving = owner.shadowRoot!.querySelector<TestMcpAppUnmountTarget>( |
| 143 | + `.leaving ${targetTag}`, |
| 144 | + )!; |
| 145 | + const retained = owner.shadowRoot!.querySelector<TestMcpAppUnmountTarget>(".retained")!; |
| 146 | + |
| 147 | + owner.removeLeaving(); |
| 148 | + await owner.updateComplete; |
| 149 | + expect(leaving.isConnected).toBe(true); |
| 150 | + expect(retained.isConnected).toBe(true); |
| 151 | + |
| 152 | + pending.resolve(); |
| 153 | + await expect.poll(() => owner.shadowRoot!.querySelector(".leaving")).toBeNull(); |
| 154 | + expect(owner.shadowRoot!.querySelector(".retained")).toBe(retained); |
| 155 | + expect(retained.restartCalls).toBe(0); |
| 156 | + expect(teardown).toHaveBeenCalledOnce(); |
| 157 | + }); |
| 158 | +}); |
0 commit comments