|
| 1 | +import { describe, it, expect, beforeEach, vi, afterEach } from "vitest"; |
| 2 | +import type { Resource } from "@modelcontextprotocol/sdk/types.js"; |
| 3 | +import { ResourceSubscriptionsState } from "@inspector/core/mcp/state/resourceSubscriptionsState"; |
| 4 | +import { ManagedResourcesState } from "@inspector/core/mcp/state/managedResourcesState"; |
| 5 | +import { FakeInspectorClient } from "@inspector/core/mcp/__tests__/fakeInspectorClient"; |
| 6 | +import type { InspectorResourceSubscription } from "@inspector/core/mcp/types"; |
| 7 | + |
| 8 | +function resource(uri: string, extras: Partial<Resource> = {}): Resource { |
| 9 | + return { uri, name: uri, ...extras }; |
| 10 | +} |
| 11 | + |
| 12 | +function waitForSubscriptionsChange( |
| 13 | + state: ResourceSubscriptionsState, |
| 14 | +): Promise<InspectorResourceSubscription[]> { |
| 15 | + return new Promise((resolve) => { |
| 16 | + state.addEventListener("subscriptionsChange", (e) => resolve(e.detail), { |
| 17 | + once: true, |
| 18 | + }); |
| 19 | + }); |
| 20 | +} |
| 21 | + |
| 22 | +describe("ResourceSubscriptionsState", () => { |
| 23 | + let client: FakeInspectorClient; |
| 24 | + |
| 25 | + beforeEach(() => { |
| 26 | + vi.useFakeTimers(); |
| 27 | + vi.setSystemTime(new Date("2026-05-19T10:00:00Z")); |
| 28 | + client = new FakeInspectorClient({ status: "connected" }); |
| 29 | + }); |
| 30 | + |
| 31 | + afterEach(() => { |
| 32 | + vi.useRealTimers(); |
| 33 | + }); |
| 34 | + |
| 35 | + it("starts empty and getSubscriptions returns a defensive copy", () => { |
| 36 | + const state = new ResourceSubscriptionsState(client); |
| 37 | + expect(state.getSubscriptions()).toEqual([]); |
| 38 | + const a = state.getSubscriptions(); |
| 39 | + const b = state.getSubscriptions(); |
| 40 | + expect(a).not.toBe(b); |
| 41 | + }); |
| 42 | + |
| 43 | + it("rebuilds subscriptions from resourceSubscriptionsChange events", async () => { |
| 44 | + const state = new ResourceSubscriptionsState(client); |
| 45 | + const changePromise = waitForSubscriptionsChange(state); |
| 46 | + client.dispatchTypedEvent("resourceSubscriptionsChange", [ |
| 47 | + "file:///a", |
| 48 | + "file:///b", |
| 49 | + ]); |
| 50 | + const next = await changePromise; |
| 51 | + expect(next).toEqual([ |
| 52 | + { resource: { uri: "file:///a", name: "file:///a" } }, |
| 53 | + { resource: { uri: "file:///b", name: "file:///b" } }, |
| 54 | + ]); |
| 55 | + }); |
| 56 | + |
| 57 | + it("resolves Resource references via ManagedResourcesState when provided", async () => { |
| 58 | + const resourcesState = new ManagedResourcesState(client); |
| 59 | + client.queueResourcePages({ |
| 60 | + resources: [resource("file:///a", { name: "Alpha", title: "Title A" })], |
| 61 | + }); |
| 62 | + await resourcesState.refresh(); |
| 63 | + |
| 64 | + const state = new ResourceSubscriptionsState(client, resourcesState); |
| 65 | + const changePromise = waitForSubscriptionsChange(state); |
| 66 | + client.dispatchTypedEvent("resourceSubscriptionsChange", [ |
| 67 | + "file:///a", |
| 68 | + "file:///unknown", |
| 69 | + ]); |
| 70 | + const next = await changePromise; |
| 71 | + expect(next[0].resource).toEqual({ |
| 72 | + uri: "file:///a", |
| 73 | + name: "Alpha", |
| 74 | + title: "Title A", |
| 75 | + }); |
| 76 | + // Unknown URI falls back to a synthetic Resource |
| 77 | + expect(next[1].resource).toEqual({ |
| 78 | + uri: "file:///unknown", |
| 79 | + name: "file:///unknown", |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + it("stamps lastUpdated on resourceUpdated for a tracked URI", async () => { |
| 84 | + const state = new ResourceSubscriptionsState(client); |
| 85 | + client.dispatchTypedEvent("resourceSubscriptionsChange", ["file:///a"]); |
| 86 | + expect(state.getSubscriptions()[0].lastUpdated).toBeUndefined(); |
| 87 | + |
| 88 | + const changePromise = waitForSubscriptionsChange(state); |
| 89 | + client.dispatchTypedEvent("resourceUpdated", { uri: "file:///a" }); |
| 90 | + const next = await changePromise; |
| 91 | + expect(next[0].lastUpdated).toEqual(new Date("2026-05-19T10:00:00Z")); |
| 92 | + }); |
| 93 | + |
| 94 | + it("ignores resourceUpdated for URIs that are not subscribed", () => { |
| 95 | + const state = new ResourceSubscriptionsState(client); |
| 96 | + client.dispatchTypedEvent("resourceSubscriptionsChange", ["file:///a"]); |
| 97 | + const handler = vi.fn(); |
| 98 | + state.addEventListener("subscriptionsChange", handler); |
| 99 | + client.dispatchTypedEvent("resourceUpdated", { |
| 100 | + uri: "file:///not-tracked", |
| 101 | + }); |
| 102 | + expect(handler).not.toHaveBeenCalled(); |
| 103 | + expect(state.getSubscriptions()[0].lastUpdated).toBeUndefined(); |
| 104 | + }); |
| 105 | + |
| 106 | + it("preserves lastUpdated across re-subscribes and drops it on unsubscribe", async () => { |
| 107 | + const state = new ResourceSubscriptionsState(client); |
| 108 | + client.dispatchTypedEvent("resourceSubscriptionsChange", [ |
| 109 | + "file:///a", |
| 110 | + "file:///b", |
| 111 | + ]); |
| 112 | + client.dispatchTypedEvent("resourceUpdated", { uri: "file:///a" }); |
| 113 | + expect(state.getSubscriptions()[0].lastUpdated).toBeInstanceOf(Date); |
| 114 | + |
| 115 | + // Unsubscribe from "a", subscribe to "c". lastUpdated for "a" is dropped. |
| 116 | + client.dispatchTypedEvent("resourceSubscriptionsChange", [ |
| 117 | + "file:///b", |
| 118 | + "file:///c", |
| 119 | + ]); |
| 120 | + expect(state.getSubscriptions().map((s) => s.resource.uri)).toEqual([ |
| 121 | + "file:///b", |
| 122 | + "file:///c", |
| 123 | + ]); |
| 124 | + expect(state.getSubscriptions().every((s) => !s.lastUpdated)).toBe(true); |
| 125 | + |
| 126 | + // Re-subscribe to "a" — no lastUpdated since the prior entry was dropped. |
| 127 | + client.dispatchTypedEvent("resourceSubscriptionsChange", [ |
| 128 | + "file:///a", |
| 129 | + "file:///b", |
| 130 | + "file:///c", |
| 131 | + ]); |
| 132 | + expect(state.getSubscriptions()[0].lastUpdated).toBeUndefined(); |
| 133 | + }); |
| 134 | + |
| 135 | + it("preserves lastUpdated when an unrelated URI is added", () => { |
| 136 | + const state = new ResourceSubscriptionsState(client); |
| 137 | + client.dispatchTypedEvent("resourceSubscriptionsChange", ["file:///a"]); |
| 138 | + client.dispatchTypedEvent("resourceUpdated", { uri: "file:///a" }); |
| 139 | + const stampedAt = state.getSubscriptions()[0].lastUpdated; |
| 140 | + expect(stampedAt).toBeInstanceOf(Date); |
| 141 | + |
| 142 | + client.dispatchTypedEvent("resourceSubscriptionsChange", [ |
| 143 | + "file:///a", |
| 144 | + "file:///b", |
| 145 | + ]); |
| 146 | + const subs = state.getSubscriptions(); |
| 147 | + expect(subs[0].lastUpdated).toEqual(stampedAt); |
| 148 | + expect(subs[1].lastUpdated).toBeUndefined(); |
| 149 | + }); |
| 150 | + |
| 151 | + it("re-resolves Resource references when ManagedResourcesState refreshes", async () => { |
| 152 | + const resourcesState = new ManagedResourcesState(client); |
| 153 | + const state = new ResourceSubscriptionsState(client, resourcesState); |
| 154 | + client.dispatchTypedEvent("resourceSubscriptionsChange", ["file:///a"]); |
| 155 | + expect(state.getSubscriptions()[0].resource.name).toBe("file:///a"); |
| 156 | + |
| 157 | + client.queueResourcePages({ |
| 158 | + resources: [resource("file:///a", { name: "Resolved Name" })], |
| 159 | + }); |
| 160 | + const changePromise = waitForSubscriptionsChange(state); |
| 161 | + await resourcesState.refresh(); |
| 162 | + const next = await changePromise; |
| 163 | + expect(next[0].resource.name).toBe("Resolved Name"); |
| 164 | + }); |
| 165 | + |
| 166 | + it("does not re-emit on resourcesChange when no URIs are subscribed", async () => { |
| 167 | + const resourcesState = new ManagedResourcesState(client); |
| 168 | + const state = new ResourceSubscriptionsState(client, resourcesState); |
| 169 | + const handler = vi.fn(); |
| 170 | + state.addEventListener("subscriptionsChange", handler); |
| 171 | + |
| 172 | + client.queueResourcePages({ resources: [resource("file:///a")] }); |
| 173 | + await resourcesState.refresh(); |
| 174 | + expect(handler).not.toHaveBeenCalled(); |
| 175 | + }); |
| 176 | + |
| 177 | + it("clears subscriptions on statusChange to disconnected", async () => { |
| 178 | + const state = new ResourceSubscriptionsState(client); |
| 179 | + client.dispatchTypedEvent("resourceSubscriptionsChange", ["file:///a"]); |
| 180 | + expect(state.getSubscriptions()).toHaveLength(1); |
| 181 | + |
| 182 | + const changePromise = waitForSubscriptionsChange(state); |
| 183 | + client.setStatus("disconnected"); |
| 184 | + const next = await changePromise; |
| 185 | + expect(next).toEqual([]); |
| 186 | + expect(state.getSubscriptions()).toEqual([]); |
| 187 | + }); |
| 188 | + |
| 189 | + it("does not clear subscriptions on non-disconnected status changes", () => { |
| 190 | + const state = new ResourceSubscriptionsState(client); |
| 191 | + client.dispatchTypedEvent("resourceSubscriptionsChange", ["file:///a"]); |
| 192 | + client.setStatus("error"); |
| 193 | + expect(state.getSubscriptions()).toHaveLength(1); |
| 194 | + }); |
| 195 | + |
| 196 | + it("destroy unsubscribes from client and resources state events", () => { |
| 197 | + const resourcesState = new ManagedResourcesState(client); |
| 198 | + const state = new ResourceSubscriptionsState(client, resourcesState); |
| 199 | + client.dispatchTypedEvent("resourceSubscriptionsChange", ["file:///a"]); |
| 200 | + expect(state.getSubscriptions()).toHaveLength(1); |
| 201 | + |
| 202 | + state.destroy(); |
| 203 | + expect(state.getSubscriptions()).toEqual([]); |
| 204 | + |
| 205 | + // Further events from the client must not affect the destroyed state. |
| 206 | + const handler = vi.fn(); |
| 207 | + state.addEventListener("subscriptionsChange", handler); |
| 208 | + client.dispatchTypedEvent("resourceSubscriptionsChange", [ |
| 209 | + "file:///a", |
| 210 | + "file:///b", |
| 211 | + ]); |
| 212 | + client.dispatchTypedEvent("resourceUpdated", { uri: "file:///a" }); |
| 213 | + expect(handler).not.toHaveBeenCalled(); |
| 214 | + expect(state.getSubscriptions()).toEqual([]); |
| 215 | + }); |
| 216 | + |
| 217 | + it("destroy is idempotent", () => { |
| 218 | + const state = new ResourceSubscriptionsState(client); |
| 219 | + state.destroy(); |
| 220 | + expect(() => state.destroy()).not.toThrow(); |
| 221 | + }); |
| 222 | +}); |
0 commit comments