Open History to see the last commit
1import { describe, expect, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import * as Sink from "effect/Sink";
5import * as Stream from "effect/Stream";
6import { ChildProcessSpawner } from "effect/unstable/process";
7
8import { discoverGrokSkills, parseGrokInspectSkills } from "./GrokSkills.ts";
9
10const inspectPayload = (skills: ReadonlyArray<unknown>) => JSON.stringify({ skills });
11
12describe("parseGrokInspectSkills", () => {
13 it("maps inspect entries onto provider skills, sorted by name", () => {
14 const skills = parseGrokInspectSkills(
15 inspectPayload([
16 {
17 name: "writing-docs",
18 description: "Write user docs.",
19 source: { type: "user", path: "/home/dev/.grok/skills/writing-docs/SKILL.md" },
20 userInvocable: true,
21 },
22 {
23 name: "deploy",
24 description: "Deploy the app.",
25 source: {
26 type: "plugin",
27 path: "/home/dev/.grok/installed-plugins/pkg/plug/skills/deploy/SKILL.md",
28 },
29 userInvocable: true,
30 },
31 ]),
32 );
33
34 expect(skills).toEqual([
35 {
36 name: "deploy",
37 description: "Deploy the app.",
38 path: "/home/dev/.grok/installed-plugins/pkg/plug/skills/deploy/SKILL.md",
39 scope: "plugin",
40 enabled: true,
41 },
42 {
43 name: "writing-docs",
44 description: "Write user docs.",
45 path: "/home/dev/.grok/skills/writing-docs/SKILL.md",
46 scope: "user",
47 enabled: true,
48 },
49 ]);
50 });
51
52 it("disables skills the CLI marks as not user-invocable", () => {
53 const skills = parseGrokInspectSkills(
54 inspectPayload([
55 {
56 name: "internal-helper",
57 source: { type: "bundled", path: "/opt/grok/bundled/skills/internal-helper/SKILL.md" },
58 userInvocable: false,
59 },
60 ]),
61 );
62
63 expect(skills).toEqual([
64 {
65 name: "internal-helper",
66 path: "/opt/grok/bundled/skills/internal-helper/SKILL.md",
67 scope: "bundled",
68 enabled: false,
69 },
70 ]);
71 });
72
73 it("skips entries without a name or a filesystem path", () => {
74 const skills = parseGrokInspectSkills(
75 inspectPayload([
76 { name: " ", source: { type: "user", path: "/tmp/skills/a/SKILL.md" } },
77 { name: "no-path", source: { type: "user" } },
78 { name: "no-source" },
79 "not-an-object",
80 { name: "kept", source: { type: "project", path: "/repo/.grok/skills/kept/SKILL.md" } },
81 ]),
82 );
83
84 expect(skills.map((skill) => skill.name)).toEqual(["kept"]);
85 });
86
87 it("returns an empty list for malformed or unexpected output", () => {
88 expect(parseGrokInspectSkills("not json")).toEqual([]);
89 expect(parseGrokInspectSkills("null")).toEqual([]);
90 expect(parseGrokInspectSkills(JSON.stringify({ skills: "nope" }))).toEqual([]);
91 expect(parseGrokInspectSkills(JSON.stringify({}))).toEqual([]);
92 });
93});
94
95describe("discoverGrokSkills", () => {
96 it.effect("spawns the inspect probe in the configured cwd", () => {
97 const spawnCwds: Array<string | undefined> = [];
98 const spawner = ChildProcessSpawner.make((command) => {
99 spawnCwds.push(command._tag === "StandardCommand" ? command.options.cwd : undefined);
100 return Effect.succeed(
101 ChildProcessSpawner.makeHandle({
102 pid: ChildProcessSpawner.ProcessId(1),
103 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)),
104 isRunning: Effect.succeed(false),
105 kill: () => Effect.void,
106 unref: Effect.succeed(Effect.void),
107 stdin: Sink.drain,
108 stdout: Stream.encodeText(
109 Stream.make(
110 inspectPayload([
111 {
112 name: "kept",
113 source: { type: "project", path: "/workspaces/demo/.grok/skills/kept/SKILL.md" },
114 },
115 ]),
116 ),
117 ),
118 stderr: Stream.empty,
119 all: Stream.empty,
120 getInputFd: () => Sink.drain,
121 getOutputFd: () => Stream.empty,
122 }),
123 );
124 });
125
126 return Effect.gen(function* () {
127 const skills = yield* discoverGrokSkills({ binaryPath: "grok" }, {}, "/workspaces/demo").pipe(
128 Effect.provide(Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner)),
129 );
130
131 expect(spawnCwds).toEqual(["/workspaces/demo"]);
132 expect(skills.map((skill) => skill.name)).toEqual(["kept"]);
133 });
134 });
135});