-
Notifications
You must be signed in to change notification settings - Fork 5.8k
fix(connect): repair drifted managed links #11911
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Gigioxx
wants to merge
2
commits into
pingdotgg:main
Choose a base branch
from
Gigioxx:t3code/fix-reported-browser-bug
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,219 @@ | ||
| import type { Discovery } from "@t3tools/client-runtime/relay"; | ||
| import { EnvironmentId } from "@t3tools/contracts"; | ||
| import * as Option from "effect/Option"; | ||
| import * as Cause from "effect/Cause"; | ||
| import { AsyncResult } from "effect/unstable/reactivity"; | ||
| import { act, useLayoutEffect } from "react"; | ||
| import { create, type ReactTestRenderer } from "react-test-renderer"; | ||
| import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test"; | ||
|
|
||
| const mocks = vi.hoisted(() => ({ | ||
| getToken: vi.fn(), | ||
| link: vi.fn(), | ||
| unlink: vi.fn(), | ||
| preferences: vi.fn(), | ||
| refresh: vi.fn(), | ||
| refreshRelay: vi.fn(), | ||
| toast: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("@clerk/react", () => ({ | ||
| useAuth: () => ({ getToken: mocks.getToken, isSignedIn: true }), | ||
| })); | ||
| vi.mock("../components/ui/toast", () => ({ toastManager: { add: mocks.toast } })); | ||
| vi.mock("../state/relay", () => ({ relayEnvironmentDiscovery: { refresh: mocks.refreshRelay } })); | ||
| vi.mock("../state/use-atom-command", () => ({ useAtomCommand: (command: unknown) => command })); | ||
| vi.mock("./linkEnvironmentAtoms", () => ({ | ||
| linkPrimaryEnvironment: mocks.link, | ||
| unlinkPrimaryEnvironment: mocks.unlink, | ||
| updatePrimaryEnvironmentPreferences: mocks.preferences, | ||
| })); | ||
| vi.mock("./primaryCloudLinkState", () => ({ usePrimaryCloudLinkState: () => linkState })); | ||
| vi.mock("../state/environments", () => ({ useRelayEnvironmentDiscovery: () => discovery })); | ||
| vi.mock("./publicConfig", () => ({ resolveRelayClerkTokenOptions: () => ({}) })); | ||
|
|
||
| import { useCloudLinkController } from "./useCloudLinkController"; | ||
|
|
||
| const target = { | ||
| environmentId: EnvironmentId.make("test-environment"), | ||
| label: "Test environment", | ||
| httpBaseUrl: "http://localhost:1234", | ||
| wsBaseUrl: "ws://localhost:1234/ws", | ||
| }; | ||
| const linkState = { | ||
| target, | ||
| data: { linked: true, managedTunnelActive: true, publishAgentActivity: true }, | ||
| refresh: mocks.refresh, | ||
| error: null, | ||
| isPending: false, | ||
| }; | ||
| let discovery: Discovery.RelayEnvironmentDiscoveryState; | ||
| let renderer: ReactTestRenderer | undefined; | ||
| let controller: ReturnType<typeof useCloudLinkController>; | ||
|
|
||
| function Harness() { | ||
| const current = useCloudLinkController(); | ||
| useLayoutEffect(() => { | ||
| controller = current; | ||
| }); | ||
| return null; | ||
| } | ||
|
|
||
| beforeEach(async () => { | ||
| vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); | ||
| vi.clearAllMocks(); | ||
| linkState.data = { linked: true, managedTunnelActive: true, publishAgentActivity: true }; | ||
| discovery = { | ||
| environments: new Map([ | ||
| [ | ||
| target.environmentId, | ||
| { | ||
| environment: { | ||
| environmentId: target.environmentId, | ||
| label: target.label, | ||
| endpoint: { | ||
| httpBaseUrl: target.httpBaseUrl, | ||
| wsBaseUrl: target.wsBaseUrl, | ||
| providerKind: "manual", | ||
| }, | ||
| linkedAt: "2026-09-15T12:00:00.000Z", | ||
| }, | ||
| availability: "online", | ||
| status: Option.none(), | ||
| error: Option.none(), | ||
| }, | ||
| ], | ||
| ]), | ||
| refreshing: false, | ||
| offline: false, | ||
| error: Option.none(), | ||
| }; | ||
| mocks.getToken.mockResolvedValue("test-token"); | ||
| for (const command of [mocks.link, mocks.unlink, mocks.preferences, mocks.refreshRelay]) { | ||
| command.mockResolvedValue(AsyncResult.success(undefined)); | ||
| } | ||
| await act(async () => { | ||
| renderer = create(<Harness />); | ||
| }); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await act(async () => renderer?.unmount()); | ||
| vi.unstubAllGlobals(); | ||
| }); | ||
|
|
||
| it("relinks a publish-only relay record even when local T3 Connect is already on", async () => { | ||
| expect(controller.managedTunnelOutOfSync).toBe(true); | ||
| await act(async () => { | ||
| expect(await controller.reconcileCloudState({ managedTunnel: true, publish: true })).toBe(true); | ||
| }); | ||
| expect(mocks.link).toHaveBeenCalledWith({ target, clerkToken: "test-token", mode: "managed" }); | ||
| expect(mocks.preferences).toHaveBeenCalledWith({ target, publishAgentActivity: true }); | ||
| expect(mocks.refreshRelay).toHaveBeenCalledOnce(); | ||
| }); | ||
|
|
||
| it("relinks when discovery temporarily clears the drifted record during refresh", async () => { | ||
| discovery = { ...discovery, environments: new Map(), refreshing: true }; | ||
| await act(async () => renderer?.update(<Harness />)); | ||
| await act(async () => { | ||
| expect(await controller.reconcileCloudState({ managedTunnel: true, publish: false })).toBe( | ||
| true, | ||
| ); | ||
| }); | ||
| expect(mocks.link).toHaveBeenCalledWith({ target, clerkToken: "test-token", mode: "managed" }); | ||
| expect(mocks.preferences).toHaveBeenCalledWith({ target, publishAgentActivity: false }); | ||
| }); | ||
|
|
||
| it.each(["healthy", "unknown"])( | ||
| "keeps publish changes cheap when the relay mode is %s", | ||
| async (state) => { | ||
| discovery = { | ||
| ...discovery, | ||
| environments: | ||
| state === "unknown" | ||
| ? new Map() | ||
| : new Map( | ||
| [...discovery.environments].map(([id, entry]) => [ | ||
| id, | ||
| { | ||
| ...entry, | ||
| environment: { | ||
| ...entry.environment, | ||
| endpoint: { ...entry.environment.endpoint, providerKind: "cloudflare_tunnel" }, | ||
| }, | ||
| }, | ||
| ]), | ||
| ), | ||
| }; | ||
| await act(async () => renderer?.update(<Harness />)); | ||
| expect(controller.managedTunnelOutOfSync).toBe(false); | ||
| await act(async () => { | ||
| expect(await controller.reconcileCloudState({ managedTunnel: true, publish: false })).toBe( | ||
| true, | ||
| ); | ||
| }); | ||
| expect(mocks.link).not.toHaveBeenCalled(); | ||
| expect(mocks.preferences).toHaveBeenCalledWith({ target, publishAgentActivity: false }); | ||
| }, | ||
| ); | ||
|
|
||
| it("allows an explicit repair without discovery and preserves disabled publishing", async () => { | ||
| discovery = { ...discovery, environments: new Map() }; | ||
| linkState.data.publishAgentActivity = false; | ||
| await act(async () => renderer?.update(<Harness />)); | ||
| await act(async () => { | ||
| expect( | ||
| await controller.reconcileCloudState( | ||
| { managedTunnel: true, publish: false }, | ||
| { forceRelink: true }, | ||
| ), | ||
| ).toBe(true); | ||
| }); | ||
| expect(mocks.link).toHaveBeenCalledWith({ target, clerkToken: "test-token", mode: "managed" }); | ||
| expect(mocks.preferences).toHaveBeenCalledWith({ target, publishAgentActivity: false }); | ||
| }); | ||
|
|
||
| it("keeps publishing on when disabling a managed tunnel", async () => { | ||
| await act(async () => { | ||
| expect(await controller.reconcileCloudState({ managedTunnel: false, publish: true })).toBe( | ||
| true, | ||
| ); | ||
| }); | ||
| expect(mocks.link).toHaveBeenCalledWith({ | ||
| target, | ||
| clerkToken: "test-token", | ||
| mode: "publish_only", | ||
| }); | ||
| expect(mocks.unlink).not.toHaveBeenCalled(); | ||
| expect(mocks.preferences).toHaveBeenCalledWith({ target, publishAgentActivity: true }); | ||
| }); | ||
|
|
||
| it("fully unlinks when both capabilities are off, even with relay drift", async () => { | ||
| await act(async () => { | ||
| expect(await controller.reconcileCloudState({ managedTunnel: false, publish: false })).toBe( | ||
| true, | ||
| ); | ||
| }); | ||
| expect(mocks.unlink).toHaveBeenCalledWith({ target, clerkToken: "test-token" }); | ||
| expect(mocks.link).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("reports a failed repair without changing publishing and refreshes local state", async () => { | ||
| const consoleError = vi.spyOn(console, "error").mockImplementation(() => undefined); | ||
| mocks.link.mockResolvedValue(AsyncResult.failure(Cause.fail(new Error("Relay unavailable")))); | ||
| try { | ||
| await act(async () => { | ||
| expect( | ||
| await controller.reconcileCloudState( | ||
| { managedTunnel: true, publish: true }, | ||
| { forceRelink: true }, | ||
| ), | ||
| ).toBe(false); | ||
| }); | ||
| expect(controller.operationError).toBe("Relay unavailable"); | ||
| expect(mocks.preferences).not.toHaveBeenCalled(); | ||
| expect(mocks.refresh).toHaveBeenCalledOnce(); | ||
| } finally { | ||
| consoleError.mockRestore(); | ||
| } | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟠 High
cloud/useCloudLinkController.ts:133Changing Publish activity during discovery refresh reports success without relinking a drifted environment, leaving the relay
publish-onlyeven though local state still says managed. Discovery clearsenvironments, makingmanagedTunnelOutOfSyncfalse; includerelayDiscovery.refreshingin the relink condition (or disable these controls while refreshing) so the managed mode is restored.🤖 Copy this AI Prompt to have your agent fix this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Confirmed and fixed in 7db4055. Reconciliation now relinks when refresh has temporarily cleared the environment record. A known matching record still avoids unnecessary relinking while status probes finish. The new regression failed before the fix and now passes; all 13 affected tests, targeted lint/formatting, and web typecheck pass.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I'm unable to act on this request because you do not have permissions within this repository.