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.

13.1 KB
Raw Blame
import * as NodeServices from "@effect/platform-node/NodeServices";
import { expect, it } from "@effect/vitest";
import * as NodeFS from "node:fs";
import * as NodePath from "node:path";
import { HostProcessPlatform } from "@akeru/shared/hostProcess";
import * as Effect from "effect/Effect";
import {
  packageManagedProviderMaintenanceResolver,
  resolveProviderMaintenanceCapabilitiesEffect,
} from "./providerMaintenance.ts";
import { makeproviderMaintenanceTestSupport } from "./test-support/providerMaintenance.ts";

const {
  driver,
  makeTempDir,
  isNativeTestCommandPath,
  packageToolUpdate,
  nativePackageToolUpdate,
  scopedPackageToolUpdate,
} = makeproviderMaintenanceTestSupport();

it.layer(NodeServices.layer)("providerMaintenance", (it) => {
  it.effect(
    "switches package-managed providers to vite-plus updates when the resolved binary lives in vite-plus global bin",
    () =>
      Effect.gen(function* () {
        const tempDir = yield* makeTempDir("t3-vite-plus-capabilities");
        const vitePlusBinDir = NodePath.join(tempDir, ".vite-plus", "bin");
        NodeFS.mkdirSync(vitePlusBinDir, { recursive: true });
        const packageToolPath = NodePath.join(vitePlusBinDir, "package-tool");
        NodeFS.writeFileSync(packageToolPath, "#!/bin/sh\n");
        NodeFS.chmodSync(packageToolPath, 0o755);

        const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(
          packageToolUpdate,
          {
            binaryPath: "package-tool",
            env: {
              PATH: vitePlusBinDir,
            },
          },
        ).pipe(Effect.provideService(HostProcessPlatform, "darwin"));

        expect(capabilities).toEqual({
          provider: driver("packageTool"),
          packageName: "@example/package-tool",
          update: {
            command: "vp i -g @example/package-tool",

            executable: "vp",

            args: ["i", "-g", "@example/package-tool"],

            lockKey: "vite-plus-global",
          },
        });
      }),
  );

  it.effect(
    "switches package-managed providers to bun updates when the resolved binary lives in bun's global bin",
    () =>
      Effect.gen(function* () {
        const tempDir = yield* makeTempDir("t3-bun-capabilities");
        const bunBinDir = NodePath.join(tempDir, ".bun", "bin");
        NodeFS.mkdirSync(bunBinDir, { recursive: true });
        NodeFS.writeFileSync(NodePath.join(bunBinDir, "native-package-tool.exe"), "MZ");

        const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(
          nativePackageToolUpdate,
          {
            binaryPath: "native-package-tool",
            env: {
              PATH: bunBinDir,
              PATHEXT: ".COM;.EXE;.BAT;.CMD",
            },
          },
        ).pipe(Effect.provideService(HostProcessPlatform, "win32"));

        expect(capabilities).toEqual({
          provider: driver("nativePackageTool"),
          packageName: "@example/native-package-tool",
          update: {
            command: "bun i -g @example/native-package-tool@latest",

            executable: "bun",

            args: ["i", "-g", "@example/native-package-tool@latest"],

            lockKey: "bun-global",
          },
        });
      }),
  );

  it.effect(
    "switches package-managed providers to pnpm updates when the resolved binary lives in pnpm's global bin",
    () =>
      Effect.gen(function* () {
        const tempDir = yield* makeTempDir("t3-pnpm-capabilities");
        const pnpmHomeDir = NodePath.join(tempDir, ".local", "share", "pnpm");
        NodeFS.mkdirSync(pnpmHomeDir, { recursive: true });
        const scopedPackageToolPath = NodePath.join(pnpmHomeDir, "scoped-package-tool");
        NodeFS.writeFileSync(scopedPackageToolPath, "#!/bin/sh\n");
        NodeFS.chmodSync(scopedPackageToolPath, 0o755);

        const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(
          scopedPackageToolUpdate,
          {
            binaryPath: "scoped-package-tool",
            env: {
              PATH: pnpmHomeDir,
            },
          },
        ).pipe(Effect.provideService(HostProcessPlatform, "darwin"));

        expect(capabilities).toEqual({
          provider: driver("scopedPackageTool"),
          packageName: "@example/scoped-package-tool",
          update: {
            command: "pnpm add -g @example/scoped-package-tool@latest",

            executable: "pnpm",

            args: ["add", "-g", "@example/scoped-package-tool@latest"],

            lockKey: "pnpm-global",
          },
        });
      }),
  );

  it("switches package-tool to Homebrew updates when the binary resolves through Homebrew", () => {
    expect(
      packageToolUpdate.resolve({
        binaryPath: "/opt/homebrew/bin/package-tool",
        env: {
          PATH: "",
        },
      }),
    ).toEqual({
      provider: driver("packageTool"),
      packageName: "@example/package-tool",
      update: {
        command: "brew upgrade package-tool",

        executable: "brew",

        args: ["upgrade", "package-tool"],

        lockKey: "homebrew",
      },
    });
  });

  it.effect(
    "switches native-package-tool to native updates when the binary resolves through the native installer",
    () =>
      Effect.gen(function* () {
        const tempDir = yield* makeTempDir("t3-native-package-tool-native-capabilities");
        const nativeBinDir = NodePath.join(tempDir, ".local", "bin");
        NodeFS.mkdirSync(nativeBinDir, { recursive: true });
        const nativePackageToolPath = NodePath.join(nativeBinDir, "native-package-tool");
        NodeFS.writeFileSync(nativePackageToolPath, "#!/bin/sh\n");
        NodeFS.chmodSync(nativePackageToolPath, 0o755);

        const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(
          nativePackageToolUpdate,
          {
            binaryPath: "native-package-tool",
            env: {
              PATH: nativeBinDir,
            },
          },
        ).pipe(Effect.provideService(HostProcessPlatform, "darwin"));

        expect(capabilities).toEqual({
          provider: driver("nativePackageTool"),
          packageName: "@example/native-package-tool",
          update: {
            command: "native-package-tool update",

            executable: "native-package-tool",

            args: ["update"],

            lockKey: "native-package-tool-native",
          },
        });
      }),
  );

  it.effect(
    "switches scoped-package-tool to native upgrades when the binary resolves through the standalone installer",
    () =>
      Effect.gen(function* () {
        const tempDir = yield* makeTempDir("t3-scoped-package-tool-native-capabilities");
        const nativeBinDir = NodePath.join(tempDir, ".scoped-package-tool", "bin");
        NodeFS.mkdirSync(nativeBinDir, { recursive: true });
        const scopedPackageToolPath = NodePath.join(nativeBinDir, "scoped-package-tool");
        NodeFS.writeFileSync(scopedPackageToolPath, "#!/bin/sh\n");
        NodeFS.chmodSync(scopedPackageToolPath, 0o755);

        const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(
          scopedPackageToolUpdate,
          {
            binaryPath: "scoped-package-tool",
            env: {
              PATH: nativeBinDir,
            },
          },
        ).pipe(Effect.provideService(HostProcessPlatform, "darwin"));

        expect(capabilities).toEqual({
          provider: driver("scopedPackageTool"),
          packageName: "@example/scoped-package-tool",
          update: {
            command: "scoped-package-tool upgrade",

            executable: "scoped-package-tool",

            args: ["upgrade"],

            lockKey: "scoped-package-tool-native",
          },
        });
      }),
  );

  it("switches native-package-tool to Homebrew updates when the binary resolves through Homebrew", () => {
    expect(
      nativePackageToolUpdate.resolve({
        binaryPath: "/opt/homebrew/bin/native-package-tool",
        env: {
          PATH: "",
        },
      }),
    ).toEqual({
      provider: driver("nativePackageTool"),
      packageName: "@example/native-package-tool",
      update: {
        command: "brew upgrade native-package-tool",

        executable: "brew",

        args: ["upgrade", "native-package-tool"],

        lockKey: "homebrew",
      },
    });
  });

  it("switches scoped-package-tool to Homebrew updates when the binary resolves through Homebrew", () => {
    expect(
      scopedPackageToolUpdate.resolve({
        binaryPath: "/opt/homebrew/bin/scoped-package-tool",
        env: {
          PATH: "",
        },
      }),
    ).toEqual({
      provider: driver("scopedPackageTool"),
      packageName: "@example/scoped-package-tool",
      update: {
        command: "brew upgrade example/tap/scoped-package-tool",

        executable: "brew",

        args: ["upgrade", "example/tap/scoped-package-tool"],

        lockKey: "homebrew",
      },
    });
  });

  it.effect("keeps npm updates for binaries symlinked into npm's global node_modules tree", () =>
    Effect.gen(function* () {
      const tempDir = yield* makeTempDir("t3-npm-capabilities");
      const binDir = NodePath.join(tempDir, "bin");

      const packageBinDir = NodePath.join(
        tempDir,
        "lib",
        "node_modules",
        "@example",
        "package-tool",
        "bin",
      );

      NodeFS.mkdirSync(binDir, { recursive: true });
      NodeFS.mkdirSync(packageBinDir, { recursive: true });
      const packageBinPath = NodePath.join(packageBinDir, "package-tool.js");
      const symlinkPath = NodePath.join(binDir, "package-tool");
      NodeFS.writeFileSync(packageBinPath, "#!/usr/bin/env node\n");
      NodeFS.chmodSync(packageBinPath, 0o755);
      NodeFS.symlinkSync(packageBinPath, symlinkPath);

      const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(packageToolUpdate, {
        binaryPath: symlinkPath,
        env: {
          PATH: "",
        },
      });

      expect(capabilities).toEqual({
        provider: driver("packageTool"),
        packageName: "@example/package-tool",
        update: {
          command:
            "npm install -g --allow-scripts=@example/package-tool @example/package-tool@latest",

          executable: "npm",

          args: [
            "install",
            "-g",
            "--allow-scripts=@example/package-tool",
            "@example/package-tool@latest",
          ],

          lockKey: "npm-global",
        },
      });
    }),
  );

  it.effect("uses Effect FileSystem realPath when detecting pnpm global symlinks", () =>
    Effect.gen(function* () {
      const tempDir = yield* makeTempDir("t3-pnpm-realpath-capabilities");
      const binDir = NodePath.join(tempDir, "bin");

      const packageBinDir = NodePath.join(
        tempDir,
        ".local",
        "share",
        "pnpm",
        "global",
        "5",
        "node_modules",
        "@example",
        "package-tool",
        "bin",
      );

      NodeFS.mkdirSync(binDir, { recursive: true });
      NodeFS.mkdirSync(packageBinDir, { recursive: true });
      const packageBinPath = NodePath.join(packageBinDir, "package-tool.js");
      const symlinkPath = NodePath.join(binDir, "package-tool");
      NodeFS.writeFileSync(packageBinPath, "#!/usr/bin/env node\n");
      NodeFS.chmodSync(packageBinPath, 0o755);
      NodeFS.symlinkSync(packageBinPath, symlinkPath);

      const capabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(packageToolUpdate, {
        binaryPath: symlinkPath,
        env: {
          PATH: "",
        },
      });

      expect(capabilities).toEqual({
        provider: driver("packageTool"),
        packageName: "@example/package-tool",
        update: {
          command: "pnpm add -g @example/package-tool@latest",

          executable: "pnpm",

          args: ["add", "-g", "@example/package-tool@latest"],

          lockKey: "pnpm-global",
        },
      });
    }),
  );

  it("allows the package's own install scripts in npm global updates", () => {
    const claudeUpdate = packageManagedProviderMaintenanceResolver({
      provider: driver("claudeAgent"),
      npmPackageName: "@anthropic-ai/claude-code",
      homebrewFormula: "claude-code",
      nativeUpdate: {
        executable: "claude",
        args: ["update"],
        lockKey: "claude-native",
        isCommandPath: isNativeTestCommandPath("/.local/bin/claude"),
      },
    });

    expect(claudeUpdate.resolve()).toEqual({
      provider: driver("claudeAgent"),
      packageName: "@anthropic-ai/claude-code",
      update: {
        command:
          "npm install -g --allow-scripts=@anthropic-ai/claude-code @anthropic-ai/claude-code@latest",

        executable: "npm",

        args: [
          "install",
          "-g",
          "--allow-scripts=@anthropic-ai/claude-code",
          "@anthropic-ai/claude-code@latest",
        ],

        lockKey: "npm-global",
      },
    });
  });

  it("disables one-click updates for explicit custom binary paths it cannot safely map", () => {
    expect(
      packageToolUpdate.resolve({
        binaryPath: "C:\\Tools\\package-tool\\package-tool.exe",
        env: {
          PATH: "",
          PATHEXT: ".COM;.EXE;.BAT;.CMD",
        },
      }),
    ).toEqual({
      provider: driver("packageTool"),
      packageName: "@example/package-tool",
      update: null,
    });
  });
});