summaryrefslogtreecommitdiffhomepage
path: root/src/features/workspaces/logic/create.test.ts
blob: 278bcd69cdc339dddd3bda10b5d11bb4d767ad0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import type { Workspace } from "@dispatch/wire";
import { describe, expect, it } from "vitest";
import {
  archivedCreateMessage,
  checkFailedCreateMessage,
  gateCreate,
  type WorkspaceLookup,
} from "./create";

function workspace(overrides: Partial<Workspace> = {}): Workspace {
  return {
    id: "old",
    title: "Old",
    defaultCwd: null,
    defaultComputerId: null,
    starred: false,
    archived: false,
    createdAt: 1,
    lastActivityAt: 2,
    ...overrides,
  };
}

describe("gateCreate", () => {
  it("blocks a slug whose workspace is archived, with the can't-create message", () => {
    const lookup: WorkspaceLookup = { kind: "found", workspace: workspace({ archived: true }) };
    const gate = gateCreate("old", lookup);
    expect(gate).toEqual({ ok: false, error: archivedCreateMessage("old") });
    expect(gate.ok === false && gate.error).toContain(
      'A workspace named "old" already exists and is archived — you can\'t create it.',
    );
  });

  it("proceeds for an existing ACTIVE workspace (ensure is idempotent)", () => {
    const lookup: WorkspaceLookup = { kind: "found", workspace: workspace({ archived: false }) };
    expect(gateCreate("old", lookup)).toEqual({ ok: true });
  });

  it("proceeds for a missing slug", () => {
    expect(gateCreate("fresh", { kind: "missing" })).toEqual({ ok: true });
  });

  it("blocks when the existence probe failed (never guesses)", () => {
    const gate = gateCreate("maybe", { kind: "failed", error: "db down" });
    expect(gate).toEqual({ ok: false, error: checkFailedCreateMessage("maybe", "db down") });
    expect(gate.ok === false && gate.error).toContain(
      'Couldn\'t check whether a workspace named "maybe"',
    );
  });
});