Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 88 additions & 4 deletions apps/web/src/components/ServerUpdateAction.test.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { ReactElement } from "react";
import { Children, isValidElement, type ReactElement, type ReactNode } from "react";
import { renderToStaticMarkup } from "react-dom/server";
import type { EnvironmentId } from "@t3tools/contracts";
import * as Cause from "effect/Cause";
import { AsyncResult } from "effect/unstable/reactivity";
import { beforeEach, describe, expect, it, vi } from "vite-plus/test";

const testState = vi.hoisted(() => ({
connectionPhase: "connected",
updateServer: vi.fn(),
toast: vi.fn(),
}));
Expand All @@ -16,6 +17,12 @@ vi.mock("~/hooks/useCopyToClipboard", () => ({
vi.mock("~/state/server", () => ({
serverEnvironment: { updateServer: Symbol("updateServer") },
}));
vi.mock("~/state/presentation", () => ({
useEnvironmentPresentation: () => ({
isReady: true,
presentation: { connection: { phase: testState.connectionPhase } },
}),
}));
vi.mock("~/state/use-atom-command", () => ({
useAtomCommand: () => testState.updateServer,
}));
Expand All @@ -24,18 +31,60 @@ vi.mock("./ui/toast", () => ({
}));

import { ServerUpdateAction, ServerUpdateProgress } from "./ServerUpdateAction";
import { Button } from "./ui/button";

type ActionElement = ReactElement<{
readonly disabled?: boolean;
readonly onClick?: () => void;
}>;

function renderAction(): ActionElement {
function renderActionResult(selfUpdate: "boot-service" | null = "boot-service") {
return ServerUpdateAction({
environmentId: "env-test" as EnvironmentId,
serverLabel: "Test server",
selfUpdate: "boot-service",
selfUpdate,
targetVersion: "0.0.31",
}) as ActionElement;
});
}

function renderAction(selfUpdate: "boot-service" | null = "boot-service"): ActionElement {
const result = renderActionResult(selfUpdate);

function findAction(node: ReactNode): ActionElement | null {
if (!isValidElement(node)) {
return null;
}
if (node.type === Button) {
return node as ActionElement;
}

const props = node.props as { readonly children?: ReactNode; readonly render?: ReactNode };
return (
findAction(props.render) ??
Children.toArray(props.children)
.map(findAction)
.find((action) => action !== null) ??
null
);
}

const action = findAction(result);
if (action === null) {
throw new Error("Server update action button not found");
}
return action;
}

function textContent(node: ReactNode): string {
if (typeof node === "string" || typeof node === "number") {
return String(node);
}
if (!isValidElement(node)) {
return "";
}

const props = node.props as { readonly children?: ReactNode };
return Children.toArray(props.children).map(textContent).join("");
}

async function flushPromises(): Promise<void> {
Expand All @@ -45,10 +94,45 @@ async function flushPromises(): Promise<void> {

describe("ServerUpdateAction", () => {
beforeEach(() => {
testState.connectionPhase = "connected";
testState.updateServer.mockReset();
testState.toast.mockReset();
});

it.each(["available", "offline", "connecting", "reconnecting", "error"])(
"disables self-update while the environment is %s",
async (connectionPhase) => {
testState.connectionPhase = connectionPhase;

const action = renderAction();
action.props.onClick?.();
await flushPromises();

expect(action.props.disabled).toBe(true);
expect(testState.updateServer).not.toHaveBeenCalled();
},
);

it("enables self-update when the environment connects", () => {
testState.connectionPhase = "connecting";
expect(renderAction().props.disabled).toBe(true);

testState.connectionPhase = "connected";
expect(renderAction().props.disabled).toBe(false);
});

it("explains why self-update is disabled", () => {
testState.connectionPhase = "connecting";

expect(textContent(renderActionResult())).toContain("Available once Test server is connected.");
});

it("keeps the manual update command available while disconnected", () => {
testState.connectionPhase = "connecting";

expect(renderAction(null).props.disabled).not.toBe(true);
});

it("reports success only after the shared update flow reconnects", async () => {
testState.updateServer.mockResolvedValue(
AsyncResult.success({ targetVersion: "0.0.31", method: "boot-service" as const }),
Expand Down
20 changes: 17 additions & 3 deletions apps/web/src/components/ServerUpdateAction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from "@t3tools/client-runtime/state/runtime";

import { useCopyToClipboard } from "~/hooks/useCopyToClipboard";
import { useEnvironmentPresentation } from "~/state/presentation";
import { serverEnvironment } from "~/state/server";
import { useAtomCommand } from "~/state/use-atom-command";
import { manualServerUpdateCommand } from "~/versionSkew";
Expand Down Expand Up @@ -87,6 +88,8 @@ export function ServerUpdateAction({
const updateServer = useAtomCommand(serverEnvironment.updateServer, {
reportFailure: false,
});
const { presentation } = useEnvironmentPresentation(environmentId);
const isConnected = presentation?.connection.phase === "connected";
const { copyToClipboard } = useCopyToClipboard<{ command: string }>({
target: "update command",
onCopy: ({ command }) => {
Expand All @@ -106,7 +109,7 @@ export function ServerUpdateAction({
});

const handleUpdate = async () => {
if (pendingUpdateEnvironmentIds.has(environmentId)) {
if (!isConnected || pendingUpdateEnvironmentIds.has(environmentId)) {
return;
}
pendingUpdateEnvironmentIds.add(environmentId);
Expand Down Expand Up @@ -153,9 +156,20 @@ export function ServerUpdateAction({
);
}

return (
<Button size="xs" onClick={() => void handleUpdate()}>
const action = (
<Button size="xs" disabled={!isConnected} onClick={() => void handleUpdate()}>
Comment thread
macroscopeapp[bot] marked this conversation as resolved.
Resolved
{label}
</Button>
);

if (isConnected) {
return action;
}

return (
<Tooltip>
<TooltipTrigger render={<span className="inline-flex">{action}</span>} />
<TooltipPopup side="top">Available once {serverLabel} is connected.</TooltipPopup>
</Tooltip>
);
}
3 changes: 3 additions & 0 deletions docs/user/updating.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ The update does not remove saved threads, settings, or project files.
The available action depends on how that server was started. T3 Code does not update connected
servers silently in the background.

If an environment is still connecting or reconnecting, **Update** and **Retry** stay disabled and
become available automatically once its status reaches **Connected**.

An older background-service launcher may ask you to run the exact
`npx t3@<version> service update` command on the server machine. That one local update installs the
rollback support needed for later remote updates, including versions that change the database.
Expand Down
Loading