|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest" |
| 2 | +import { rpcEventClient, setDebug } from "../src/event-client" |
| 3 | + |
| 4 | +describe("rpcEventClient", () => { |
| 5 | + beforeEach(() => { |
| 6 | + // Reset is handled by test/setup.ts |
| 7 | + }) |
| 8 | + |
| 9 | + describe("proxy behavior", () => { |
| 10 | + it("forwards method calls to the real client", () => { |
| 11 | + expect(typeof rpcEventClient.emit).toBe("function") |
| 12 | + expect(typeof rpcEventClient.on).toBe("function") |
| 13 | + expect(typeof rpcEventClient.getPluginId).toBe("function") |
| 14 | + }) |
| 15 | + |
| 16 | + it("returns correct pluginId", () => { |
| 17 | + expect(rpcEventClient.getPluginId()).toBe("effect-rpc") |
| 18 | + }) |
| 19 | + |
| 20 | + it("maintains reference after multiple accesses", () => { |
| 21 | + const emit1 = rpcEventClient.emit |
| 22 | + const emit2 = rpcEventClient.emit |
| 23 | + // Functions should be equivalent (bound to same client) |
| 24 | + expect(typeof emit1).toBe("function") |
| 25 | + expect(typeof emit2).toBe("function") |
| 26 | + }) |
| 27 | + }) |
| 28 | + |
| 29 | + describe("singleton pattern", () => { |
| 30 | + it("caches client in globalThis", () => { |
| 31 | + // Access the client |
| 32 | + rpcEventClient.getPluginId() |
| 33 | + |
| 34 | + // Check globalThis has the client |
| 35 | + expect(globalThis.__EFFECT_RPC_DEVTOOLS_CLIENT__).toBeDefined() |
| 36 | + }) |
| 37 | + |
| 38 | + it("reuses cached client", () => { |
| 39 | + const pluginId1 = rpcEventClient.getPluginId() |
| 40 | + const pluginId2 = rpcEventClient.getPluginId() |
| 41 | + |
| 42 | + expect(pluginId1).toBe(pluginId2) |
| 43 | + expect(pluginId1).toBe("effect-rpc") |
| 44 | + }) |
| 45 | + }) |
| 46 | +}) |
| 47 | + |
| 48 | +describe("setDebug", () => { |
| 49 | + beforeEach(() => { |
| 50 | + // Reset is handled by test/setup.ts |
| 51 | + }) |
| 52 | + |
| 53 | + it("sets debug flag in globalThis", () => { |
| 54 | + expect(globalThis.__EFFECT_RPC_DEVTOOLS_DEBUG__).toBeUndefined() |
| 55 | + |
| 56 | + setDebug(true) |
| 57 | + |
| 58 | + expect(globalThis.__EFFECT_RPC_DEVTOOLS_DEBUG__).toBe(true) |
| 59 | + }) |
| 60 | + |
| 61 | + it("recreates client when debug changes", () => { |
| 62 | + // Access client to create it |
| 63 | + rpcEventClient.getPluginId() |
| 64 | + const firstClient = globalThis.__EFFECT_RPC_DEVTOOLS_CLIENT__ |
| 65 | + |
| 66 | + // Change debug setting |
| 67 | + setDebug(true) |
| 68 | + const secondClient = globalThis.__EFFECT_RPC_DEVTOOLS_CLIENT__ |
| 69 | + |
| 70 | + expect(secondClient).not.toBe(firstClient) |
| 71 | + }) |
| 72 | + |
| 73 | + it("does not recreate client when debug value is same", () => { |
| 74 | + setDebug(true) |
| 75 | + const firstClient = globalThis.__EFFECT_RPC_DEVTOOLS_CLIENT__ |
| 76 | + |
| 77 | + setDebug(true) |
| 78 | + const secondClient = globalThis.__EFFECT_RPC_DEVTOOLS_CLIENT__ |
| 79 | + |
| 80 | + expect(secondClient).toBe(firstClient) |
| 81 | + }) |
| 82 | + |
| 83 | + it("proxy reflects new client after setDebug", () => { |
| 84 | + // Create initial client |
| 85 | + rpcEventClient.getPluginId() |
| 86 | + const initialClient = globalThis.__EFFECT_RPC_DEVTOOLS_CLIENT__ |
| 87 | + |
| 88 | + // Change debug |
| 89 | + setDebug(true) |
| 90 | + |
| 91 | + // Proxy should now use new client |
| 92 | + rpcEventClient.getPluginId() |
| 93 | + const currentClient = globalThis.__EFFECT_RPC_DEVTOOLS_CLIENT__ |
| 94 | + |
| 95 | + expect(currentClient).not.toBe(initialClient) |
| 96 | + }) |
| 97 | + |
| 98 | + it("toggles debug off", () => { |
| 99 | + setDebug(true) |
| 100 | + expect(globalThis.__EFFECT_RPC_DEVTOOLS_DEBUG__).toBe(true) |
| 101 | + |
| 102 | + setDebug(false) |
| 103 | + expect(globalThis.__EFFECT_RPC_DEVTOOLS_DEBUG__).toBe(false) |
| 104 | + }) |
| 105 | +}) |
| 106 | + |
| 107 | +describe("isDev detection", () => { |
| 108 | + beforeEach(() => { |
| 109 | + vi.unstubAllEnvs() |
| 110 | + }) |
| 111 | + |
| 112 | + it("creates client with enabled based on environment", () => { |
| 113 | + // In test environment, NODE_ENV is typically 'test', not 'development' |
| 114 | + // So the client should be created but may not be enabled |
| 115 | + rpcEventClient.getPluginId() |
| 116 | + expect(globalThis.__EFFECT_RPC_DEVTOOLS_CLIENT__).toBeDefined() |
| 117 | + }) |
| 118 | +}) |
0 commit comments