Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

9.6 KB
Raw Blame
import { describe, expect, it } from "vitest";
import { dataEngineSpillPath, ephemeralWorkspaceTokenPath, excludedWorkspaceAuthPath, portableWorkspaceConfig, redownloadedOrgLibraryPath, restoredWorkspaceConfig } from "./workspace-backup-policy.ts";

describe("workspace backup data boundary", () => {
  it("exports ordinary settings only and retains destination connection sections unchanged", () => {
    const connections = {
      xai: { key: "xai-secret", url: "https://source.example" },
      anthropic: { key: "claude-secret", url: "https://claude.example" },
      openaiCompat: { key: "api-secret", url: "https://api.example", model: "model", provider: "provider" },
      composio: { apiKey: "composio-secret", userId: "source-account", sessionId: "source-session" },
      box: { token: "boat-secret" }, opencodeGo: { apiKey: "go-secret" },
      tts: { key: "voice-secret", fishKey: "fish-secret", voice: "source-voice", provider: "fish" },
      imageGen: { key: "image-secret", customApiKey: "custom-secret", customUrl: "https://images.example/v1", provider: "custom" },
      instances: { source: { driver: "claudeAgent", environment: { ORDINARY_NAME: "env-secret" }, config: { unknown: "driver-secret" } } },
      mcpServers: { source: { command: "mcp", args: ["arg-secret"], env: { ORDINARY: "env-secret" }, headers: { Authorization: "Bearer header-secret" } } },
      futureConnection: { unrecognizedField: "future-secret" }, apiKey: "legacy-secret",
      defaultModelSelection: { instanceId: "source", model: "source" },
      signIn: { admins: ["[email protected]"] }, customDomain: "source.example", vps: { sshAlias: "source" },
      cliStartup: { access: "public-url", publicUrl: "https://source.example" },
    };
    const portable = portableWorkspaceConfig({ ...connections, language: "ja", features: { browser: true }, profile: { name: "Example" } });
    expect(portable).toEqual({ profile: { name: "Example" }, language: "ja", features: { browser: true } });
    const restored = restoredWorkspaceConfig(portable, { ...connections, language: "en", budgets: { monthlyUsd: 12 } });
    expect(restored).toEqual({ ...connections, ...portable });
    expect(restored.instances).toBe(connections.instances);
  });

  it("leaves the helper model with the destination: a restored backup never names one and never clears the destination's", () => {
    // it names one of this machine's provider instances, and switching it on sends bots' text to that provider
    const portable = portableWorkspaceConfig({ helper: { instanceId: "claude" }, language: "ja" });
    expect(portable).toEqual({ language: "ja" });
    expect(restoredWorkspaceConfig(portable, {})).not.toHaveProperty("helper");
    expect(restoredWorkspaceConfig(portable, { helper: { instanceId: "openrouter" } }).helper).toEqual({ instanceId: "openrouter" });
  });

  it("carries the follow-up title preference and keeps the destination's provider selection", () => {
    const source = { instanceId: "anthropic", model: "source-sonnet" };
    const destination = { instanceId: "anthropic", model: "destination-sonnet" };
    const portable = portableWorkspaceConfig({ threadTitles: { model: source, followUp: true } });
    expect(portable).toEqual({ threadTitles: { followUp: true } });
    expect(restoredWorkspaceConfig(portable, {}).threadTitles).toEqual({ followUp: true });
    expect(restoredWorkspaceConfig(portable, { threadTitles: { model: destination, followUp: false } }).threadTitles)
      .toEqual({ model: destination, followUp: true });
    expect(restoredWorkspaceConfig({}, { threadTitles: { followUp: true } })).not.toHaveProperty("threadTitles");
  });

  it("keeps the destination's own computer-sharing switch, whatever the backup says", () => {
    const portable = portableWorkspaceConfig({ features: { browser: true, sharedComputers: true } });
    expect(restoredWorkspaceConfig(portable, { features: { sharedComputers: false } }).features).toEqual({ browser: true, sharedComputers: false });
    expect(restoredWorkspaceConfig(portable, {}).features).toEqual({ browser: true });
    expect(restoredWorkspaceConfig(portableWorkspaceConfig({ features: { sharedComputers: true } }), {})).not.toHaveProperty("features");
    expect(restoredWorkspaceConfig({}, { features: { sharedComputers: true, browser: true } }).features).toEqual({ sharedComputers: true });
  });

  it.each([
    "providers/account/.credentials.json", "providers/antigravity/profile/antigravity-acp/acp_token.json",
    "workspace-credentials.json", "browser-engine-key", "caddy/data/certificates/private.key",
    "external-runtimes.json", "external-runtimes.json.123.tmp",
    "command-allowlist.json", "command-allowlist.json.123.05a7b3e0-1234.tmp",
    "chrome-profile/Default/Cookies", ".agent-browser/auth/site.json",
    "vm-home/.browser-profiles/chrome/Cookies", "vm-homes/abc123/.browser-profiles/chromium/Local State",
    "config.json.123.tmp", "config.json.123.05a7b3e0-1234.tmp", "sessions.json.456.tmp",
    "webhooks.json.123.05a7b3e0-1234.tmp", "browser-engine-key.12.05a7b3e0-1234.tmp",
  ])("excludes known owned authentication path %s", (path) => {
    expect(excludedWorkspaceAuthPath(path)).toBe(true);
  });

  it("never exports or restores saved website logins or their key, or the key sealing webhook secrets", () => {
    for (const path of ["logins.json.enc", "member-secrets.json.enc", "member-secrets.json.enc.4242.05a7b3e0-1234-4abc-8def-0123456789ab.tmp", "logins-key", "logins.json.enc.4242.05a7b3e0-1234-4abc-8def-0123456789ab.tmp", "logins-key.12.05a7b3e0-1234.tmp", "webhook-key", "webhook-key.12.05a7b3e0-1234.tmp"]) {
      expect(excludedWorkspaceAuthPath(path)).toBe(true);
    }
    for (const path of ["logins.json", "logins.md", "workspaces/bot/logins.json.enc", "attachments/logins-key"]) expect(excludedWorkspaceAuthPath(path)).toBe(false);
  });

  it("never exports a GitHub sign-in's folder, whose config holds its token", () => {
    for (const path of ["github", "github/owner/config/hosts.yml", "github/p_abcdefghijklmnopqrstuv/config/hosts.yml", "github/owner/home/.gitconfig"]) {
      expect(excludedWorkspaceAuthPath(path)).toBe(true);
    }
    for (const path of ["workspaces/bot/github/notes.md", "githubber/x", "task-workspaces/b/t/github"]) expect(excludedWorkspaceAuthPath(path)).toBe(false);
  });

  it("never exports or restores a Cloud home's connected-apps installation", () => {
    for (const path of ["connected-apps-installation.json", "connected-apps-installation.json.4242.05a7b3e0-1234-4abc-8def-0123456789ab.tmp"]) {
      expect(excludedWorkspaceAuthPath(path)).toBe(true);
    }
    for (const path of ["connected-apps-installation.json.md", "workspaces/bot/connected-apps-installation.json"]) expect(excludedWorkspaceAuthPath(path)).toBe(false);
  });

  it("never exports or restores MCP sign-in tokens", () => {
    for (const path of ["mcp-oauth.json", "mcp-oauth.json.4242.05a7b3e0-1234-4abc-8def-0123456789ab.tmp"]) expect(excludedWorkspaceAuthPath(path)).toBe(true);
    for (const path of ["mcp-oauth.json.md", "workspaces/bot/mcp-oauth.json"]) expect(excludedWorkspaceAuthPath(path)).toBe(false);
  });

  it("never exports or restores the Bot API keys saved for teammates on other servers", () => {
    for (const path of ["remote-teammates.json", "remote-teammates.json.4242.05a7b3e0-1234-4abc-8def-0123456789ab.tmp"]) expect(excludedWorkspaceAuthPath(path)).toBe(true);
    for (const path of ["remote-teammates.json.md", "remote-teammate-work.json", "workspaces/bot/remote-teammates.json"]) expect(excludedWorkspaceAuthPath(path)).toBe(false);
  });

  it("never exports the per-turn hook token directory, and only that directory", () => {
    for (const path of ["hook-tokens", "hook-tokens/0123456789abcdef01234567.token", "sessions.json.open"]) expect(ephemeralWorkspaceTokenPath(path)).toBe(true);
    for (const path of ["hook-tokens.md", "workspaces/bot/hook-tokens/notes.md", "attachments/api.token", "sessions.json.opened", "workspaces/bot/sessions.json.open"]) expect(ephemeralWorkspaceTokenPath(path)).toBe(false);
  });

  it("leaves out only the Organization library files Electron main downloads again", () => {
    for (const path of ["org-library/catalog.json", "org-library/catalog.json.05a7b3e0-1234-4abc-8def-0123456789ab.tmp", "org-library/blobs",
      `org-library/blobs/${"a".repeat(64)}.json`, `org-library/blobs/${"a".repeat(64)}.json.05a7b3e0-1234-4abc-8def-0123456789ab.tmp`]) {
      expect(redownloadedOrgLibraryPath(path)).toBe(true);
    }
    for (const path of ["org-library", "org-library/state.json", "org-library/presets.json", "org-library/catalog.json.bak", "org-library/blobs.md",
      "workspaces/bot/org-library/catalog.json", "attachments/org-library/blobs/x.json", "catalog.json", "blobs/x.json"]) {
      expect(redownloadedOrgLibraryPath(path)).toBe(false);
    }
  });

  it("leaves out a bot's DuckDB spill directory, and nothing else in its folder: its Data tables go", () => {
    for (const path of ["bots/b1/data-tmp", "bots/b1/data-tmp/duckdb_temp_storage-0.tmp"]) {
      expect(dataEngineSpillPath(path)).toBe(true);
    }
    for (const path of ["bots/b1/data.duckdb", "bots/b1/data.duckdb.wal", "bots/b1/SOUL.md", "bots/b1/sheet.json", "bots/b1/data.duckdb.bak", "bots/b1/data-tmp.zip", "bots/data.duckdb", "data.duckdb",
      "workspaces/bot/data.duckdb", "bots/b1/exports/data.duckdb", "attachments/data-tmp/x"]) {
      expect(dataEngineSpillPath(path)).toBe(false);
    }
  });

  it.each(["attachments/key.txt", "workspaces/bot/notes.md", "workspaces/bot/external-runtimes.json", "vm-home/project/config.json", "config.json", "webhooks.json"])("does not redact arbitrary user file %s", (path) => {
    expect(excludedWorkspaceAuthPath(path)).toBe(false);
  });
});