Files
PaperClipAI/ui/src/pages/CompanyEnvironments.test.tsx
T
DottaandPaperclip 57e977be72 feat: integrate Pi 1.0 into the experimental Runner (#14921)
## Thinking Path

> - Paperclip manages AI agents and their work.
> - The experimental Runner owns provider processes and durable
sessions.
> - Pi needs working task execution and human controls.
> - The five-PR stack must preserve changes already on master.
> - Each layer now carries the complete integrated source for a safe
sequential fallback.
> - This PR belongs to native GitHub stack #15602, ending at #14956.

## Linked Issues or Issue Description

Refs #14436, #14631, #14743 and #14956.

Ship Pi 1.0 through the experimental Paperclip Runner. The five PRs are
#14921, #14922, #14923, #14924 and #14956. The user authorized the
complete merge after checks pass. Existing `pi_local` execution is
unchanged. Accounting and wider provider/platform qualification remain
deferred.

## What Changed

- Recover missing final replies after workspace finalization changes
owners, using accepted-turn evidence without rerunning work or granting
external-chat publication.
- Preserve the admitted Pi instruction root across warm runs, while
retaining changed-root rejection.
- Give Pi a bounded 15-second default shutdown grace so stop, drain
acknowledgement and durable suspension can complete. Explicit deadlines
and other providers retain their existing behavior.
- Integrate the Pi 1.0 runtime and master contracts.
- Use Pi profile 22. Preserve explicit caller-selected models and exact
native thinking levels. Keep Pi's wrapper, helper, extension and
question/control behavior unchanged from the qualified profile-19
runtime.
- Preserve master's Dot lifecycle and consent fields, configured task
environment, status guards and current Codex/Claude dependency versions.
Cursor stays qualified. Copilot stays pending; profile 17 binds the
changed shared protocol validation sources.
- Exclude general AWS IAM credentials from Pi static/custom provider
bindings and selected task projections; preserve the provider-scoped
Bedrock bearer key. Profile 21 is retained as historical provenance.
Rust and cloud install probes use the current declaration.
- Patch bundled brace-expansion 5.0.9 to the exact official 5.0.12
payload. Pin the patch and complete runtime closures. Include the patch
in normal installed setup tooling. Keep the upstream Pi shrinkwrap as
provenance and permit only this exact security correction.
- Include current attestation files in the Docker build context. Keep
the repository lockfile unchanged from master. CI and private image
builds resolve manifest changes before their frozen installation.

## Verification

- Full local `pnpm -r typecheck` passes, including Runner Rust, server
and UI. Focused integration checks pass: 194 Runner
admission/environment tests, 63 profile/credential tests with one
expected skip, 152 Dot/UI configuration tests, and Pi transcript/notice
tests.
- Full local `pnpm build` passes on the final source.
- Fresh final-source checks pass: all 698 Rust workspace tests (32
binaries), 156 credential/profile/controller tests with one expected
skip, Runner TypeScript typecheck, and 20 package/setup/sandbox tests.
- The profile-21 Pi materializer passes on the native host with the
official pinned Node 24.21.0 and its npm. It verifies all 150 locked
packages, the patched dependency and the exact closure. Setup/package
bundle tests and UI token gates pass.
- The old hashes were reproduced for all three supported targets before
calculating the patched graph. New closure hashes are darwin-arm64
`282022db10150c6632b3444df421342e7d534bdf5d5fb1097a2e79d0625a2bcf`,
darwin-x64
`64e251e19009f755c0b04f73ce2138246faab71a961b0f13d75ebfcc34bef12e`, and
linux-x64
`713b1fdff42fb56a1518bdc084f181d70bee8ebadc3e4b1d76321ed9108c8410`.
Independent native platform execution is separate from graph identity
reproduction.
- Historical cloud qualification remains unchanged: all seven core cases
pass on shipping source `10dc43c9ec65d88c2f782d62afb296d09494f215`,
harness `1a4408a48cfb5a1f094a311141c257c92cd7a893`, image
`sha256:5b3a775b383591bda1b0c1889e509acc70ce7f37c53f09733c81d59037f02280`,
and accepted Sonnet 4.6/low fixture. All 215 canonical files and all
seven cleanup checks pass independent verification. These are profile-19
results and are not relabeled as fresh profile-22 runs.
- Current Pi digest:
`sha256:e92078bee3c23bec4100aa589013a44613d054cd686826534025d8019e9f39a9`.
[The readiness
plan](https://github.com/paperclipai/paperclip/blob/codex/pi-production-readiness/doc/plans/2026-10-02-pi-production-readiness.md)
preserves campaign and failed-attempt provenance.
- Merge only after every PR's current-head CI and fresh review pass.
Linux CI covers the full suites, build and browser tests. The local
embedded Postgres API-authority suite cannot start on this macOS/Node 26
host, so Linux CI must confirm that suite.

### Fresh profile-22 core qualification — 2026-10-08

All seven accepted core cases pass canonically on Pi profile 22, with
`openrouter/anthropic/claude-sonnet-4.6` and native-confirmed low
thinking. This model is a fixture; production accepts the caller's
explicit Pi provider/model.

Runtime/install source: `3241a992f2a7703e59e97ed0fd3e5d6405de4401`.
Frozen accepted harness: `1a4408a48cfb5a1f094a311141c257c92cd7a893`.
Immutable cloud image:
`ghcr.io/paperclipai/paperclip-daytona-runner@sha256:506f22db7edd78f37c0c40bec1cc084af1850455026dbf467194bfbb8fcef141`.
Pi digest:
`sha256:e92078bee3c23bec4100aa589013a44613d054cd686826534025d8019e9f39a9`.

[Hosted Linux image and clean-install
verification](https://github.com/paperclipai/paperclip/actions/runs/37868328023)
passes, including all 20 source-bound archives, normal CLI/Pi setup,
companion import and the production pack reader. This exact installation
source includes the latest master integration and the corrected Pi warm
instruction-root fence. Full local typecheck/build and current-head
hosted CI verify the final stack. All 13 focused real-root regressions
pass. The full local executor suite passed 662 tests; 15 database tests
could not start the Mac embedded PostgreSQL service. Hosted Linux CI
passes the full required verification and E2E checks. These fresh
results keep their own source identity; profile-19 results remain
historical.

| Core path | Canonical campaign | Retained archive SHA-256 |
| --- | --- | --- |
| File edit, validation, download and Done |
`pi-core22-replyfix-0-1791511228` | 23 files;
`a473e8603a3dd4737863291f8d3d1e392391f0b16d433c3e0e0e9d8baf7a97b0` |
| Pending question and controller restart |
`pi-core22-replyfix-1-1791511376` | 33 files;
`6b829c4eb74e1f32a89c692a4ae7130dbfc1c6d3cf13915effe2103d9e242c8e` |
| Three-turn session/process/workspace continuity |
`pi-core22-replyfix-2-1791511587` | 23 files;
`7a87021f8f9a3fdd3c58bb4467f8d82c635e3ea4795d6e75f144d9aa14818df8` |
| Four typed questions and browser reconnects |
`pi-core22-replyfix-3-1791511881` | 42 files;
`9e31755252be1f4f9cb0626c984c142d4d1ae5f5bee3a7af08444db8d12c280a` |
| Plan approval and completion | `pi-core22-replyfix-4-1791512031` | 22
files;
`a0383ce1aab38e7b5a25ce0e9dd3bebea5c037ebd96ae6b29dae19015da2ae2c` |
| Same-turn steering and permission denial |
`pi-core22-replyfix-5-1791512261` | 39 files;
`c929b8c7070f0b66aedc17e65ca46e6beab1e363926ac9f7e2a75fb250f05949` |
| Stop during pending permission | `pi-core22-replyfix-6-1791512390` |
33 files;
`7f0a58ae0f4d5bfc76149435f4e322537089c5bd16e7ffe9b5ad71f10a621a07` |

All 215 canonical files (28714587 bytes) are independently
hash-verified. All seven cleanup grades pass, with no owned runtime
process or temporary root after each case. Automatic retries are zero.
The owned cloud host stopped normally after retention. The prior
profile-22 warm attempt remains failed and separately retained: archive
SHA-256
`1e54eba5ec72b50cee1534b23d1d1d4f21a090006b8a64501ba70db972abfde5`. Its
original canonical classification is preserved. Diagnosis reproduced a
product bug comparing an agent-files root against an unset
checkpoint-only field. The fix stores the admitted physical root
separately from the adopted per-run collection capability. The real-root
regression fails before the fix and passes afterward, including
rejection of a changed physical root. Fixture, grader, model and all
seven accepted case IDs are unchanged; this fresh campaign tests
final-reply publication after file registration first. The intermediate
restart attempt also remains failed and retained: archive SHA-256
`5dcaefdf1d17cf4cd54fd4cf810f45e736667392339b8ce7caf08bb4e225277f`. Its
original canonical classification is preserved. Pi resumed, wrote the
verified answer and completed its task; exact runner suspension was
proven, but idle stop consumed about 5.2s and left under 3s for the
drain acknowledgement. The Pi-only default shutdown grace is now 15s,
preserving a full 5s drain round trip and a finite suspension reserve.
Explicit caller deadlines, other provider defaults, literal drain
receipts and exact suspension identity checks remain unchanged. The
timing regression fails before this correction and passes afterward; all
18 focused settlement tests and Runner typecheck pass. The final-source
file attempt is also preserved as failed (`candidate_failure`), archive
SHA-256
`db6767b6773ea618997927ac77bdb005a5ac81492c7b9c0ffbc900449f829bc9`.
Native edit, validation, exact downloadable artifact and Done/succeeded
all passed, and the exact final reply was durably recorded. A workspace
recovery owner completed before the live heartbeat reached presentation,
leaving that reply absent from task chat. Recovery now materializes only
a completed final reply from the accepted turn of an ordinary internal
Done task, preserving issue/run/contract binding, suppression,
external-chat authorization and same-run deduplication. The database
regression covers the generated file-preparation receipt, suppression,
unapproved external continuation and replay. Server typecheck and all 49
response-selection tests pass; hosted Linux verifies the database
regression because embedded PostgreSQL cannot start on this Mac.

The delayed-final-answer database regression passes on [the final
root-source Linux server
shard](https://github.com/paperclipai/paperclip/actions/runs/37868262553/job/113628594152),
alongside 1,108 passing tests. The first root Runner shard had one
unchanged durable-resume test exceed its 5-second timeout; the identical
top-source shard and the isolated exact test passed. One rerun of that
failed job and its required aggregate passed without source or test
changes. The original failed job log and the single-rerun receipt remain
retained.

### October 9 merge verification

Current merge head: `5a8fe63512a7166aaef5cf50065a25008aa8b44b`. All
current-head checks pass, including `ci / verify` and `ci / e2e`;
exact-head Greptile review is 5/5 with no unresolved threads. Current
master conflicts are resolved. The user authorized the maintainer
override of the code-owner review gate after these checks. The seven
retained live core cases remain bound to source
`3241a992f2a7703e59e97ed0fd3e5d6405de4401` and its recorded cloud image.

## Risks

- The security correction changes the dependency closure and profile
identity. Old sessions must reopen on the new profile. Exact identities
and credential bindings fail closed.
- The runner remains experimental and requires explicit selection.
Legacy Pi Local is unchanged. Caller model IDs pass through; the E2E
model is a fixture.
- Accounting and the broad platform/provider matrix remain deferred.
This merge does not publish a release or deploy a service.

## Model Used

OpenAI GPT-6 through Codex assisted with reasoning, repository
inspection, editing and tool use. The exact serving ID and context
window are not exposed in this session. Final live qualification uses Pi
1.0.0 with `openrouter/anthropic/claude-sonnet-4.6` and native-confirmed
low thinking.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` OR (b) described the issue in-PR following the relevant issue
template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [x] All Paperclip CI gates are green
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-10-09 08:56:41 -05:00

1935 lines
73 KiB
TypeScript

// @vitest-environment jsdom
import { createRoot } from "react-dom/client";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { MemoryRouter, Route, Routes } from "react-router-dom";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { TooltipProvider } from "@/components/ui/tooltip";
import { CompanyEnvironments } from "./CompanyEnvironments";
import { ApiError } from "@/api/client";
const xtermMocks = vi.hoisted(() => {
class MockTerminal {
readonly options: Record<string, unknown>;
cols: number;
rows: number;
writes: string[] = [];
focused = false;
disposed = false;
openedElement: HTMLElement | null = null;
private readonly dataHandlers: Array<(data: string) => void> = [];
constructor(options: Record<string, unknown> = {}) {
this.options = options;
this.cols = typeof options.cols === "number" ? options.cols : 80;
this.rows = typeof options.rows === "number" ? options.rows : 24;
xtermMocks.terminalInstances.push(this);
}
loadAddon(addon: { activate?: (terminal: MockTerminal) => void }) {
addon.activate?.(this);
}
open(element: HTMLElement) {
this.openedElement = element;
element.dataset.mockXtermOpen = "true";
}
onData(handler: (data: string) => void) {
this.dataHandlers.push(handler);
return {
dispose: () => {
const index = this.dataHandlers.indexOf(handler);
if (index >= 0) this.dataHandlers.splice(index, 1);
},
};
}
emitData(data: string) {
for (const handler of this.dataHandlers) handler(data);
}
write(data: string) {
this.writes.push(data);
if (!this.openedElement) return;
const chunk = document.createElement("span");
// Keep test DOM readable without reimplementing xterm's ANSI parser.
chunk.textContent = data.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, "");
this.openedElement.appendChild(chunk);
}
resize(cols: number, rows: number) {
this.cols = cols;
this.rows = rows;
}
focus() {
this.focused = true;
this.openedElement?.focus();
}
reset() {
this.writes = [];
if (this.openedElement) this.openedElement.textContent = "";
}
clear() {
if (this.openedElement) this.openedElement.textContent = "";
}
dispose() {
this.disposed = true;
}
}
class MockFitAddon {
fitCalls = 0;
terminal: MockTerminal | null = null;
constructor() {
xtermMocks.fitAddonInstances.push(this);
}
activate(terminal: MockTerminal) {
this.terminal = terminal;
}
fit() {
this.fitCalls += 1;
this.terminal?.resize(120, 32);
}
}
return {
MockTerminal,
MockFitAddon,
terminalInstances: [] as MockTerminal[],
fitAddonInstances: [] as MockFitAddon[],
reset() {
this.terminalInstances.length = 0;
this.fitAddonInstances.length = 0;
},
};
});
vi.mock("@xterm/xterm", () => ({
Terminal: xtermMocks.MockTerminal,
}));
vi.mock("@xterm/addon-fit", () => ({
FitAddon: xtermMocks.MockFitAddon,
}));
vi.mock("@xterm/xterm/css/xterm.css", () => ({}));
const mockEnvironmentsApi = vi.hoisted(() => ({
list: vi.fn(),
capabilities: vi.fn(),
secretRefs: vi.fn(),
probe: vi.fn(),
probeConfig: vi.fn(),
create: vi.fn(),
update: vi.fn(),
remove: vi.fn(),
deleteBlastRadius: vi.fn(),
setDefault: vi.fn(),
customImageTemplate: vi.fn(),
startCustomImageSetupSession: vi.fn(),
customImageSetupSession: vi.fn(),
createCustomImageTerminalSessionToken: vi.fn(),
finishCustomImageSetupSession: vi.fn(),
cancelCustomImageSetupSession: vi.fn(),
rollbackCustomImageTemplate: vi.fn(),
relinkCustomImageTemplate: vi.fn(),
disableCustomImageTemplate: vi.fn(),
}));
const mockInstanceSettingsApi = vi.hoisted(() => ({
get: vi.fn(),
getExperimental: vi.fn(),
}));
const mockSecretsApi = vi.hoisted(() => ({
list: vi.fn(),
}));
const mockAgentsApi = vi.hoisted(() => ({
list: vi.fn(),
update: vi.fn(),
}));
vi.mock("@/context/CompanyContext", () => ({
useCompany: () => ({
selectedCompanyId: "company-1",
selectedCompany: null,
}),
}));
vi.mock("@/context/BreadcrumbContext", () => ({
useBreadcrumbs: () => ({ setBreadcrumbs: vi.fn() }),
}));
vi.mock("@/context/ToastContext", () => ({
useToast: () => ({ pushToast: vi.fn() }),
useToastActions: () => ({ pushToast: vi.fn() }),
useOptionalToastActions: () => ({ pushToast: vi.fn() }),
}));
vi.mock("@/api/environments", () => ({
environmentsApi: mockEnvironmentsApi,
}));
vi.mock("@/api/instanceSettings", () => ({
instanceSettingsApi: mockInstanceSettingsApi,
}));
vi.mock("@/api/secrets", () => ({
secretsApi: mockSecretsApi,
}));
vi.mock("@/api/agents", () => ({
agentsApi: mockAgentsApi,
}));
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(globalThis as any).IS_REACT_ACT_ENVIRONMENT = true;
// Minimal browser APIs for jsdom.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(globalThis as any).ResizeObserver = class {
observe() {}
unobserve() {}
disconnect() {}
};
class FakeWebSocket {
static CONNECTING = 0;
static OPEN = 1;
static CLOSING = 2;
static CLOSED = 3;
static instances: FakeWebSocket[] = [];
readonly url: string;
readyState = FakeWebSocket.CONNECTING;
sent: string[] = [];
onopen: ((event: Event) => void) | null = null;
onmessage: ((event: MessageEvent) => void) | null = null;
onclose: ((event: CloseEvent) => void) | null = null;
onerror: ((event: Event) => void) | null = null;
constructor(url: string) {
this.url = url;
FakeWebSocket.instances.push(this);
}
send(data: string) {
this.sent.push(data);
}
close() {
this.readyState = FakeWebSocket.CLOSED;
this.onclose?.(new Event("close") as CloseEvent);
}
open() {
this.readyState = FakeWebSocket.OPEN;
this.onopen?.(new Event("open"));
}
emitMessage(data: string) {
this.onmessage?.(new MessageEvent("message", { data }));
}
}
async function act(callback: () => void | Promise<void>) {
await callback();
await Promise.resolve();
await new Promise((resolve) => window.setTimeout(resolve, 0));
}
async function flushReact() {
for (let i = 0; i < 3; i += 1) {
await Promise.resolve();
await new Promise((resolve) => window.setTimeout(resolve, 0));
}
}
async function waitForAssertion(assertion: () => void) {
let lastError: unknown;
for (let i = 0; i < 20; i += 1) {
await flushReact();
try {
assertion();
return;
} catch (error) {
lastError = error;
}
}
throw lastError;
}
function testProviderButtons(container: HTMLElement): HTMLButtonElement[] {
return Array.from(container.querySelectorAll("button")).filter((button) => {
const label = button.textContent?.trim();
return label === "Test provider" || label === "Testing...";
});
}
function findButton(root: ParentNode, label: string): HTMLButtonElement | undefined {
return Array.from(root.querySelectorAll("button")).find((button) => button.textContent?.trim() === label);
}
function findAction(root: ParentNode, label: string): HTMLElement | undefined {
return Array.from(root.querySelectorAll<HTMLElement>("button,a")).find((element) => element.textContent?.trim() === label);
}
function editButtons(root: ParentNode): HTMLElement[] {
return Array.from(root.querySelectorAll<HTMLElement>("button,a")).filter((element) => element.textContent?.trim() === "Edit");
}
function click(element: Element | null | undefined) {
element?.dispatchEvent(new MouseEvent("click", { bubbles: true, cancelable: true }));
}
function setInputValue(input: HTMLInputElement, value: string) {
const setter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")?.set;
setter?.call(input, value);
input.dispatchEvent(new Event("input", { bubbles: true }));
}
function setSelectValue(select: HTMLSelectElement, value: string) {
const setter = Object.getOwnPropertyDescriptor(HTMLSelectElement.prototype, "value")?.set;
setter?.call(select, value);
select.dispatchEvent(new Event("change", { bubbles: true }));
}
const ENVIRONMENTS_PATH = "/company/settings/instance/environments";
function getEnvironmentFormPage(): HTMLElement | null {
return document.body.querySelector("[data-testid='environment-form-page']");
}
// Opens the edit page for the environment at `index`. The environment list
// depends on an async query. Under a loaded test worker that query can resolve
// after the first flush, so a direct click can miss the Edit control and never
// open the form page. This helper first waits for the control, then clicks it,
// then waits for the form page.
async function openEnvironmentEditPage(container: HTMLElement, index = 0) {
await waitForAssertion(() => {
expect(editButtons(container)[index]).toBeTruthy();
});
await act(async () => click(editButtons(container)[index]));
await waitForAssertion(() => {
expect(getEnvironmentFormPage()).not.toBeNull();
});
}
function renderCompanyEnvironments(queryClient: QueryClient, initialPath = ENVIRONMENTS_PATH) {
return (
<QueryClientProvider client={queryClient}>
<MemoryRouter initialEntries={[initialPath]}>
<TooltipProvider>
<Routes>
<Route path={ENVIRONMENTS_PATH} element={<CompanyEnvironments />} />
<Route path={`${ENVIRONMENTS_PATH}/new`} element={<CompanyEnvironments mode="create" />} />
<Route path={`${ENVIRONMENTS_PATH}/:environmentId/edit`} element={<CompanyEnvironments mode="edit" />} />
</Routes>
</TooltipProvider>
</MemoryRouter>
</QueryClientProvider>
);
}
function createSession(overrides: Record<string, unknown> = {}) {
return {
id: "session-1",
environmentId: "env-1",
templateId: null,
promotedTemplateId: null,
provider: "daytona",
providerLeaseId: "lease-redacted",
environmentLeaseId: null,
status: "waiting_for_user",
startedByUserId: "user-1",
startedByAgentId: null,
baseTemplateRef: null,
expiresAt: "2026-06-25T21:00:00.000Z",
finishedAt: null,
failureReason: null,
connectionSummary: {
type: "ssh",
username: "sandbox",
hostRedacted: true,
portRedacted: true,
},
connectionSecretRef: "secret-redacted",
metadata: null,
createdAt: "2026-06-25T20:00:00.000Z",
updatedAt: "2026-06-25T20:00:00.000Z",
...overrides,
};
}
function createTemplate(overrides: Record<string, unknown> = {}) {
return {
id: "template-1",
environmentId: "env-1",
provider: "daytona",
templateKind: "snapshot",
templateRef: "redacted-template-ref",
sourceTemplateRef: null,
sourceEnvironmentConfigFingerprint: "fingerprint",
status: "active",
createdByUserId: "user-1",
createdByAgentId: null,
capturedAt: "2026-06-25T20:00:00.000Z",
lastUsedAt: null,
supersededByTemplateId: null,
metadata: null,
createdAt: "2026-06-25T20:00:00.000Z",
updatedAt: "2026-06-25T20:00:00.000Z",
...overrides,
};
}
function createDeleteBlastRadius(overrides: {
canDelete?: boolean;
deleteBlockedReasons?: string[];
staticReferences?: Record<string, unknown>;
activeRuntimeUse?: Record<string, unknown>;
pendingCleanupLeaseCount?: number;
reusableSandboxLeaseCount?: number;
reusableSandboxLeaseHolders?: Array<Record<string, unknown>>;
} = {}) {
return {
environmentId: "env-1",
canDelete: overrides.canDelete ?? true,
deleteBlockedReasons: overrides.deleteBlockedReasons ?? [],
reusableSandboxLeaseHolders: overrides.reusableSandboxLeaseHolders ?? [],
staticReferences: {
isManagedLocal: false,
isInstanceDefault: false,
agentDefaultCount: 0,
executionWorkspaceSelectionCount: 0,
issueSelectionCount: 0,
projectSelectionCount: 0,
secretBindingCount: 0,
...overrides.staticReferences,
},
activeRuntimeUse: {
activeLeaseCount: 0,
activeCustomImageSetupSessionCount: 0,
hasActiveRuntimeUse: false,
...overrides.activeRuntimeUse,
},
pendingCleanupLeaseCount: overrides.pendingCleanupLeaseCount ?? 0,
reusableSandboxLeaseCount: overrides.reusableSandboxLeaseCount ?? 0,
};
}
function supportedDaytonaCapabilities() {
return {
adapters: [],
drivers: { local: "supported", ssh: "supported", sandbox: "supported", plugin: "unsupported" },
sandboxProviders: {
daytona: {
status: "supported",
supportsSavedProbe: true,
supportsUnsavedProbe: true,
supportsRunExecution: true,
supportsReusableLeases: true,
supportsInteractiveSetup: true,
interactiveSetupConnectionTypes: ["ssh"],
supportsTemplateCapture: true,
supportsTemplateDelete: true,
displayName: "Daytona",
},
},
};
}
describe("CompanyEnvironments — test provider button", () => {
let container: HTMLDivElement;
let root: ReturnType<typeof createRoot> | null;
let probeResolvers: Map<string, () => void>;
let originalWebSocket: typeof WebSocket | undefined;
beforeEach(() => {
container = document.createElement("div");
document.body.appendChild(container);
root = null;
probeResolvers = new Map();
originalWebSocket = globalThis.WebSocket;
FakeWebSocket.instances = [];
xtermMocks.reset();
globalThis.WebSocket = FakeWebSocket as unknown as typeof WebSocket;
mockInstanceSettingsApi.get.mockResolvedValue({ defaultEnvironmentId: null });
mockInstanceSettingsApi.getExperimental.mockResolvedValue({ enableEnvironments: true });
mockEnvironmentsApi.capabilities.mockResolvedValue({ adapters: [], sandboxProviders: {} });
mockEnvironmentsApi.secretRefs.mockResolvedValue({ refs: [] });
mockSecretsApi.list.mockResolvedValue([]);
mockEnvironmentsApi.customImageTemplate.mockResolvedValue({
activeTemplate: null,
activeSession: null,
latestSession: null,
});
mockEnvironmentsApi.startCustomImageSetupSession.mockResolvedValue({
session: createSession(),
connectionPayload: { type: "ssh", command: "ssh sandbox@setup.example.invalid" },
});
mockEnvironmentsApi.customImageSetupSession.mockResolvedValue({
session: createSession(),
connectionPayload: { type: "ssh", command: "ssh sandbox@setup.example.invalid" },
});
mockEnvironmentsApi.createCustomImageTerminalSessionToken.mockResolvedValue({
id: "terminal-session-1",
token: "terminal-token-terminal-token-123456",
expiresAt: "2026-06-25T20:05:00.000Z",
setupSessionId: "session-1",
environmentId: "env-1",
connectionType: "ssh",
websocketPath:
"/api/environment-custom-image-setup-sessions/session-1/terminal/ws?terminalSessionId=terminal-session-1",
});
mockEnvironmentsApi.finishCustomImageSetupSession.mockResolvedValue({
session: createSession({ status: "promoted", promotedTemplateId: "template-1", finishedAt: "2026-06-25T20:10:00.000Z" }),
template: createTemplate(),
connectionPayload: null,
});
mockEnvironmentsApi.cancelCustomImageSetupSession.mockResolvedValue(
createSession({ status: "cancelled", finishedAt: "2026-06-25T20:10:00.000Z" }),
);
mockEnvironmentsApi.rollbackCustomImageTemplate.mockResolvedValue({
activeTemplate: createTemplate({ id: "template-previous" }),
supersededTemplate: createTemplate({ id: "template-current", status: "superseded" }),
});
mockEnvironmentsApi.relinkCustomImageTemplate.mockResolvedValue({
template: createTemplate({ id: "template-relinked" }),
classification: "knob_only",
});
mockEnvironmentsApi.disableCustomImageTemplate.mockResolvedValue(
createTemplate({ status: "revoked" }),
);
mockEnvironmentsApi.list.mockResolvedValue([
{ id: "env-1", name: "Alpha", driver: "sandbox", description: null, config: { provider: "e2b" } },
{ id: "env-2", name: "Beta", driver: "sandbox", description: null, config: { provider: "e2b" } },
]);
mockEnvironmentsApi.create.mockImplementation(async (_companyId: string, body: { name: string }) => ({
id: "env-new",
name: body.name,
driver: "ssh",
description: null,
config: {},
}));
mockEnvironmentsApi.update.mockImplementation(async (environmentId: string, body: { name: string }) => ({
id: environmentId,
name: body.name,
driver: "sandbox",
description: null,
config: { provider: "e2b" },
}));
mockEnvironmentsApi.deleteBlastRadius.mockResolvedValue(createDeleteBlastRadius());
mockEnvironmentsApi.remove.mockImplementation(async (environmentId: string) => ({
id: environmentId,
name: environmentId === "env-1" ? "Alpha" : "Beta",
driver: "sandbox",
description: null,
config: { provider: "e2b" },
}));
mockAgentsApi.list.mockResolvedValue([]);
mockAgentsApi.update.mockResolvedValue({});
// Each probe stays pending until its resolver is called, so the testing
// state remains observable and can be settled per environment.
mockEnvironmentsApi.probe.mockImplementation(
(environmentId: string) =>
new Promise<{ ok: boolean; driver: string; summary: string; details: null }>((resolve) => {
probeResolvers.set(environmentId, () =>
resolve({ ok: true, driver: "sandbox", summary: "ok", details: null }),
);
}),
);
});
afterEach(() => {
root?.unmount();
root = null;
container.remove();
document.body.innerHTML = "";
if (originalWebSocket) {
globalThis.WebSocket = originalWebSocket;
} else {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
delete (globalThis as any).WebSocket;
}
vi.restoreAllMocks();
vi.clearAllMocks();
});
it("shows the testing state only on the clicked environment's button", async () => {
root = createRoot(container);
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
await act(async () => {
root!.render(renderCompanyEnvironments(queryClient));
});
await flushReact();
const buttonsBefore = testProviderButtons(container);
expect(buttonsBefore).toHaveLength(2);
expect(buttonsBefore.every((button) => button.textContent?.trim() === "Test provider")).toBe(true);
expect(buttonsBefore.every((button) => !button.disabled)).toBe(true);
await act(async () => {
buttonsBefore[0].dispatchEvent(new MouseEvent("click", { bubbles: true }));
});
await flushReact();
const buttonsAfter = testProviderButtons(container);
expect(buttonsAfter).toHaveLength(2);
expect(buttonsAfter[0].textContent?.trim()).toBe("Testing...");
expect(buttonsAfter[0].disabled).toBe(true);
expect(buttonsAfter[1].textContent?.trim()).toBe("Test provider");
expect(buttonsAfter[1].disabled).toBe(false);
expect(mockEnvironmentsApi.probe).toHaveBeenCalledExactlyOnceWith("env-1", "company-1");
});
it("explains that successful sandbox provider tests use a temporary sandbox", async () => {
root = createRoot(container);
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
mockEnvironmentsApi.list.mockResolvedValue([
{ id: "env-1", name: "Daytona", driver: "sandbox", description: null, config: { provider: "daytona" } },
]);
mockEnvironmentsApi.capabilities.mockResolvedValue(supportedDaytonaCapabilities());
mockEnvironmentsApi.probe.mockResolvedValue({
ok: true,
driver: "sandbox",
summary: "Connected to Daytona sandbox paperclip-probe.",
details: {
provider: "daytona",
diagnostics: [],
metadata: {
provider: "daytona",
sandboxId: "473167E9",
sandboxName: "paperclip-probe",
},
},
});
await act(async () => {
root!.render(renderCompanyEnvironments(queryClient));
});
await flushReact();
await act(async () => {
testProviderButtons(container)[0].dispatchEvent(new MouseEvent("click", { bubbles: true }));
});
await flushReact();
expect(mockEnvironmentsApi.probe).toHaveBeenCalledExactlyOnceWith("env-1", "company-1");
expect(container.textContent).toContain("Connected to Daytona sandbox paperclip-probe.");
expect(container.textContent).not.toContain("Verified temporary daytona sandbox");
expect(container.textContent).not.toContain("Test probes clean up the validation sandbox after the check");
expect(container.textContent).not.toContain("provider dashboard");
});
it("does not show sandbox lifecycle success copy for failed sandbox provider tests", async () => {
root = createRoot(container);
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
mockEnvironmentsApi.list.mockResolvedValue([
{ id: "env-1", name: "Daytona", driver: "sandbox", description: null, config: { provider: "daytona" } },
]);
mockEnvironmentsApi.capabilities.mockResolvedValue(supportedDaytonaCapabilities());
mockEnvironmentsApi.probe.mockResolvedValue({
ok: false,
driver: "sandbox",
summary: "Daytona sandbox probe failed.",
details: {
provider: "daytona",
error: "Sandbox image was not found.",
metadata: {
provider: "daytona",
sandboxId: "473167E9",
sandboxName: "paperclip-probe",
},
},
});
await act(async () => {
root!.render(renderCompanyEnvironments(queryClient));
});
await flushReact();
await act(async () => {
testProviderButtons(container)[0].dispatchEvent(new MouseEvent("click", { bubbles: true }));
});
await flushReact();
expect(container.textContent).toContain("Daytona sandbox probe failed.");
expect(container.textContent).toContain("Sandbox image was not found.");
expect(container.textContent).not.toContain("Verified temporary daytona sandbox");
expect(container.textContent).not.toContain("Test probes clean up the validation sandbox after the check");
});
it("keeps the second environment's testing state when an earlier probe settles", async () => {
root = createRoot(container);
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
await act(async () => {
root!.render(renderCompanyEnvironments(queryClient));
});
await flushReact();
// Click both rows in quick succession while both probes are still pending.
await act(async () => {
testProviderButtons(container)[0].dispatchEvent(new MouseEvent("click", { bubbles: true }));
});
await flushReact();
await act(async () => {
testProviderButtons(container)[1].dispatchEvent(new MouseEvent("click", { bubbles: true }));
});
await flushReact();
// Settle only the first environment's probe.
await act(async () => {
probeResolvers.get("env-1")?.();
});
await flushReact();
const buttons = testProviderButtons(container);
expect(buttons[1].textContent?.trim()).toBe("Testing...");
expect(buttons[1].disabled).toBe(true);
});
it("opens the add-environment form on a standalone page and closes it on cancel", async () => {
root = createRoot(container);
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
await act(async () => {
root!.render(renderCompanyEnvironments(queryClient));
});
await flushReact();
await act(async () => {
click(container.querySelector('[aria-label="Add environment"]'));
});
await waitForAssertion(() => {
expect(getEnvironmentFormPage()?.textContent).toContain("Add environment");
});
await act(async () => {
findButton(document.body, "Cancel")?.dispatchEvent(new MouseEvent("click", { bubbles: true }));
});
await flushReact();
expect(getEnvironmentFormPage()).toBeNull();
});
it("opens the edit form on a standalone page with existing values and closes after save", async () => {
root = createRoot(container);
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
await act(async () => {
root!.render(renderCompanyEnvironments(queryClient));
});
await flushReact();
await act(async () => {
click(findAction(container, "Edit"));
});
await waitForAssertion(() => {
expect(getEnvironmentFormPage()?.textContent).toContain("Edit environment");
});
const page = getEnvironmentFormPage();
expect(document.body.querySelector("[role='dialog']")).toBeNull();
expect(
Array.from(page?.querySelectorAll("input") ?? []).some((input) => (input as HTMLInputElement).value === "Alpha"),
).toBe(true);
await act(async () => click(findButton(page!, "Add variable")));
await flushReact();
const variableName = page!.querySelector<HTMLInputElement>('input[aria-label="Variable name"]')!;
const variableValue = page!.querySelector<HTMLInputElement>('input[aria-label="Variable value"]')!;
await act(async () => {
setInputValue(variableName, "API_TOKEN");
setInputValue(variableValue, "draft-token");
});
await flushReact();
await act(async () => {
findButton(document.body, "Save environment")?.dispatchEvent(new MouseEvent("click", { bubbles: true }));
});
await flushReact();
expect(mockEnvironmentsApi.update).toHaveBeenCalledExactlyOnceWith(
"env-1",
expect.objectContaining({
name: "Alpha",
driver: "sandbox",
envVars: { API_TOKEN: { type: "plain", value: "draft-token" } },
}),
// The secret-context company must ride along so the server can scope
// bindings even when the environment has none yet.
"company-1",
);
// Saving awaits query invalidation before navigating away. A single flush
// can observe the API call before that asynchronous success path settles.
await waitForAssertion(() => {
expect(getEnvironmentFormPage()).toBeNull();
});
});
it("confirms before cancelling the edit page with unsaved environment variable drafts", async () => {
root = createRoot(container);
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
const confirmSpy = vi.spyOn(window, "confirm").mockReturnValue(false);
await act(async () => {
root!.render(renderCompanyEnvironments(queryClient));
});
await flushReact();
await act(async () => {
click(findAction(container, "Edit"));
});
await waitForAssertion(() => {
expect(getEnvironmentFormPage()?.textContent).toContain("Edit environment");
});
const page = getEnvironmentFormPage()!;
await act(async () => click(findButton(page, "Add variable")));
await flushReact();
const variableName = page.querySelector<HTMLInputElement>('input[aria-label="Variable name"]')!;
const variableValue = page.querySelector<HTMLInputElement>('input[aria-label="Variable value"]')!;
await act(async () => {
setInputValue(variableName, "API_TOKEN");
setInputValue(variableValue, "draft-token");
});
await flushReact();
await act(async () => click(findButton(document.body, "Cancel")));
await flushReact();
expect(confirmSpy).toHaveBeenCalledWith("Discard unsaved environment changes?");
expect(getEnvironmentFormPage()).not.toBeNull();
confirmSpy.mockReturnValue(true);
await act(async () => click(findButton(document.body, "Cancel")));
await flushReact();
expect(getEnvironmentFormPage()).toBeNull();
});
it("keeps unload and in-app link warnings after env var changes are staged into the form", async () => {
root = createRoot(container);
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
const confirmSpy = vi.spyOn(window, "confirm").mockReturnValue(false);
await act(async () => {
root!.render(renderCompanyEnvironments(queryClient));
});
await flushReact();
await act(async () => {
click(findAction(container, "Edit"));
});
await waitForAssertion(() => {
expect(getEnvironmentFormPage()?.textContent).toContain("Edit environment");
});
const page = getEnvironmentFormPage()!;
await act(async () => click(findButton(page, "Add variable")));
await flushReact();
const variableName = page.querySelector<HTMLInputElement>('input[aria-label="Variable name"]')!;
const variableValue = page.querySelector<HTMLInputElement>('input[aria-label="Variable value"]')!;
await act(async () => {
setInputValue(variableName, "API_TOKEN");
setInputValue(variableValue, "draft-token");
});
await flushReact();
await act(async () => click(findButton(page, "Save")));
await flushReact();
expect(page.textContent).not.toContain("Unsaved changes");
const beforeUnload = new Event("beforeunload", { cancelable: true });
window.dispatchEvent(beforeUnload);
expect(beforeUnload.defaultPrevented).toBe(true);
const link = document.createElement("a");
link.href = "/agents/dashboard";
document.body.appendChild(link);
const clickEvent = new MouseEvent("click", { bubbles: true, cancelable: true, button: 0 });
link.dispatchEvent(clickEvent);
expect(confirmSpy).toHaveBeenCalledWith("Discard unsaved environment changes?");
expect(clickEvent.defaultPrevented).toBe(true);
expect(getEnvironmentFormPage()).not.toBeNull();
link.remove();
});
it("shows image setup controls only for providers advertising setup and capture support", async () => {
root = createRoot(container);
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
mockEnvironmentsApi.list.mockResolvedValue([
{ id: "env-1", name: "Daytona", driver: "sandbox", description: null, config: { provider: "daytona" } },
{ id: "env-2", name: "E2B", driver: "sandbox", description: null, config: { provider: "e2b" } },
{ id: "env-3", name: "Policy", driver: "sandbox", description: null, config: { provider: "policy" } },
]);
mockEnvironmentsApi.capabilities.mockResolvedValue({
adapters: [],
drivers: { local: "supported", ssh: "supported", sandbox: "supported", plugin: "unsupported" },
sandboxProviders: {
daytona: {
status: "supported",
supportsSavedProbe: true,
supportsUnsavedProbe: true,
supportsRunExecution: true,
supportsReusableLeases: true,
supportsInteractiveSetup: true,
interactiveSetupConnectionTypes: ["ssh"],
supportsTemplateCapture: true,
supportsTemplateDelete: true,
displayName: "Daytona",
},
e2b: {
status: "supported",
supportsSavedProbe: true,
supportsUnsavedProbe: true,
supportsRunExecution: true,
supportsReusableLeases: true,
supportsInteractiveSetup: false,
interactiveSetupConnectionTypes: [],
supportsTemplateCapture: false,
supportsTemplateDelete: false,
displayName: "E2B",
},
policy: {
status: "supported",
supportsSavedProbe: true,
supportsUnsavedProbe: true,
supportsRunExecution: true,
supportsReusableLeases: true,
supportsInteractiveSetup: true,
interactiveSetupConnectionTypes: ["ssh"],
supportsTemplateCapture: false,
supportsTemplateDelete: false,
displayName: "Policy Sandbox",
},
},
});
await act(async () => {
root!.render(renderCompanyEnvironments(queryClient));
});
await flushReact();
// Daytona supports setup + capture -> "Configure image" on its edit page.
await openEnvironmentEditPage(container);
await waitForAssertion(() => {
expect(getEnvironmentFormPage()?.textContent).toContain("Configure image");
});
expect(mockEnvironmentsApi.customImageTemplate).toHaveBeenCalledExactlyOnceWith("env-1", "company-1");
await act(async () => click(findButton(document.body, "Cancel")));
await waitForAssertion(() => expect(getEnvironmentFormPage()).toBeNull());
// E2B does not advertise interactive setup.
await openEnvironmentEditPage(container, 1);
await waitForAssertion(() => {
expect(getEnvironmentFormPage()?.textContent).toContain("Unsupported provider");
});
expect(getEnvironmentFormPage()?.textContent).not.toContain("Configure image");
await act(async () => click(findButton(document.body, "Cancel")));
await waitForAssertion(() => expect(getEnvironmentFormPage()).toBeNull());
// Provider advertises setup but cannot capture an image.
await openEnvironmentEditPage(container, 2);
await waitForAssertion(() => {
expect(getEnvironmentFormPage()?.textContent).toContain("Setup capture unavailable");
});
expect(getEnvironmentFormPage()?.textContent).not.toContain("Configure image");
});
it("shows a live connect command and removes it after cancellation", async () => {
root = createRoot(container);
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
const command = "ssh sandbox@setup.example.invalid -p 2222";
let activeSession: ReturnType<typeof createSession> | null = createSession();
let latestSession: ReturnType<typeof createSession> | null = activeSession;
mockEnvironmentsApi.list.mockResolvedValue([
{ id: "env-1", name: "Daytona", driver: "sandbox", description: null, config: { provider: "daytona" } },
]);
mockEnvironmentsApi.capabilities.mockResolvedValue({
adapters: [],
drivers: { local: "supported", ssh: "supported", sandbox: "supported", plugin: "unsupported" },
sandboxProviders: {
daytona: {
status: "supported",
supportsSavedProbe: true,
supportsUnsavedProbe: true,
supportsRunExecution: true,
supportsReusableLeases: true,
supportsInteractiveSetup: true,
interactiveSetupConnectionTypes: ["ssh"],
supportsTemplateCapture: true,
supportsTemplateDelete: true,
displayName: "Daytona",
},
},
});
mockEnvironmentsApi.customImageTemplate.mockImplementation(async () => ({
activeTemplate: null,
activeSession,
latestSession,
}));
mockEnvironmentsApi.customImageSetupSession.mockResolvedValue({
session: createSession(),
connectionPayload: { type: "ssh", command },
});
mockEnvironmentsApi.cancelCustomImageSetupSession.mockImplementation(async () => {
activeSession = null;
latestSession = createSession({ status: "cancelled", finishedAt: "2026-06-25T20:10:00.000Z" });
return latestSession;
});
await act(async () => {
root!.render(renderCompanyEnvironments(queryClient));
});
await flushReact();
await openEnvironmentEditPage(container);
await waitForAssertion(() => {
expect(getEnvironmentFormPage()?.textContent).toContain(command);
});
await act(async () => click(findButton(getEnvironmentFormPage()!, "Cancel")));
await waitForAssertion(() => {
expect(getEnvironmentFormPage()?.textContent).toContain("Setup cancelled");
});
expect(mockEnvironmentsApi.cancelCustomImageSetupSession).toHaveBeenCalledExactlyOnceWith(
"session-1",
{ reason: "operator cancelled" },
);
expect(getEnvironmentFormPage()?.textContent).not.toContain(command);
});
it("opens an embedded browser terminal automatically while preserving the SSH command fallback", async () => {
root = createRoot(container);
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
const command = "ssh sandbox@setup.example.invalid -p 2222";
mockEnvironmentsApi.list.mockResolvedValue([
{ id: "env-1", name: "Daytona", driver: "sandbox", description: null, config: { provider: "daytona" } },
]);
mockEnvironmentsApi.capabilities.mockResolvedValue(supportedDaytonaCapabilities());
mockEnvironmentsApi.customImageTemplate.mockResolvedValue({
activeTemplate: null,
activeSession: createSession(),
latestSession: createSession(),
});
mockEnvironmentsApi.customImageSetupSession.mockResolvedValue({
session: createSession(),
connectionPayload: { type: "ssh", command },
});
await act(async () => {
root!.render(renderCompanyEnvironments(queryClient));
});
await flushReact();
await openEnvironmentEditPage(container);
await waitForAssertion(() => {
expect(getEnvironmentFormPage()?.textContent).toContain(command);
expect(getEnvironmentFormPage()?.textContent).toContain("Browser terminal");
expect(getEnvironmentFormPage()?.textContent).toContain("SSH command fallback");
expect(mockEnvironmentsApi.createCustomImageTerminalSessionToken).toHaveBeenCalledExactlyOnceWith("session-1", {});
expect(FakeWebSocket.instances).toHaveLength(1);
});
expect(FakeWebSocket.instances[0].url).toContain(
"/api/environment-custom-image-setup-sessions/session-1/terminal/ws?terminalSessionId=terminal-session-1",
);
expect(FakeWebSocket.instances[0].url).not.toContain("token=");
expect(FakeWebSocket.instances[0].url).not.toContain("terminal-token-terminal-token-123456");
expect(FakeWebSocket.instances[0].url).toContain("cols=120");
expect(FakeWebSocket.instances[0].url).toContain("rows=32");
expect(xtermMocks.terminalInstances[0].options.cursorBlink).toBe(true);
expect(xtermMocks.terminalInstances[0].options.cursorInactiveStyle).toBe("bar");
expect(xtermMocks.terminalInstances[0].options.cursorStyle).toBe("bar");
expect(xtermMocks.terminalInstances[0].options.cursorWidth).toBe(2);
expect(xtermMocks.terminalInstances[0].options.customGlyphs).toBe(true);
expect(xtermMocks.terminalInstances[0].options.letterSpacing).toBe(0);
expect(xtermMocks.terminalInstances[0].options.theme).toMatchObject({
cursor: "#22d3ee",
cursorAccent: "#020617",
});
expect(String(xtermMocks.terminalInstances[0].options.fontFamily)).toContain("Nerd Font");
await act(async () => {
FakeWebSocket.instances[0].open();
FakeWebSocket.instances[0].emitMessage(JSON.stringify({ type: "ready" }));
FakeWebSocket.instances[0].emitMessage(JSON.stringify({ type: "output", data: "\u001b[?2004hsetup shell\r\n$ " }));
});
const terminalScreen = getEnvironmentFormPage()?.querySelector<HTMLElement>(
"[data-testid='custom-image-terminal-screen-session-1']",
);
await waitForAssertion(() => {
expect(terminalScreen?.dataset.mockXtermOpen).toBe("true");
expect(xtermMocks.terminalInstances[0].writes.join("")).toContain("setup shell");
expect(xtermMocks.terminalInstances[0].focused).toBe(true);
expect(document.activeElement).toBe(terminalScreen);
expect(getEnvironmentFormPage()?.textContent).toContain(command);
});
expect(FakeWebSocket.instances[0].sent).toContain(JSON.stringify({
type: "auth",
token: "terminal-token-terminal-token-123456",
}));
expect(FakeWebSocket.instances[0].sent).toContain(JSON.stringify({ type: "resize", cols: 120, rows: 32 }));
await act(async () => {
xtermMocks.terminalInstances[0].emitData("l");
xtermMocks.terminalInstances[0].emitData("\r");
});
expect(FakeWebSocket.instances[0].sent).toContain(JSON.stringify({ type: "input", data: "l" }));
expect(FakeWebSocket.instances[0].sent).toContain(JSON.stringify({ type: "input", data: "\r" }));
});
it("keeps the environment edit page open when Escape is pressed in setup terminal", async () => {
root = createRoot(container);
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
const command = "ssh sandbox@setup.example.invalid -p 2222";
mockEnvironmentsApi.list.mockResolvedValue([
{ id: "env-1", name: "Daytona", driver: "sandbox", description: null, config: { provider: "daytona" } },
]);
mockEnvironmentsApi.capabilities.mockResolvedValue(supportedDaytonaCapabilities());
mockEnvironmentsApi.customImageTemplate.mockResolvedValue({
activeTemplate: null,
activeSession: createSession(),
latestSession: createSession(),
});
mockEnvironmentsApi.customImageSetupSession.mockResolvedValue({
session: createSession(),
connectionPayload: { type: "ssh", command },
});
await act(async () => {
root!.render(renderCompanyEnvironments(queryClient));
});
await flushReact();
await openEnvironmentEditPage(container);
let terminalScreen: HTMLElement | null = null;
await waitForAssertion(() => {
terminalScreen = getEnvironmentFormPage()?.querySelector<HTMLElement>(
"[data-testid='custom-image-terminal-screen-session-1']",
) ?? null;
expect(terminalScreen).toBeTruthy();
expect(document.body.querySelector("[role='dialog']")).toBeNull();
});
await act(async () => {
terminalScreen?.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true }));
});
await flushReact();
expect(getEnvironmentFormPage()?.textContent).toContain(command);
expect(getEnvironmentFormPage()?.textContent).toContain("Edit environment");
});
it("does not render connect details when an active session refreshes as expired", async () => {
root = createRoot(container);
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
const command = "ssh sandbox@setup.example.invalid -p 2222";
mockEnvironmentsApi.list.mockResolvedValue([
{ id: "env-1", name: "Daytona", driver: "sandbox", description: null, config: { provider: "daytona" } },
]);
mockEnvironmentsApi.capabilities.mockResolvedValue({
adapters: [],
drivers: { local: "supported", ssh: "supported", sandbox: "supported", plugin: "unsupported" },
sandboxProviders: {
daytona: {
status: "supported",
supportsSavedProbe: true,
supportsUnsavedProbe: true,
supportsRunExecution: true,
supportsReusableLeases: true,
supportsInteractiveSetup: true,
interactiveSetupConnectionTypes: ["ssh"],
supportsTemplateCapture: true,
supportsTemplateDelete: true,
displayName: "Daytona",
},
},
});
mockEnvironmentsApi.customImageTemplate.mockResolvedValue({
activeTemplate: null,
activeSession: createSession(),
latestSession: createSession(),
});
mockEnvironmentsApi.customImageSetupSession.mockResolvedValue({
session: createSession({ status: "timed_out", finishedAt: "2026-06-25T20:10:00.000Z" }),
connectionPayload: { type: "ssh", command },
});
await act(async () => {
root!.render(renderCompanyEnvironments(queryClient));
});
await flushReact();
await openEnvironmentEditPage(container);
await waitForAssertion(() => {
expect(getEnvironmentFormPage()?.textContent).toContain("Setup expired");
});
expect(getEnvironmentFormPage()?.textContent).not.toContain(command);
});
it("shows a setup connection refresh fallback without breaking finish or cancel", async () => {
root = createRoot(container);
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
mockEnvironmentsApi.list.mockResolvedValue([
{ id: "env-1", name: "Daytona", driver: "sandbox", description: null, config: { provider: "daytona" } },
]);
mockEnvironmentsApi.capabilities.mockResolvedValue(supportedDaytonaCapabilities());
mockEnvironmentsApi.customImageTemplate.mockResolvedValue({
activeTemplate: null,
activeSession: createSession(),
latestSession: createSession(),
});
mockEnvironmentsApi.customImageSetupSession.mockRejectedValue(new Error("proxy unavailable"));
await act(async () => {
root!.render(renderCompanyEnvironments(queryClient));
});
await flushReact();
await openEnvironmentEditPage(container);
await waitForAssertion(() => {
expect(getEnvironmentFormPage()?.textContent).toContain("Setup connection details could not be refreshed.");
});
const dialog = getEnvironmentFormPage()!;
expect(findButton(dialog, "Finished")?.disabled).toBe(false);
expect(findButton(dialog, "Cancel")?.disabled).toBe(false);
await act(async () => click(findButton(dialog, "Finished")));
await flushReact();
expect(mockEnvironmentsApi.finishCustomImageSetupSession).toHaveBeenCalledExactlyOnceWith("session-1", {});
});
it("shows provider fallback messaging for unsupported setup connection payloads", async () => {
root = createRoot(container);
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
mockEnvironmentsApi.list.mockResolvedValue([
{ id: "env-1", name: "Daytona", driver: "sandbox", description: null, config: { provider: "daytona" } },
]);
mockEnvironmentsApi.capabilities.mockResolvedValue(supportedDaytonaCapabilities());
mockEnvironmentsApi.customImageTemplate.mockResolvedValue({
activeTemplate: null,
activeSession: createSession(),
latestSession: createSession(),
});
mockEnvironmentsApi.customImageSetupSession.mockResolvedValue({
session: createSession(),
connectionPayload: { type: "browser_terminal", command: null },
});
await act(async () => {
root!.render(renderCompanyEnvironments(queryClient));
});
await flushReact();
await openEnvironmentEditPage(container);
await waitForAssertion(() => {
expect(getEnvironmentFormPage()?.textContent).toContain("Browser terminal is not available for this provider connection.");
});
const dialog = getEnvironmentFormPage()!;
expect(findButton(dialog, "Finished")?.disabled).toBe(false);
expect(findButton(dialog, "Cancel")?.disabled).toBe(false);
});
it("shows active template controls for refresh, rollback, and disable", async () => {
root = createRoot(container);
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
const activeTemplateId = "12345678-90ab-cdef-1234-567890abcdef";
mockEnvironmentsApi.list.mockResolvedValue([
{ id: "env-1", name: "Daytona", driver: "sandbox", description: null, config: { provider: "daytona" } },
]);
mockEnvironmentsApi.capabilities.mockResolvedValue({
adapters: [],
drivers: { local: "supported", ssh: "supported", sandbox: "supported", plugin: "unsupported" },
sandboxProviders: {
daytona: {
status: "supported",
supportsSavedProbe: true,
supportsUnsavedProbe: true,
supportsRunExecution: true,
supportsReusableLeases: true,
supportsInteractiveSetup: true,
interactiveSetupConnectionTypes: ["ssh"],
supportsTemplateCapture: true,
supportsTemplateDelete: true,
displayName: "Daytona",
},
},
});
mockEnvironmentsApi.customImageTemplate.mockResolvedValue({
activeTemplate: createTemplate({ id: activeTemplateId }),
activeSession: null,
latestSession: null,
});
await act(async () => {
root!.render(renderCompanyEnvironments(queryClient));
});
await flushReact();
await openEnvironmentEditPage(container);
await waitForAssertion(() => {
const dialog = getEnvironmentFormPage();
expect(dialog?.textContent).toContain("Active template");
expect(dialog?.textContent).toContain("redacted-template-ref");
expect(dialog?.textContent).not.toContain("id 12345678-90a");
expect(
dialog?.querySelector(
"[title='Provider snapshot ref redacted-template-ref (Paperclip template 12345678-90ab-cdef-1234-567890abcdef)']",
),
).toBeTruthy();
expect(findButton(dialog!, "Refresh")).toBeTruthy();
expect(findButton(dialog!, "Rollback")).toBeTruthy();
expect(findButton(dialog!, "Disable")).toBeTruthy();
});
await act(async () => click(findButton(getEnvironmentFormPage()!, "Refresh")));
await flushReact();
expect(mockEnvironmentsApi.startCustomImageSetupSession).toHaveBeenCalledWith(
"env-1",
"company-1",
{ templateId: activeTemplateId },
);
await waitForAssertion(() => {
expect(getEnvironmentFormPage()?.textContent).toContain("Browser terminal");
expect(getEnvironmentFormPage()?.textContent).toContain("SSH command fallback");
expect(mockEnvironmentsApi.createCustomImageTerminalSessionToken).toHaveBeenCalledExactlyOnceWith("session-1", {});
expect(FakeWebSocket.instances).toHaveLength(1);
});
});
it("allows stale capturing setup sessions to be cancelled back to active template controls", async () => {
root = createRoot(container);
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
const activeTemplate = createTemplate({ id: "template-active" });
let activeSession: ReturnType<typeof createSession> | null = createSession({ status: "capturing" });
let latestSession: ReturnType<typeof createSession> | null = activeSession;
mockEnvironmentsApi.list.mockResolvedValue([
{ id: "env-1", name: "Daytona", driver: "sandbox", description: null, config: { provider: "daytona" } },
]);
mockEnvironmentsApi.capabilities.mockResolvedValue(supportedDaytonaCapabilities());
mockEnvironmentsApi.customImageTemplate.mockImplementation(async () => ({
activeTemplate,
activeSession,
latestSession,
}));
mockEnvironmentsApi.customImageSetupSession.mockResolvedValue({
session: activeSession,
connectionPayload: null,
});
mockEnvironmentsApi.cancelCustomImageSetupSession.mockImplementation(async () => {
activeSession = null;
latestSession = createSession({ status: "cancelled", finishedAt: "2026-06-25T20:10:00.000Z" });
return latestSession;
});
await act(async () => {
root!.render(renderCompanyEnvironments(queryClient));
});
await flushReact();
await openEnvironmentEditPage(container);
await waitForAssertion(() => {
const dialog = getEnvironmentFormPage()!;
expect(dialog.textContent).toContain("Capturing template");
expect(dialog.textContent).toContain("Capture is in progress.");
expect(findButton(dialog, "Finished")?.disabled).toBe(true);
expect(findButton(dialog, "Cancel")?.disabled).toBe(false);
});
await act(async () => click(findButton(getEnvironmentFormPage()!, "Cancel")));
await waitForAssertion(() => {
const dialog = getEnvironmentFormPage()!;
expect(dialog.textContent).toContain("Active template");
expect(findButton(dialog, "Refresh")).toBeTruthy();
});
expect(mockEnvironmentsApi.cancelCustomImageSetupSession).toHaveBeenCalledExactlyOnceWith(
"session-1",
{ reason: "operator cancelled" },
);
});
it("shows an out-of-sync warning when the active template no longer matches the saved config", async () => {
root = createRoot(container);
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
mockEnvironmentsApi.list.mockResolvedValue([
{ id: "env-1", name: "Daytona", driver: "sandbox", description: null, config: { provider: "daytona" } },
]);
mockEnvironmentsApi.capabilities.mockResolvedValue(supportedDaytonaCapabilities());
mockEnvironmentsApi.customImageTemplate.mockResolvedValue({
activeTemplate: createTemplate({ id: "template-active" }),
activeTemplateMatchesConfig: false,
activeSession: null,
latestSession: null,
});
await act(async () => {
root!.render(renderCompanyEnvironments(queryClient));
});
await flushReact();
await openEnvironmentEditPage(container);
await waitForAssertion(() => {
const dialog = getEnvironmentFormPage()!;
expect(dialog.textContent).toContain("Active template");
expect(dialog.textContent).toContain("Not in use — the environment configuration changed");
});
});
it("names the changed boot-source field in the out-of-sync banner for a boot-source drift", async () => {
root = createRoot(container);
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
mockEnvironmentsApi.list.mockResolvedValue([
{ id: "env-1", name: "Daytona", driver: "sandbox", description: null, config: { provider: "daytona" } },
]);
mockEnvironmentsApi.capabilities.mockResolvedValue(supportedDaytonaCapabilities());
mockEnvironmentsApi.customImageTemplate.mockResolvedValue({
activeTemplate: createTemplate({ id: "template-active" }),
activeTemplateMatchesConfig: false,
activeTemplateDrift: {
classification: "boot_source_drift",
driftedPaths: [{ path: "snapshot", from: "a", to: "b" }],
},
activeSession: null,
latestSession: null,
});
await act(async () => {
root!.render(renderCompanyEnvironments(queryClient));
});
await flushReact();
await openEnvironmentEditPage(container);
await waitForAssertion(() => {
const dialog = getEnvironmentFormPage()!;
expect(dialog.textContent).toContain("Not in use — Base image changed: snapshot `a` -> `b`");
expect(dialog.textContent).not.toContain("the environment configuration changed");
});
});
it("keeps the generic out-of-sync banner for an unclassified drift", async () => {
root = createRoot(container);
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
mockEnvironmentsApi.list.mockResolvedValue([
{ id: "env-1", name: "Daytona", driver: "sandbox", description: null, config: { provider: "daytona" } },
]);
mockEnvironmentsApi.capabilities.mockResolvedValue(supportedDaytonaCapabilities());
mockEnvironmentsApi.customImageTemplate.mockResolvedValue({
activeTemplate: createTemplate({ id: "template-active" }),
activeTemplateMatchesConfig: false,
activeTemplateDrift: { classification: "unclassified", driftedPaths: [{ path: "apiUrl" }] },
activeSession: null,
latestSession: null,
});
await act(async () => {
root!.render(renderCompanyEnvironments(queryClient));
});
await flushReact();
await openEnvironmentEditPage(container);
await waitForAssertion(() => {
const dialog = getEnvironmentFormPage()!;
expect(dialog.textContent).toContain("Not in use — the environment configuration changed");
expect(dialog.textContent).not.toContain("Base image changed");
});
});
it("does not show the out-of-sync warning when the active template matches the saved config", async () => {
root = createRoot(container);
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
mockEnvironmentsApi.list.mockResolvedValue([
{ id: "env-1", name: "Daytona", driver: "sandbox", description: null, config: { provider: "daytona" } },
]);
mockEnvironmentsApi.capabilities.mockResolvedValue(supportedDaytonaCapabilities());
mockEnvironmentsApi.customImageTemplate.mockResolvedValue({
activeTemplate: createTemplate({ id: "template-active" }),
activeTemplateMatchesConfig: true,
activeSession: null,
latestSession: null,
});
await act(async () => {
root!.render(renderCompanyEnvironments(queryClient));
});
await flushReact();
await openEnvironmentEditPage(container);
await waitForAssertion(() => {
const dialog = getEnvironmentFormPage()!;
expect(dialog.textContent).toContain("Active template");
expect(dialog.textContent).not.toContain("Not in use");
});
});
it("passes company context when rolling back and disabling an active template", async () => {
root = createRoot(container);
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
mockEnvironmentsApi.list.mockResolvedValue([
{ id: "env-1", name: "Daytona", driver: "sandbox", description: null, config: { provider: "daytona" } },
]);
mockEnvironmentsApi.capabilities.mockResolvedValue({
adapters: [],
drivers: { local: "supported", ssh: "supported", sandbox: "supported", plugin: "unsupported" },
sandboxProviders: {
daytona: {
status: "supported",
supportsSavedProbe: true,
supportsUnsavedProbe: true,
supportsRunExecution: true,
supportsReusableLeases: true,
supportsInteractiveSetup: true,
interactiveSetupConnectionTypes: ["ssh"],
supportsTemplateCapture: true,
supportsTemplateDelete: true,
displayName: "Daytona",
},
},
});
mockEnvironmentsApi.customImageTemplate.mockResolvedValue({
activeTemplate: createTemplate({ id: "template-active" }),
activeSession: null,
latestSession: null,
});
await act(async () => {
root!.render(renderCompanyEnvironments(queryClient));
});
await flushReact();
await openEnvironmentEditPage(container);
await waitForAssertion(() => {
const dialog = getEnvironmentFormPage();
expect(dialog?.textContent).toContain("Active template");
expect(findButton(dialog!, "Rollback")).toBeTruthy();
expect(findButton(dialog!, "Disable")).toBeTruthy();
});
await act(async () => click(findButton(getEnvironmentFormPage()!, "Rollback")));
await waitForAssertion(() => {
expect(mockEnvironmentsApi.rollbackCustomImageTemplate).toHaveBeenCalledExactlyOnceWith("env-1", "company-1");
});
await act(async () => click(findButton(getEnvironmentFormPage()!, "Disable")));
await waitForAssertion(() => {
expect(mockEnvironmentsApi.disableCustomImageTemplate).toHaveBeenCalledExactlyOnceWith("env-1", "company-1");
});
});
function setupOutOfSyncTemplatePanel() {
mockEnvironmentsApi.list.mockResolvedValue([
{ id: "env-1", name: "Daytona", driver: "sandbox", description: null, config: { provider: "daytona" } },
]);
mockEnvironmentsApi.capabilities.mockResolvedValue({
adapters: [],
drivers: { local: "supported", ssh: "supported", sandbox: "supported", plugin: "unsupported" },
sandboxProviders: {
daytona: {
status: "supported",
supportsSavedProbe: true,
supportsUnsavedProbe: true,
supportsRunExecution: true,
supportsReusableLeases: true,
supportsInteractiveSetup: true,
interactiveSetupConnectionTypes: ["ssh"],
supportsTemplateCapture: true,
supportsTemplateDelete: true,
displayName: "Daytona",
},
},
});
mockEnvironmentsApi.customImageTemplate.mockResolvedValue({
activeTemplate: createTemplate({ id: "template-active" }),
activeTemplateMatchesConfig: false,
activeSession: null,
latestSession: null,
});
}
it("relinks an out-of-sync template and names both remedies in the copy", async () => {
root = createRoot(container);
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
setupOutOfSyncTemplatePanel();
await act(async () => {
root!.render(renderCompanyEnvironments(queryClient));
});
await flushReact();
await openEnvironmentEditPage(container);
await waitForAssertion(() => {
const dialog = getEnvironmentFormPage()!;
expect(dialog.textContent).toContain("relink this image or capture a new one");
expect(findButton(dialog, "Relink")).toBeTruthy();
});
await act(async () => click(findButton(getEnvironmentFormPage()!, "Relink")));
await waitForAssertion(() => {
expect(mockEnvironmentsApi.relinkCustomImageTemplate).toHaveBeenCalledExactlyOnceWith("env-1", "company-1");
});
});
it("confirms boot-source drift before re-sending the relink with the flag", async () => {
root = createRoot(container);
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
setupOutOfSyncTemplatePanel();
mockEnvironmentsApi.relinkCustomImageTemplate
.mockRejectedValueOnce(new ApiError("Confirm the relink.", 409, {
error: "Confirm the relink.",
details: {
classification: "boot_source_drift",
driftedPaths: [{ path: "image", from: "fake:base", to: "fake:other" }],
},
}))
.mockResolvedValueOnce({
template: createTemplate({ id: "template-relinked" }),
classification: "boot_source_drift",
});
const confirmSpy = vi.spyOn(window, "confirm").mockReturnValue(true);
try {
await act(async () => {
root!.render(renderCompanyEnvironments(queryClient));
});
await flushReact();
await openEnvironmentEditPage(container);
await waitForAssertion(() => {
expect(findButton(getEnvironmentFormPage()!, "Relink")).toBeTruthy();
});
await act(async () => click(findButton(getEnvironmentFormPage()!, "Relink")));
await waitForAssertion(() => {
expect(mockEnvironmentsApi.relinkCustomImageTemplate).toHaveBeenNthCalledWith(1, "env-1", "company-1");
expect(mockEnvironmentsApi.relinkCustomImageTemplate).toHaveBeenNthCalledWith(
2,
"env-1",
"company-1",
{ confirmBootSourceDrift: true },
);
});
expect(confirmSpy).toHaveBeenCalledTimes(1);
expect(confirmSpy.mock.calls[0]![0]).toContain("image fake:base -> fake:other");
} finally {
confirmSpy.mockRestore();
}
});
it("does not re-send the relink when the operator declines the confirmation", async () => {
root = createRoot(container);
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
setupOutOfSyncTemplatePanel();
mockEnvironmentsApi.relinkCustomImageTemplate.mockRejectedValueOnce(new ApiError("Cannot verify.", 409, {
error: "Cannot verify.",
details: { classification: "unclassified", driftedPaths: [{ path: "apiUrl" }] },
}));
const confirmSpy = vi.spyOn(window, "confirm").mockReturnValue(false);
try {
await act(async () => {
root!.render(renderCompanyEnvironments(queryClient));
});
await flushReact();
await openEnvironmentEditPage(container);
await waitForAssertion(() => {
expect(findButton(getEnvironmentFormPage()!, "Relink")).toBeTruthy();
});
await act(async () => click(findButton(getEnvironmentFormPage()!, "Relink")));
await waitForAssertion(() => {
expect(confirmSpy).toHaveBeenCalledTimes(1);
});
expect(confirmSpy.mock.calls[0]![0]).toContain("cannot verify the boot source");
expect(mockEnvironmentsApi.relinkCustomImageTemplate).toHaveBeenCalledTimes(1);
expect(mockEnvironmentsApi.relinkCustomImageTemplate).toHaveBeenCalledWith("env-1", "company-1");
} finally {
confirmSpy.mockRestore();
}
});
it("offers the implicit Local option in the default picker by default", async () => {
root = createRoot(container);
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
await act(async () => {
root!.render(renderCompanyEnvironments(queryClient));
});
await flushReact();
const options = Array.from(container.querySelectorAll("option"));
expect(options.some((option) => option.textContent?.trim() === "Local")).toBe(true);
});
it("hides the implicit Local option in the default picker under managed-sandbox-only", async () => {
mockInstanceSettingsApi.getExperimental.mockResolvedValue({
enableEnvironments: true,
enableManagedSandboxOnly: true,
});
root = createRoot(container);
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
await act(async () => {
root!.render(renderCompanyEnvironments(queryClient));
});
await flushReact();
const options = Array.from(container.querySelectorAll("option"));
expect(options.some((option) => option.textContent?.trim() === "Local")).toBe(false);
// Saved non-local environments remain selectable defaults.
expect(options.some((option) => option.textContent?.includes("Alpha"))).toBe(true);
});
it("reassigns this company's active agents to the chosen environment before deleting", async () => {
mockAgentsApi.list.mockResolvedValue([
{ id: "agent-1", name: "Iris", status: "active", defaultEnvironmentId: "env-1" },
{ id: "agent-2", name: "Miles", status: "active", defaultEnvironmentId: "env-2" },
{ id: "agent-3", name: "Retired", status: "terminated", defaultEnvironmentId: "env-1" },
]);
mockEnvironmentsApi.deleteBlastRadius.mockResolvedValue(
createDeleteBlastRadius({ staticReferences: { agentDefaultCount: 2 } }),
);
root = createRoot(container);
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
await act(async () => {
root!.render(renderCompanyEnvironments(queryClient, `${ENVIRONMENTS_PATH}/env-1/edit`));
});
await waitForAssertion(() => {
expect(getEnvironmentFormPage()?.textContent).toContain("Edit environment");
});
const deleteButton = document.body.querySelector<HTMLButtonElement>("[data-testid='environment-delete-button']");
expect(deleteButton).not.toBeNull();
await act(async () => click(deleteButton));
await waitForAssertion(() => {
expect(
document.body.querySelector("[data-testid='environment-delete-dialog']")?.textContent,
).toContain("1 agent uses this environment as their default");
});
const dialog = document.body.querySelector<HTMLElement>("[data-testid='environment-delete-dialog']")!;
// Only this company's active agents are reassignable; the terminated one is
// left to the FK fallback and surfaces through the impact note instead.
expect(dialog.textContent).toContain("Iris");
expect(dialog.textContent).not.toContain("Retired");
expect(dialog.textContent).toContain("Other references to this environment");
const reassignSelect = dialog.querySelector<HTMLSelectElement>(
"[data-testid='environment-delete-reassign-select']",
)!;
const optionLabels = Array.from(reassignSelect.querySelectorAll("option")).map((option) =>
option.textContent?.trim(),
);
expect(optionLabels).toEqual(["Default: Local", "Beta · sandbox"]);
await act(async () => setSelectValue(reassignSelect, "env-2"));
await act(async () =>
click(document.body.querySelector("[data-testid='environment-delete-confirm']")),
);
await flushReact();
expect(mockAgentsApi.update).toHaveBeenCalledExactlyOnceWith(
"agent-1",
{ defaultEnvironmentId: "env-2" },
"company-1",
);
expect(mockEnvironmentsApi.remove).toHaveBeenCalledExactlyOnceWith("env-1");
expect(mockAgentsApi.update.mock.invocationCallOrder[0]).toBeLessThan(
mockEnvironmentsApi.remove.mock.invocationCallOrder[0],
);
await waitForAssertion(() => {
expect(getEnvironmentFormPage()).toBeNull();
});
});
it("deletes without any reassignment when no agents use the environment", async () => {
mockAgentsApi.list.mockResolvedValue([
{ id: "agent-2", name: "Miles", status: "active", defaultEnvironmentId: "env-2" },
]);
root = createRoot(container);
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
await act(async () => {
root!.render(renderCompanyEnvironments(queryClient, `${ENVIRONMENTS_PATH}/env-1/edit`));
});
await waitForAssertion(() => {
expect(getEnvironmentFormPage()?.textContent).toContain("Edit environment");
});
await act(async () =>
click(document.body.querySelector("[data-testid='environment-delete-button']")),
);
await waitForAssertion(() => {
expect(
document.body.querySelector("[data-testid='environment-delete-dialog']")?.textContent,
).toContain("permanently deleted");
});
expect(
document.body.querySelector("[data-testid='environment-delete-reassign-select']"),
).toBeNull();
await act(async () =>
click(document.body.querySelector("[data-testid='environment-delete-confirm']")),
);
await flushReact();
expect(mockAgentsApi.update).not.toHaveBeenCalled();
expect(mockEnvironmentsApi.remove).toHaveBeenCalledExactlyOnceWith("env-1");
await waitForAssertion(() => {
expect(getEnvironmentFormPage()).toBeNull();
});
});
it("disables the delete confirmation while the server would block the delete", async () => {
mockEnvironmentsApi.deleteBlastRadius.mockResolvedValue(
createDeleteBlastRadius({
canDelete: false,
deleteBlockedReasons: ["instance_default"],
staticReferences: { isInstanceDefault: true },
}),
);
root = createRoot(container);
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
await act(async () => {
root!.render(renderCompanyEnvironments(queryClient, `${ENVIRONMENTS_PATH}/env-1/edit`));
});
await waitForAssertion(() => {
expect(getEnvironmentFormPage()?.textContent).toContain("Edit environment");
});
await act(async () =>
click(document.body.querySelector("[data-testid='environment-delete-button']")),
);
await waitForAssertion(() => {
expect(
document.body.querySelector("[data-testid='environment-delete-dialog']")?.textContent,
).toContain("Cannot delete the current instance default environment");
});
const confirm = document.body.querySelector<HTMLButtonElement>(
"[data-testid='environment-delete-confirm']",
)!;
expect(confirm.disabled).toBe(true);
expect(
document.body.querySelector("[data-testid='environment-delete-reassign-select']"),
).toBeNull();
expect(mockEnvironmentsApi.remove).not.toHaveBeenCalled();
});
it("links the workspaces holding blocking sandbox leases in the delete dialog", async () => {
mockEnvironmentsApi.deleteBlastRadius.mockResolvedValue(
createDeleteBlastRadius({
canDelete: false,
deleteBlockedReasons: ["reusable_sandbox_lease"],
reusableSandboxLeaseCount: 3,
reusableSandboxLeaseHolders: [
{
leaseId: "lease-1",
executionWorkspaceId: "ws-1",
executionWorkspaceName: "ACME-7-fix-the-thing",
issueId: "issue-1",
issueIdentifier: "ACME-7",
issueTitle: "Fix the thing",
},
{
leaseId: "lease-2",
executionWorkspaceId: "ws-1",
executionWorkspaceName: "ACME-7-fix-the-thing",
issueId: "issue-2",
issueIdentifier: "ACME-9",
issueTitle: "Follow-up",
},
{
leaseId: "lease-3",
executionWorkspaceId: null,
executionWorkspaceName: null,
issueId: null,
issueIdentifier: null,
issueTitle: null,
},
],
}),
);
root = createRoot(container);
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
await act(async () => {
root!.render(renderCompanyEnvironments(queryClient, `${ENVIRONMENTS_PATH}/env-1/edit`));
});
await waitForAssertion(() => {
expect(getEnvironmentFormPage()?.textContent).toContain("Edit environment");
});
await act(async () =>
click(document.body.querySelector("[data-testid='environment-delete-button']")),
);
await waitForAssertion(() => {
expect(
document.body.querySelector("[data-testid='environment-delete-lease-holders']"),
).not.toBeNull();
});
const holders = document.body.querySelector<HTMLElement>(
"[data-testid='environment-delete-lease-holders']",
)!;
const workspaceLink = holders.querySelector<HTMLAnchorElement>("a[href='/execution-workspaces/ws-1']");
expect(workspaceLink?.textContent).toBe("ACME-7-fix-the-thing");
// The two leases on ws-1 collapse into one row naming both issues.
expect(holders.querySelectorAll("li")).toHaveLength(2);
expect(holders.textContent).toContain("2 sandbox leases");
expect(holders.textContent).toContain("ACME-7, ACME-9");
expect(holders.textContent).toContain("Workspace no longer on record");
// Leases as the only blocker are consentable: the confirm stays enabled
// and destroys the sandboxes as part of the delete.
const confirm = document.body.querySelector<HTMLButtonElement>(
"[data-testid='environment-delete-confirm']",
)!;
expect(confirm.disabled).toBe(false);
expect(confirm.textContent?.trim()).toBe("Destroy 3 sandboxes and delete");
await act(async () => click(confirm));
await flushReact();
expect(mockEnvironmentsApi.remove).toHaveBeenCalledExactlyOnceWith("env-1", {
destroyReusableSandboxLeases: true,
});
await waitForAssertion(() => {
expect(getEnvironmentFormPage()).toBeNull();
});
});
it("does not offer delete on the add-environment page", async () => {
root = createRoot(container);
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
await act(async () => {
root!.render(renderCompanyEnvironments(queryClient, `${ENVIRONMENTS_PATH}/new`));
});
await waitForAssertion(() => {
expect(getEnvironmentFormPage()?.textContent).toContain("Add environment");
});
expect(document.body.querySelector("[data-testid='environment-delete-button']")).toBeNull();
});
});