Files
Triple-C/app/src/hooks/useProjectActions.test.ts
T
shadowdaoandClaude Opus 5.5 8441a096c8 fix(aws): stop guard review round 1
The stop guard's choice now reaches the workspace: after a successful
push (or for Stop without pushing) the app records pushed/discard in
/run/triple-c/stop-reason as root, and the entrypoint's shutdown hook
then skips its own push; a marker that can't be written is reported
and the stop goes on. The push runs before the auth bridge is stopped;
a guarded stop of a workspace that already stopped succeeds. The
Fargate table allows only 512/1024/2048 MiB with 256 CPU, pinned on
both sides by a shared fixture. Removal's missing-connection refusal
carries a stable prefix. OpenAI Compatible is allowed on AWS, with a
warning for local addresses and the app's own gateway. HostSection
locks resource actions unless Stopped, the add dialog drops the
environment on a region change, and the repository editor keeps rows
in progress across a refresh.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 10:26:53 -08:00

139 lines
5.3 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from "vitest";
import { act, renderHook } from "@testing-library/react";
import { useProjectActions } from "./useProjectActions";
import { useAppState } from "../store/appState";
import { awsProject, localProject } from "../test/awsFixtures";
import type { UnsavedWork } from "../lib/types";
const start = vi.fn();
const stop = vi.fn();
const checkUnsavedWork = vi.fn();
const awsTaskLog = vi.fn();
vi.mock("./useProjects", () => ({
useProjects: () => ({ start, stop, rebuild: vi.fn() }),
}));
vi.mock("./useTerminal", () => ({
useTerminal: () => ({ open: vi.fn(), sendInput: vi.fn() }),
}));
vi.mock("@tauri-apps/plugin-dialog", () => ({ save: vi.fn() }));
vi.mock("../lib/tauri-commands", () => ({
checkUnsavedWork: (id: string) => checkUnsavedWork(id),
awsTaskLog: (id: string, n: number) => awsTaskLog(id, n),
downloadContainerBackup: vi.fn(),
}));
const clean: UnsavedWork = { checked: true, repos: [], error: null };
const dirty: UnsavedWork = {
checked: true,
repos: [{ folder: "api", branch: "main", dirty: true, unpushed: 0 }],
error: null,
};
beforeEach(() => {
vi.clearAllMocks();
useAppState.setState({ toasts: [] } as never);
});
const toasts = () => (useAppState.getState() as unknown as { toasts: { message: string; detail?: string }[] }).toasts;
describe("useProjectActions stop guard", () => {
it("stops an AWS workspace with nothing unsaved straight away", async () => {
checkUnsavedWork.mockResolvedValue(clean);
const { result } = renderHook(() => useProjectActions(awsProject({ status: "running" })));
await act(async () => {
await result.current.handleStop();
});
expect(stop).toHaveBeenCalledWith("p1");
expect(result.current.stopGuard).toBeNull();
});
it("asks first when there is unsaved work, then stops with the choice", async () => {
checkUnsavedWork.mockResolvedValue(dirty);
const { result } = renderHook(() => useProjectActions(awsProject({ status: "running" })));
await act(async () => {
await result.current.handleStop();
});
expect(stop).not.toHaveBeenCalled();
expect(result.current.stopGuard).toEqual(dirty);
await act(async () => {
await result.current.resolveStopGuard("push");
});
expect(stop).toHaveBeenCalledWith("p1", "push");
expect(result.current.stopGuard).toBeNull();
});
it("asks (without a push) when the check itself fails, and Cancel stops nothing", async () => {
checkUnsavedWork.mockRejectedValue("Project p1 not found");
const { result } = renderHook(() => useProjectActions(awsProject({ status: "running" })));
await act(async () => {
await result.current.handleStop();
});
expect(result.current.stopGuard?.checked).toBe(false);
await act(async () => {
await result.current.resolveStopGuard(null);
});
expect(stop).not.toHaveBeenCalled();
});
it("toasts a failed push or StopTask and leaves the project as it was", async () => {
checkUnsavedWork.mockResolvedValue(dirty);
stop.mockRejectedValue("Pushing unsaved work failed (exit 1), so the workspace was not stopped:\npush denied");
const { result } = renderHook(() => useProjectActions(awsProject({ status: "running" })));
await act(async () => {
await result.current.handleStop();
});
await act(async () => {
await result.current.resolveStopGuard("push");
});
expect(toasts().at(-1)?.detail).toMatch(/push denied/);
});
it("never asks for a local project", async () => {
const { result } = renderHook(() => useProjectActions(localProject({ status: "running" })));
await act(async () => {
await result.current.handleStop();
});
expect(checkUnsavedWork).not.toHaveBeenCalled();
expect(stop).toHaveBeenCalledWith("p1");
});
});
describe("useProjectActions stop notes", () => {
it("tells the user when the choice could not be recorded in the workspace", async () => {
checkUnsavedWork.mockResolvedValue(dirty);
stop.mockResolvedValue("its own push of work in progress on shutdown may still run");
const { result } = renderHook(() => useProjectActions(awsProject({ status: "running" })));
await act(async () => {
await result.current.handleStop();
});
await act(async () => {
await result.current.resolveStopGuard("discard");
});
expect(stop).toHaveBeenCalledWith("p1", "discard");
expect(toasts().at(-1)?.detail).toMatch(/may still run/);
});
});
describe("useProjectActions start", () => {
it("adds the task's last log lines to an AWS start failure", async () => {
start.mockRejectedValue("The task stopped: Essential container exited");
awsTaskLog.mockResolvedValue(["entrypoint: clone failed", "bye"]);
const { result } = renderHook(() => useProjectActions(awsProject()));
await act(async () => {
await result.current.handleStart();
});
expect(awsTaskLog).toHaveBeenCalledWith("p1", 30);
expect(toasts().at(-1)?.detail).toMatch(/Last log lines:\nentrypoint: clone failed\nbye/);
});
it("does not read the log twice when the error already carries it", async () => {
start.mockRejectedValue("failed\nLast log lines:\nx");
const { result } = renderHook(() => useProjectActions(awsProject()));
await act(async () => {
await result.current.handleStart();
});
expect(awsTaskLog).not.toHaveBeenCalled();
});
});