|
| 1 | +import { describe, expect, it, beforeEach } from "vitest"; |
| 2 | + |
| 3 | +/** |
| 4 | + * Tests for the `enabled` prop in useAgent hook. |
| 5 | + * |
| 6 | + * The `enabled` prop follows the React Query pattern for conditional connections: |
| 7 | + * - When `enabled` is `false`, the WebSocket connection is not established |
| 8 | + * - When `enabled` is `true` (default), the connection is established normally |
| 9 | + * - When `enabled` transitions from `false` to `true`, the connection is opened |
| 10 | + * - When `enabled` transitions from `true` to `false`, the connection is closed |
| 11 | + * |
| 12 | + * @see https://github.com/cloudflare/agents/issues/533 |
| 13 | + */ |
| 14 | + |
| 15 | +describe("useAgent enabled prop (issue #533)", () => { |
| 16 | + describe("Type definitions", () => { |
| 17 | + it("should accept enabled as an optional boolean prop", () => { |
| 18 | + // This is a compile-time check - if UseAgentOptions doesn't include enabled, |
| 19 | + // TypeScript would fail. We're just documenting the expected type here. |
| 20 | + type ExpectedOptions = { |
| 21 | + agent: string; |
| 22 | + name?: string; |
| 23 | + enabled?: boolean; |
| 24 | + }; |
| 25 | + |
| 26 | + const options: ExpectedOptions = { |
| 27 | + agent: "test-agent", |
| 28 | + enabled: false |
| 29 | + }; |
| 30 | + |
| 31 | + expect(options.enabled).toBe(false); |
| 32 | + }); |
| 33 | + |
| 34 | + it("should default enabled to true when not specified", () => { |
| 35 | + const optionsWithEnabled = { agent: "test", enabled: true }; |
| 36 | + const optionsWithoutEnabled: { agent: string; enabled?: boolean } = { |
| 37 | + agent: "test" |
| 38 | + }; |
| 39 | + |
| 40 | + // Default behavior: enabled should be true when undefined |
| 41 | + const defaultEnabled = optionsWithoutEnabled.enabled ?? true; |
| 42 | + expect(defaultEnabled).toBe(true); |
| 43 | + expect(optionsWithEnabled.enabled).toBe(true); |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + describe("Connection lifecycle", () => { |
| 48 | + it("should start closed when enabled is false", () => { |
| 49 | + // When enabled=false, startClosed should be passed as true to usePartySocket |
| 50 | + const enabled = false; |
| 51 | + const startClosed = !enabled; |
| 52 | + |
| 53 | + expect(startClosed).toBe(true); |
| 54 | + }); |
| 55 | + |
| 56 | + it("should start open when enabled is true", () => { |
| 57 | + // When enabled=true (default), startClosed should be false |
| 58 | + const enabled = true; |
| 59 | + const startClosed = !enabled; |
| 60 | + |
| 61 | + expect(startClosed).toBe(false); |
| 62 | + }); |
| 63 | + |
| 64 | + it("should start open when enabled is undefined (default)", () => { |
| 65 | + // Simulate the default behavior when enabled is not provided |
| 66 | + const enabledFromOptions: boolean | undefined = undefined; |
| 67 | + const enabled = enabledFromOptions ?? true; |
| 68 | + const startClosed = !enabled; |
| 69 | + |
| 70 | + expect(startClosed).toBe(false); |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + describe("State transition logic", () => { |
| 75 | + let wasEnabled: boolean; |
| 76 | + let currentEnabled: boolean; |
| 77 | + let reconnectCalled: boolean; |
| 78 | + let closeCalled: boolean; |
| 79 | + |
| 80 | + beforeEach(() => { |
| 81 | + reconnectCalled = false; |
| 82 | + closeCalled = false; |
| 83 | + }); |
| 84 | + |
| 85 | + function simulateTransition(prev: boolean, current: boolean) { |
| 86 | + wasEnabled = prev; |
| 87 | + currentEnabled = current; |
| 88 | + |
| 89 | + // Simulate the useEffect logic |
| 90 | + if (!wasEnabled && currentEnabled) { |
| 91 | + reconnectCalled = true; |
| 92 | + } else if (wasEnabled && !currentEnabled) { |
| 93 | + closeCalled = true; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + it("should call reconnect when transitioning from disabled to enabled", () => { |
| 98 | + simulateTransition(false, true); |
| 99 | + |
| 100 | + expect(reconnectCalled).toBe(true); |
| 101 | + expect(closeCalled).toBe(false); |
| 102 | + }); |
| 103 | + |
| 104 | + it("should call close when transitioning from enabled to disabled", () => { |
| 105 | + simulateTransition(true, false); |
| 106 | + |
| 107 | + expect(reconnectCalled).toBe(false); |
| 108 | + expect(closeCalled).toBe(true); |
| 109 | + }); |
| 110 | + |
| 111 | + it("should not call either when staying enabled", () => { |
| 112 | + simulateTransition(true, true); |
| 113 | + |
| 114 | + expect(reconnectCalled).toBe(false); |
| 115 | + expect(closeCalled).toBe(false); |
| 116 | + }); |
| 117 | + |
| 118 | + it("should not call either when staying disabled", () => { |
| 119 | + simulateTransition(false, false); |
| 120 | + |
| 121 | + expect(reconnectCalled).toBe(false); |
| 122 | + expect(closeCalled).toBe(false); |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + describe("Integration with other options", () => { |
| 127 | + it("should work alongside startClosed option (enabled takes precedence)", () => { |
| 128 | + // If user passes both startClosed and enabled, enabled should win |
| 129 | + // because it's destructured before restOptions spread |
| 130 | + const options = { |
| 131 | + agent: "test", |
| 132 | + enabled: false, |
| 133 | + startClosed: false // This should be overridden |
| 134 | + }; |
| 135 | + |
| 136 | + const { enabled, startClosed: _userStartClosed } = options; |
| 137 | + const effectiveStartClosed = !enabled; // enabled takes precedence |
| 138 | + |
| 139 | + expect(effectiveStartClosed).toBe(true); |
| 140 | + }); |
| 141 | + |
| 142 | + it("should preserve other options when enabled is specified", () => { |
| 143 | + const options: { |
| 144 | + agent: string; |
| 145 | + name: string; |
| 146 | + enabled: boolean; |
| 147 | + cacheTtl: number; |
| 148 | + queryDeps: string[]; |
| 149 | + } = { |
| 150 | + agent: "test-agent", |
| 151 | + name: "instance-1", |
| 152 | + enabled: false, |
| 153 | + cacheTtl: 60000, |
| 154 | + queryDeps: ["dep1"] |
| 155 | + }; |
| 156 | + |
| 157 | + const { queryDeps, cacheTtl, enabled, ...restOptions } = options; |
| 158 | + |
| 159 | + expect(restOptions.agent).toBe("test-agent"); |
| 160 | + expect(restOptions.name).toBe("instance-1"); |
| 161 | + expect(enabled).toBe(false); |
| 162 | + expect(cacheTtl).toBe(60000); |
| 163 | + expect(queryDeps).toEqual(["dep1"]); |
| 164 | + }); |
| 165 | + }); |
| 166 | + |
| 167 | + describe("Common use cases", () => { |
| 168 | + it("should support authentication-based conditional connection", () => { |
| 169 | + // Simulate: only connect when user is authenticated |
| 170 | + const isAuthenticated = false; |
| 171 | + |
| 172 | + const options = { |
| 173 | + agent: "chat-agent", |
| 174 | + enabled: isAuthenticated |
| 175 | + }; |
| 176 | + |
| 177 | + expect(options.enabled).toBe(false); |
| 178 | + expect(!options.enabled).toBe(true); // startClosed would be true |
| 179 | + }); |
| 180 | + |
| 181 | + it("should support feature flag based conditional connection", () => { |
| 182 | + // Simulate: only connect when feature is enabled |
| 183 | + const featureEnabled = true; |
| 184 | + |
| 185 | + const options = { |
| 186 | + agent: "experimental-agent", |
| 187 | + enabled: featureEnabled |
| 188 | + }; |
| 189 | + |
| 190 | + expect(options.enabled).toBe(true); |
| 191 | + expect(!options.enabled).toBe(false); // startClosed would be false |
| 192 | + }); |
| 193 | + |
| 194 | + it("should support lazy loading pattern", () => { |
| 195 | + // Simulate: connect only when user navigates to a specific section |
| 196 | + let userOnAgentPage = false; |
| 197 | + |
| 198 | + const getOptions = () => ({ |
| 199 | + agent: "page-agent", |
| 200 | + enabled: userOnAgentPage |
| 201 | + }); |
| 202 | + |
| 203 | + expect(getOptions().enabled).toBe(false); |
| 204 | + |
| 205 | + // User navigates to the page |
| 206 | + userOnAgentPage = true; |
| 207 | + expect(getOptions().enabled).toBe(true); |
| 208 | + }); |
| 209 | + }); |
| 210 | +}); |
0 commit comments