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
78 changes: 55 additions & 23 deletions lib/sandbox/__tests__/createSandboxFromSnapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import type { Sandbox } from "@vercel/sandbox";

import { createSandboxFromSnapshot } from "../createSandboxFromSnapshot";

const mockSelectAccountSnapshots = vi.fn();
const mockGetValidSnapshotId = vi.fn();
const mockInsertAccountSandbox = vi.fn();
const mockCreateSandbox = vi.fn();
const mockCreateSandboxWithFallback = vi.fn();

vi.mock("@/lib/sandbox/createSandbox", () => ({
createSandbox: (...args: unknown[]) => mockCreateSandbox(...args),
vi.mock("@/lib/sandbox/createSandboxWithFallback", () => ({
createSandboxWithFallback: (...args: unknown[]) => mockCreateSandboxWithFallback(...args),
}));

vi.mock("@/lib/supabase/account_snapshots/selectAccountSnapshots", () => ({
selectAccountSnapshots: (...args: unknown[]) => mockSelectAccountSnapshots(...args),
vi.mock("@/lib/sandbox/getValidSnapshotId", () => ({
getValidSnapshotId: (...args: unknown[]) => mockGetValidSnapshotId(...args),
}));

vi.mock("@/lib/supabase/account_sandboxes/insertAccountSandbox", () => ({
Expand All @@ -28,43 +28,40 @@ describe("createSandboxFromSnapshot", () => {

beforeEach(() => {
vi.clearAllMocks();
mockCreateSandbox.mockResolvedValue({
mockCreateSandboxWithFallback.mockResolvedValue({
sandbox: mockSandbox,
response: {
sandboxId: "sbx_new",
sandboxStatus: "running",
timeout: 1800000,
createdAt: "2024-01-01T00:00:00.000Z",
},
fromSnapshot: false,
});
mockInsertAccountSandbox.mockResolvedValue({
data: { account_id: "acc_1", sandbox_id: "sbx_new" },
error: null,
});
});

it("creates from snapshot when available", async () => {
mockSelectAccountSnapshots.mockResolvedValue([
{ snapshot_id: "snap_abc", account_id: "acc_1" },
]);
it("passes snapshotId to createSandboxWithFallback", async () => {
mockGetValidSnapshotId.mockResolvedValue("snap_abc");

await createSandboxFromSnapshot("acc_1");

expect(mockCreateSandbox).toHaveBeenCalledWith({
source: { type: "snapshot", snapshotId: "snap_abc" },
});
expect(mockCreateSandboxWithFallback).toHaveBeenCalledWith("snap_abc");
});

it("creates fresh sandbox when no snapshot exists", async () => {
mockSelectAccountSnapshots.mockResolvedValue([]);
it("passes undefined when no snapshot exists", async () => {
mockGetValidSnapshotId.mockResolvedValue(undefined);

await createSandboxFromSnapshot("acc_1");

expect(mockCreateSandbox).toHaveBeenCalledWith({});
expect(mockCreateSandboxWithFallback).toHaveBeenCalledWith(undefined);
});

it("inserts account_sandbox record", async () => {
mockSelectAccountSnapshots.mockResolvedValue([]);
mockGetValidSnapshotId.mockResolvedValue(undefined);

await createSandboxFromSnapshot("acc_1");

Expand All @@ -74,18 +71,53 @@ describe("createSandboxFromSnapshot", () => {
});
});

it("returns { sandbox, fromSnapshot: true } when snapshot exists", async () => {
mockSelectAccountSnapshots.mockResolvedValue([
{ snapshot_id: "snap_abc", account_id: "acc_1" },
]);
it("returns { sandbox, fromSnapshot: true } when snapshot used", async () => {
mockGetValidSnapshotId.mockResolvedValue("snap_abc");
mockCreateSandboxWithFallback.mockResolvedValue({
sandbox: mockSandbox,
response: {
sandboxId: "sbx_new",
sandboxStatus: "running",
timeout: 1800000,
createdAt: "2024-01-01T00:00:00.000Z",
},
fromSnapshot: true,
});

const result = await createSandboxFromSnapshot("acc_1");

expect(result).toEqual({ sandbox: mockSandbox, fromSnapshot: true });
});

it("returns { sandbox, fromSnapshot: false } when no snapshot", async () => {
mockSelectAccountSnapshots.mockResolvedValue([]);
mockGetValidSnapshotId.mockResolvedValue(undefined);

const result = await createSandboxFromSnapshot("acc_1");

expect(result).toEqual({ sandbox: mockSandbox, fromSnapshot: false });
});

it("returns { sandbox, fromSnapshot: false } for expired snapshot", async () => {
mockGetValidSnapshotId.mockResolvedValue(undefined);

const result = await createSandboxFromSnapshot("acc_1");

expect(mockCreateSandboxWithFallback).toHaveBeenCalledWith(undefined);
expect(result).toEqual({ sandbox: mockSandbox, fromSnapshot: false });
});

it("returns { sandbox, fromSnapshot: false } when snapshot creation fails", async () => {
mockGetValidSnapshotId.mockResolvedValue("snap_bad");
mockCreateSandboxWithFallback.mockResolvedValue({
sandbox: mockSandbox,
response: {
sandboxId: "sbx_new",
sandboxStatus: "running",
timeout: 1800000,
createdAt: "2024-01-01T00:00:00.000Z",
},
fromSnapshot: false,
});

const result = await createSandboxFromSnapshot("acc_1");

Expand Down
Loading
Loading