Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

3.7 KB
Raw Blame
import { BearerConnectionTarget, PrimaryConnectionTarget } from "@akeru/client-runtime/connection";
import { EnvironmentId, ProjectId, type PortabilityImportPreview } from "@akeru/contracts";
import { describe, expect, it } from "vite-plus/test";

import { PORTABILITY_ARCHIVE_MAX_CHARS } from "@akeru/contracts";

import {
  canApplyPortabilityPreview,
  portabilityArchiveFileError,
  portabilityProjectPickerTarget,
  updatePortabilityProjectFolderMap,
} from "./PortabilitySettings.logic";

const ENVIRONMENT_ID = EnvironmentId.make("environment-1");

describe("portabilityProjectPickerTarget", () => {
  it("uses only native or desktop-local folder pickers", () => {
    expect(
      portabilityProjectPickerTarget(
        new PrimaryConnectionTarget({
          environmentId: ENVIRONMENT_ID,
          httpBaseUrl: "http://127.0.0.1:3773",
          label: "This device",
          wsBaseUrl: "ws://127.0.0.1:3773",
        }),
      ),
    ).toBeNull();
    expect(
      portabilityProjectPickerTarget(
        new BearerConnectionTarget({
          connectionId: "local:wsl:Ubuntu",
          environmentId: ENVIRONMENT_ID,
          label: "WSL",
        }),
      ),
    ).toBe("wsl:Ubuntu");
  });
});

const preview: PortabilityImportPreview = {
  snapshotSequence: 1,
  stateChecksum: "0".repeat(64),
  additions: [],
  changes: [],
  conflicts: [],
  missingProviders: [],
  skippedSecrets: [],
  projectFolders: [],
  unsupported: [],
};

describe("canApplyPortabilityPreview", () => {
  it("allows only previews with safe additions or changes", () => {
    expect(canApplyPortabilityPreview(preview)).toBe(false);
    expect(
      canApplyPortabilityPreview({
        ...preview,
        conflicts: [{ recordType: "bot", id: "bot-1", title: "Bot" }],
      }),
    ).toBe(false);
    expect(
      canApplyPortabilityPreview({
        ...preview,
        changes: [{ recordType: "mcp-server", id: "mcp-1", title: "MCP server" }],
      }),
    ).toBe(true);

    const partial = {
      ...preview,
      changes: [{ recordType: "mcp-server" as const, id: "mcp-1", title: "MCP server" }],
      projectFolders: [
        {
          projectId: ProjectId.make("project-1"),
          title: "Project",
          workspaceName: "project",
          destination: null,
        },
      ],
    };

    expect(canApplyPortabilityPreview(partial)).toBe(true);
    expect(
      canApplyPortabilityPreview({
        ...preview,
        additions: [{ recordType: "project", id: "project-1", title: "Project" }],
        projectFolders: [
          {
            projectId: ProjectId.make("project-1"),
            title: "Project",
            workspaceName: "project",
            destination: null,
          },
        ],
      }),
    ).toBe(true);
  });
});

describe("portabilityArchiveFileError", () => {
  it("rejects files larger than the archive limit before reading them", () => {
    expect(portabilityArchiveFileError(PORTABILITY_ARCHIVE_MAX_CHARS)).toBeNull();
    expect(portabilityArchiveFileError(PORTABILITY_ARCHIVE_MAX_CHARS + 1)).toBe(
      "Archives must be 20 MB or smaller.",
    );
  });
});

describe("updatePortabilityProjectFolderMap", () => {
  it("keeps prior folder picks and can clear one mapping", () => {
    const first = ProjectId.make("project-1");
    const second = ProjectId.make("project-2");

    const withBoth = updatePortabilityProjectFolderMap(
      updatePortabilityProjectFolderMap({}, first, " /tmp/first "),
      second,
      "/tmp/second",
    );

    expect(withBoth).toEqual({
      [first]: "/tmp/first",
      [second]: "/tmp/second",
    });
    expect(updatePortabilityProjectFolderMap(withBoth, first, "")).toEqual({
      [second]: "/tmp/second",
    });
  });
});