Skip to content
Merged
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
97 changes: 2 additions & 95 deletions lib/sandbox/__tests__/createSandboxPostHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { validateSandboxBody } from "@/lib/sandbox/validateSandboxBody";
import { createSandbox } from "@/lib/sandbox/createSandbox";
import { insertAccountSandbox } from "@/lib/supabase/account_sandboxes/insertAccountSandbox";
import { triggerRunSandboxCommand } from "@/lib/trigger/triggerRunSandboxCommand";
import { triggerSetupSandbox } from "@/lib/trigger/triggerSetupSandbox";

import { selectAccountSnapshots } from "@/lib/supabase/account_snapshots/selectAccountSnapshots";

vi.mock("@/lib/sandbox/validateSandboxBody", () => ({
Expand All @@ -26,9 +26,6 @@ vi.mock("@/lib/trigger/triggerRunSandboxCommand", () => ({
triggerRunSandboxCommand: vi.fn(),
}));

vi.mock("@/lib/trigger/triggerSetupSandbox", () => ({
triggerSetupSandbox: vi.fn(),
}));

vi.mock("@/lib/supabase/account_snapshots/selectAccountSnapshots", () => ({
selectAccountSnapshots: vi.fn(),
Expand Down Expand Up @@ -84,9 +81,6 @@ describe("createSandboxPostHandler", () => {
},
error: null,
});
vi.mocked(triggerSetupSandbox).mockResolvedValue({
id: "setup_abc123",
});
vi.mocked(triggerRunSandboxCommand).mockResolvedValue({
id: "run_abc123",
});
Expand Down Expand Up @@ -310,7 +304,7 @@ describe("createSandboxPostHandler", () => {
});
});

it("returns runId from setup task when no command is provided", async () => {
it("returns 200 without runId when no command is provided", async () => {
vi.mocked(validateSandboxBody).mockResolvedValue({
accountId: "acc_123",
orgId: null,
Expand All @@ -332,9 +326,6 @@ describe("createSandboxPostHandler", () => {
},
error: null,
});
vi.mocked(triggerSetupSandbox).mockResolvedValue({
id: "setup_abc123",
});

const request = createMockRequest();
const response = await createSandboxPostHandler(request);
Expand All @@ -349,15 +340,10 @@ describe("createSandboxPostHandler", () => {
sandboxStatus: "running",
timeout: 600000,
createdAt: "2024-01-01T00:00:00.000Z",
runId: "setup_abc123",
},
],
});
expect(triggerRunSandboxCommand).not.toHaveBeenCalled();
expect(triggerSetupSandbox).toHaveBeenCalledWith({
sandboxId: "sbx_123",
accountId: "acc_123",
});
});

it("returns 200 without runId when triggerRunSandboxCommand throws", async () => {
Expand All @@ -383,9 +369,6 @@ describe("createSandboxPostHandler", () => {
},
error: null,
});
vi.mocked(triggerSetupSandbox).mockResolvedValue({
id: "setup_abc123",
});
vi.mocked(triggerRunSandboxCommand).mockRejectedValue(new Error("Task trigger failed"));

const request = createMockRequest();
Expand All @@ -407,80 +390,4 @@ describe("createSandboxPostHandler", () => {
});
});

it("calls triggerSetupSandbox with sandboxId and accountId on every creation", async () => {
vi.mocked(validateSandboxBody).mockResolvedValue({
accountId: "acc_123",
orgId: null,
authToken: "token",
});
vi.mocked(selectAccountSnapshots).mockResolvedValue([]);
vi.mocked(createSandbox).mockResolvedValue({
sandboxId: "sbx_789",
sandboxStatus: "running",
timeout: 600000,
createdAt: "2024-01-01T00:00:00.000Z",
});
vi.mocked(insertAccountSandbox).mockResolvedValue({
data: {
id: "record_123",
account_id: "acc_123",
sandbox_id: "sbx_789",
created_at: "2024-01-01T00:00:00.000Z",
},
error: null,
});
vi.mocked(triggerSetupSandbox).mockResolvedValue({
id: "setup_xyz",
});

const request = createMockRequest();
await createSandboxPostHandler(request);

expect(triggerSetupSandbox).toHaveBeenCalledWith({
sandboxId: "sbx_789",
accountId: "acc_123",
});
});

it("returns 200 without runId when triggerSetupSandbox throws and no command", async () => {
vi.mocked(validateSandboxBody).mockResolvedValue({
accountId: "acc_123",
orgId: null,
authToken: "token",
});
vi.mocked(selectAccountSnapshots).mockResolvedValue([]);
vi.mocked(createSandbox).mockResolvedValue({
sandboxId: "sbx_123",
sandboxStatus: "running",
timeout: 600000,
createdAt: "2024-01-01T00:00:00.000Z",
});
vi.mocked(insertAccountSandbox).mockResolvedValue({
data: {
id: "record_123",
account_id: "acc_123",
sandbox_id: "sbx_123",
created_at: "2024-01-01T00:00:00.000Z",
},
error: null,
});
vi.mocked(triggerSetupSandbox).mockRejectedValue(new Error("Setup trigger failed"));

const request = createMockRequest();
const response = await createSandboxPostHandler(request);

expect(response.status).toBe(200);
const json = await response.json();
expect(json).toEqual({
status: "success",
sandboxes: [
{
sandboxId: "sbx_123",
sandboxStatus: "running",
timeout: 600000,
createdAt: "2024-01-01T00:00:00.000Z",
},
],
});
});
});
15 changes: 2 additions & 13 deletions lib/sandbox/createSandboxPostHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { createSandbox } from "@/lib/sandbox/createSandbox";
import { validateSandboxBody } from "@/lib/sandbox/validateSandboxBody";
import { insertAccountSandbox } from "@/lib/supabase/account_sandboxes/insertAccountSandbox";
import { triggerRunSandboxCommand } from "@/lib/trigger/triggerRunSandboxCommand";
import { triggerSetupSandbox } from "@/lib/trigger/triggerSetupSandbox";

import { selectAccountSnapshots } from "@/lib/supabase/account_snapshots/selectAccountSnapshots";

/**
Expand Down Expand Up @@ -41,19 +41,8 @@ export async function createSandboxPostHandler(request: NextRequest): Promise<Ne
sandbox_id: result.sandboxId,
});

// Trigger the setup-sandbox task (fire-and-forget)
// Trigger the command execution task if a command was provided
let runId: string | undefined;
try {
const setupHandle = await triggerSetupSandbox({
sandboxId: result.sandboxId,
accountId: validated.accountId,
});
runId = setupHandle.id;
} catch (triggerError) {
console.error("Failed to trigger setup-sandbox task:", triggerError);
}

// Trigger the command execution task if a command was provided (overrides runId)
if (validated.command) {
try {
const handle = await triggerRunSandboxCommand({
Expand Down
42 changes: 0 additions & 42 deletions lib/trigger/__tests__/triggerSetupSandbox.test.ts

This file was deleted.

17 changes: 0 additions & 17 deletions lib/trigger/triggerSetupSandbox.ts

This file was deleted.