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.

5.5 KB
Raw Blame
import * as Predicate from "effect/Predicate";
import { describe, expect, it } from "@effect/vitest";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Sink from "effect/Sink";
import * as Stream from "effect/Stream";
import { ChildProcessSpawner } from "effect/unstable/process";

import { discoverGrokSkills, parseGrokInspectSkills } from "./GrokSkills.ts";

const inspectPayload = (skills: ReadonlyArray<unknown>) => JSON.stringify({ skills });

describe("parseGrokInspectSkills", () => {
  it("maps inspect entries onto provider skills, sorted by name", () => {
    const skills = parseGrokInspectSkills(
      inspectPayload([
        {
          name: "writing-docs",
          description: "Write user docs.",
          source: { type: "user", path: "/home/dev/.grok/skills/writing-docs/SKILL.md" },
          userInvocable: true,
        },
        {
          name: "deploy",
          description: "Deploy the app.",
          source: {
            type: "plugin",
            path: "/home/dev/.grok/installed-plugins/pkg/plug/skills/deploy/SKILL.md",
          },
          userInvocable: true,
        },
      ]),
    );

    expect(skills).toEqual([
      {
        name: "deploy",
        description: "Deploy the app.",
        path: "/home/dev/.grok/installed-plugins/pkg/plug/skills/deploy/SKILL.md",
        scope: "plugin",
        enabled: true,
      },
      {
        name: "writing-docs",
        description: "Write user docs.",
        path: "/home/dev/.grok/skills/writing-docs/SKILL.md",
        scope: "user",
        enabled: true,
      },
    ]);
  });

  it("carries the reported icon and omits it when absent", () => {
    const skills = parseGrokInspectSkills(
      inspectPayload([
        {
          name: "with-icon",
          description: "Has an icon.",
          icon: "wrench",
          source: { type: "user", path: "/home/dev/.grok/skills/with-icon/SKILL.md" },
          userInvocable: true,
        },
        {
          name: "no-icon",
          source: { type: "user", path: "/home/dev/.grok/skills/no-icon/SKILL.md" },
          userInvocable: true,
        },
      ]),
    );

    expect(skills.find((skill) => skill.name === "with-icon")?.icon).toBe("wrench");
    expect(skills.find((skill) => skill.name === "no-icon")?.icon).toBeUndefined();
  });

  it("disables skills the CLI marks as not user-invocable", () => {
    const skills = parseGrokInspectSkills(
      inspectPayload([
        {
          name: "internal-helper",
          source: { type: "bundled", path: "/opt/grok/bundled/skills/internal-helper/SKILL.md" },
          userInvocable: false,
        },
      ]),
    );

    expect(skills).toEqual([
      {
        name: "internal-helper",
        path: "/opt/grok/bundled/skills/internal-helper/SKILL.md",
        scope: "bundled",
        enabled: false,
      },
    ]);
  });

  it("skips entries without a name or a filesystem path", () => {
    const skills = parseGrokInspectSkills(
      inspectPayload([
        { name: "  ", source: { type: "user", path: "/tmp/skills/a/SKILL.md" } },
        { name: "no-path", source: { type: "user" } },
        { name: "no-source" },
        "not-an-object",
        { name: "kept", source: { type: "project", path: "/repo/.grok/skills/kept/SKILL.md" } },
      ]),
    );

    expect(skills.map((skill) => skill.name)).toEqual(["kept"]);
  });

  it("returns an empty list for malformed or unexpected output", () => {
    expect(parseGrokInspectSkills("not json")).toEqual([]);
    expect(parseGrokInspectSkills("null")).toEqual([]);
    expect(parseGrokInspectSkills(JSON.stringify({ skills: "nope" }))).toEqual([]);
    expect(parseGrokInspectSkills(JSON.stringify({}))).toEqual([]);
  });
});

describe("discoverGrokSkills", () => {
  it.effect("spawns in the configured cwd and rejects a failed probe", () => {
    const spawnCwds: Array<string | undefined> = [];
    let exitCode = 0;

    const spawner = ChildProcessSpawner.make((command) => {
      spawnCwds.push(
        Predicate.isTagged(command, "StandardCommand") ? command.options.cwd : undefined,
      );

      return Effect.succeed(
        ChildProcessSpawner.makeHandle({
          pid: ChildProcessSpawner.ProcessId(1),
          exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(exitCode)),
          isRunning: Effect.succeed(false),
          kill: () => Effect.void,
          unref: Effect.succeed(Effect.void),
          stdin: Sink.drain,
          stdout: Stream.encodeText(
            Stream.make(
              inspectPayload([
                {
                  name: "kept",
                  source: { type: "project", path: "/workspaces/demo/.grok/skills/kept/SKILL.md" },
                },
              ]),
            ),
          ),
          stderr: Stream.empty,
          all: Stream.empty,
          getInputFd: () => Sink.drain,
          getOutputFd: () => Stream.empty,
        }),
      );
    });

    return Effect.gen(function* () {
      const skills = yield* discoverGrokSkills({ binaryPath: "grok" }, {}, "/workspaces/demo").pipe(
        Effect.provide(Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner)),
      );

      expect(spawnCwds).toEqual(["/workspaces/demo"]);
      expect(skills.map((skill) => skill.name)).toEqual(["kept"]);

      exitCode = 1;

      const failed = yield* discoverGrokSkills({ binaryPath: "grok" }).pipe(
        Effect.result,
        Effect.provide(Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner)),
      );

      expect(failed._tag).toBe("Failure");
    });
  });
});