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.

4.1 KB
Raw Blame
import * as NodeFS from "node:fs";
import * as NodeOS from "node:os";
import * as NodePath from "node:path";

import * as NodeServices from "@effect/platform-node/NodeServices";
import { describe, expect, it } from "@effect/vitest";
import * as Effect from "effect/Effect";
import * as Path from "effect/Path";

import { subscriptionRuntimeEnvironment } from "../../subscription-auth/runtime.ts";
import { SubscriptionAuthService } from "../../subscription-auth/service.ts";
import {
  claudeSignedOutMessage,
  claudeCapabilitiesCacheKey,
  claudeContinuationGroupKey,
  claudeEnvironmentForConfig,
  resolveClaudeHomePath,
} from "./ClaudeHome.ts";

it.layer(NodeServices.layer)("ClaudeHome", (it) => {
  describe("Claude home resolution", () => {
    it.effect("uses the process home when no Claude home override is configured", () =>
      Effect.gen(function* () {
        const path = yield* Path.Path;
        const resolved = path.resolve(NodeOS.homedir());

        expect(yield* resolveClaudeHomePath({ homePath: "" })).toBe(resolved);
        expect(yield* claudeEnvironmentForConfig({ homePath: "" })).toBe(process.env);
      }),
    );

    it.effect("resolves configured Claude HOME and stamps continuation/cache keys with it", () =>
      Effect.gen(function* () {
        const path = yield* Path.Path;
        const homePath = "~/.claude-work";
        const resolved = path.resolve(NodeOS.homedir(), ".claude-work");

        expect(yield* resolveClaudeHomePath({ homePath })).toBe(resolved);
        expect((yield* claudeEnvironmentForConfig({ homePath })).CLAUDE_CONFIG_DIR).toBe(resolved);
        expect(yield* claudeContinuationGroupKey({ homePath })).toBe(`claude:home:${resolved}`);
        expect(yield* claudeCapabilitiesCacheKey({ binaryPath: "claude", homePath })).toBe(
          `claude\0${resolved}\0`,
        );
      }),
    );

    it("points the signed-out hint at the configured Claude home", () => {
      expect(claudeSignedOutMessage({ configDir: undefined, cwd: "/synthetic" })).toContain(
        "run `claude auth login`",
      );
      const configDir = "/synthetic/Claude work's $literal";
      const message = claudeSignedOutMessage({ configDir, cwd: "/synthetic/project" });
      expect(message).toContain(`CLAUDE_CONFIG_DIR set to "${configDir}"`);
      expect(message).not.toContain("CLAUDE_CONFIG_DIR=");
      expect(message).toContain("then start a new thread");
    });

    it.effect("marks CLAUDE_CONFIG_DIR explicit so saved API keys do not replace the account", () =>
      Effect.gen(function* () {
        const secretsDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "akeru-claude-home-"));

        try {
          const auth = yield* SubscriptionAuthService.forSecretsDir(secretsDir);

          const login = yield* Effect.promise(() =>
            auth.startLogin("anthropic", { authMode: "api-key" }),
          );

          yield* Effect.promise(() => auth.completeLogin(login.loginId, "provider-wide-key"));

          const environment = yield* claudeEnvironmentForConfig(
            { homePath: "~/.claude-work" },
            { CLAUDE_CODE_OAUTH_TOKEN: "native-oauth" },
          );

          expect(yield* subscriptionRuntimeEnvironment(secretsDir, "anthropic", environment)).toBe(
            environment,
          );
          expect(environment.ANTHROPIC_API_KEY).toBeUndefined();
        } finally {
          NodeFS.rmSync(secretsDir, { recursive: true, force: true });
        }
      }),
    );

    it.effect("separates capability probes by cwd", () =>
      Effect.gen(function* () {
        const config = { binaryPath: "claude", homePath: "" };
        const first = yield* claudeCapabilitiesCacheKey(config, "/repo-a");
        const second = yield* claudeCapabilitiesCacheKey(config, "/repo-b");
        expect(first).not.toBe(second);
      }),
    );

    it.effect("keeps continuation compatible across instances with the same Claude HOME", () =>
      Effect.gen(function* () {
        const path = yield* Path.Path;
        const resolved = path.resolve(NodeOS.homedir());

        expect(yield* claudeContinuationGroupKey({ homePath: "" })).toBe(`claude:home:${resolved}`);
      }),
    );
  });
});