import { describe, it, expect } from "@effect/vitest";import { Effect } from "effect"; import { createExecutor, makeTestConfig } from "@executor/sdk"; import { graphqlPlugin } from "./plugin";import type { IntrospectionResult } from "./introspect"; // ---------------------------------------------------------------------------// Mock introspection response// --------------------------------------------------------------------------- const introspectionResult: IntrospectionResult = { __schema: { queryType: { name: "Query" }, mutationType: { name: "Mutation" }, types: [ { kind: "OBJECT", name: "Query", description: null, fields: [ { name: "hello", description: "Say hello", args: [ { name: "name", description: null, type: { kind: "SCALAR", name: "String", ofType: null }, defaultValue: null, }, ], type: { kind: "SCALAR", name: "String", ofType: null }, }, ], inputFields: null, enumValues: null, }, { kind: "OBJECT", name: "Mutation", description: null, fields: [ { name: "setGreeting", description: "Set greeting message", args: [ { name: "message", description: null, type: { kind: "NON_NULL", name: null, ofType: { kind: "SCALAR", name: "String", ofType: null }, }, defaultValue: null, }, ], type: { kind: "SCALAR", name: "String", ofType: null }, }, ], inputFields: null, enumValues: null, }, { kind: "SCALAR", name: "String", description: null, fields: null, inputFields: null, enumValues: null, }, ], },}; const introspectionJson = JSON.stringify({ data: introspectionResult }); describe("graphqlPlugin", () => { it.effect("registers tools from introspection JSON", () => Effect.gen(function* () { const executor = yield* createExecutor( makeTestConfig({ plugins: [graphqlPlugin()] as const }), ); const result = yield* executor.graphql.addSource({ endpoint: "http://localhost:4000/graphql", introspectionJson, namespace: "test_api", }); expect(result.toolCount).toBe(2); const tools = yield* executor.tools.list(); const ids = tools.map((t) => t.id); expect(ids).toContain("test_api.query.hello"); expect(ids).toContain("test_api.mutation.setGreeting"); // static control tool also present expect(ids).toContain("graphql.addSource"); const queryTool = tools.find((t) => t.id === "test_api.query.hello"); expect(queryTool?.description).toBe("Say hello"); const mutationTool = tools.find( (t) => t.id === "test_api.mutation.setGreeting", ); expect(mutationTool?.description).toBe("Set greeting message"); }), ); it.effect("removes a source and its tools", () => Effect.gen(function* () { const executor = yield* createExecutor( makeTestConfig({ plugins: [graphqlPlugin()] as const }), ); yield* executor.graphql.addSource({ endpoint: "http://localhost:4000/graphql", introspectionJson, namespace: "removable", }); let tools = yield* executor.tools.list(); expect( tools.filter((t) => t.sourceId === "removable").length, ).toBe(2); yield* executor.graphql.removeSource("removable"); tools = yield* executor.tools.list(); expect( tools.filter((t) => t.sourceId === "removable").length, ).toBe(0); const source = yield* executor.graphql.getSource("removable"); expect(source).toBeNull(); }), ); it.effect("lists sources with the static control source", () => Effect.gen(function* () { const executor = yield* createExecutor( makeTestConfig({ plugins: [graphqlPlugin()] as const }), ); yield* executor.graphql.addSource({ endpoint: "http://localhost:4000/graphql", introspectionJson, namespace: "my_gql", }); const sources = yield* executor.sources.list(); const dynamic = sources.find((s) => s.id === "my_gql"); expect(dynamic).toBeDefined(); expect(dynamic!.kind).toBe("graphql"); expect(dynamic!.canRemove).toBe(true); expect(dynamic!.canEdit).toBe(true); expect(dynamic!.runtime).toBe(false); const control = sources.find((s) => s.id === "graphql"); expect(control).toBeDefined(); expect(control!.runtime).toBe(true); }), ); it.effect("mutations require approval via resolveAnnotations", () => Effect.gen(function* () { const executor = yield* createExecutor( makeTestConfig({ plugins: [graphqlPlugin()] as const }), ); yield* executor.graphql.addSource({ endpoint: "http://localhost:4000/graphql", introspectionJson, namespace: "approval_test", }); const tools = yield* executor.tools.list(); const mutationTool = tools.find( (t) => t.id === "approval_test.mutation.setGreeting", ); expect(mutationTool).toBeDefined(); expect(mutationTool!.annotations?.requiresApproval).toBe(true); expect(mutationTool!.annotations?.approvalDescription).toBe( "mutation setGreeting", ); const queryTool = tools.find( (t) => t.id === "approval_test.query.hello", ); expect(queryTool).toBeDefined(); expect(queryTool!.annotations?.requiresApproval).toBeFalsy(); }), ); it.effect("updateSource patches endpoint/headers without re-registering", () => Effect.gen(function* () { const executor = yield* createExecutor( makeTestConfig({ plugins: [graphqlPlugin()] as const }), ); yield* executor.graphql.addSource({ endpoint: "http://localhost:4000/graphql", introspectionJson, namespace: "patched", }); yield* executor.graphql.updateSource("patched", { endpoint: "http://localhost:5000/graphql", headers: { "x-custom": "abc" }, }); const source = yield* executor.graphql.getSource("patched"); expect(source?.endpoint).toBe("http://localhost:5000/graphql"); expect(source?.headers).toEqual({ "x-custom": "abc" }); // Tools still present (no re-register happened, but they were // already there from addSource and haven't been removed). const tools = yield* executor.tools.list(); expect( tools.filter((t) => t.sourceId === "patched").length, ).toBe(2); }), ); it.effect("static graphql.addSource delegates to extension", () => Effect.gen(function* () { const executor = yield* createExecutor( makeTestConfig({ plugins: [graphqlPlugin()] as const }), ); const result = yield* executor.tools.invoke("graphql.addSource", { endpoint: "http://localhost:4000/graphql", introspectionJson, namespace: "via_static", }); expect(result).toEqual({ toolCount: 2 }); const tools = yield* executor.tools.list(); expect( tools.filter((t) => t.sourceId === "via_static").length, ).toBe(2); }), );});
import { describe, it, expect } from "@effect/vitest";
import { Effect } from "effect";
import { createExecutor, makeTestConfig } from "@executor/sdk";
import { graphqlPlugin } from "./plugin";
import type { IntrospectionResult } from "./introspect";
// ---------------------------------------------------------------------------
// Mock introspection response
// ---------------------------------------------------------------------------
const introspectionResult: IntrospectionResult = {
__schema: {
queryType: { name: "Query" },
mutationType: { name: "Mutation" },
types: [
{
kind: "OBJECT",
name: "Query",
description: null,
fields: [
{
name: "hello",
description: "Say hello",
args: [
{
name: "name",
description: null,
type: { kind: "SCALAR", name: "String", ofType: null },
defaultValue: null,
},
],
type: { kind: "SCALAR", name: "String", ofType: null },
},
],
inputFields: null,
enumValues: null,
},
{
kind: "OBJECT",
name: "Mutation",
description: null,
fields: [
{
name: "setGreeting",
description: "Set greeting message",
args: [
{
name: "message",
description: null,
type: {
kind: "NON_NULL",
name: null,
ofType: { kind: "SCALAR", name: "String", ofType: null },
},
defaultValue: null,
},
],
type: { kind: "SCALAR", name: "String", ofType: null },
},
],
inputFields: null,
enumValues: null,
},
{
kind: "SCALAR",
name: "String",
description: null,
fields: null,
inputFields: null,
enumValues: null,
},
],
},
};
const introspectionJson = JSON.stringify({ data: introspectionResult });
describe("graphqlPlugin", () => {
it.effect("registers tools from introspection JSON", () =>
Effect.gen(function* () {
const executor = yield* createExecutor(
makeTestConfig({ plugins: [graphqlPlugin()] as const }),
);
const result = yield* executor.graphql.addSource({
endpoint: "http://localhost:4000/graphql",
introspectionJson,
namespace: "test_api",
});
expect(result.toolCount).toBe(2);
const tools = yield* executor.tools.list();
const ids = tools.map((t) => t.id);
expect(ids).toContain("test_api.query.hello");
expect(ids).toContain("test_api.mutation.setGreeting");
// static control tool also present
expect(ids).toContain("graphql.addSource");
const queryTool = tools.find((t) => t.id === "test_api.query.hello");
expect(queryTool?.description).toBe("Say hello");
const mutationTool = tools.find(
(t) => t.id === "test_api.mutation.setGreeting",
);
expect(mutationTool?.description).toBe("Set greeting message");
}),
);
it.effect("removes a source and its tools", () =>
Effect.gen(function* () {
const executor = yield* createExecutor(
makeTestConfig({ plugins: [graphqlPlugin()] as const }),
);
yield* executor.graphql.addSource({
endpoint: "http://localhost:4000/graphql",
introspectionJson,
namespace: "removable",
});
let tools = yield* executor.tools.list();
expect(
tools.filter((t) => t.sourceId === "removable").length,
).toBe(2);
yield* executor.graphql.removeSource("removable");
tools = yield* executor.tools.list();
expect(
tools.filter((t) => t.sourceId === "removable").length,
).toBe(0);
const source = yield* executor.graphql.getSource("removable");
expect(source).toBeNull();
}),
);
it.effect("lists sources with the static control source", () =>
Effect.gen(function* () {
const executor = yield* createExecutor(
makeTestConfig({ plugins: [graphqlPlugin()] as const }),
);
yield* executor.graphql.addSource({
endpoint: "http://localhost:4000/graphql",
introspectionJson,
namespace: "my_gql",
});
const sources = yield* executor.sources.list();
const dynamic = sources.find((s) => s.id === "my_gql");
expect(dynamic).toBeDefined();
expect(dynamic!.kind).toBe("graphql");
expect(dynamic!.canRemove).toBe(true);
expect(dynamic!.canEdit).toBe(true);
expect(dynamic!.runtime).toBe(false);
const control = sources.find((s) => s.id === "graphql");
expect(control).toBeDefined();
expect(control!.runtime).toBe(true);
}),
);
it.effect("mutations require approval via resolveAnnotations", () =>
Effect.gen(function* () {
const executor = yield* createExecutor(
makeTestConfig({ plugins: [graphqlPlugin()] as const }),
);
yield* executor.graphql.addSource({
endpoint: "http://localhost:4000/graphql",
introspectionJson,
namespace: "approval_test",
});
const tools = yield* executor.tools.list();
const mutationTool = tools.find(
(t) => t.id === "approval_test.mutation.setGreeting",
);
expect(mutationTool).toBeDefined();
expect(mutationTool!.annotations?.requiresApproval).toBe(true);
expect(mutationTool!.annotations?.approvalDescription).toBe(
"mutation setGreeting",
);
const queryTool = tools.find(
(t) => t.id === "approval_test.query.hello",
);
expect(queryTool).toBeDefined();
expect(queryTool!.annotations?.requiresApproval).toBeFalsy();
}),
);
it.effect("updateSource patches endpoint/headers without re-registering", () =>
Effect.gen(function* () {
const executor = yield* createExecutor(
makeTestConfig({ plugins: [graphqlPlugin()] as const }),
);
yield* executor.graphql.addSource({
endpoint: "http://localhost:4000/graphql",
introspectionJson,
namespace: "patched",
});
yield* executor.graphql.updateSource("patched", {
endpoint: "http://localhost:5000/graphql",
headers: { "x-custom": "abc" },
});
const source = yield* executor.graphql.getSource("patched");
expect(source?.endpoint).toBe("http://localhost:5000/graphql");
expect(source?.headers).toEqual({ "x-custom": "abc" });
// Tools still present (no re-register happened, but they were
// already there from addSource and haven't been removed).
const tools = yield* executor.tools.list();
expect(
tools.filter((t) => t.sourceId === "patched").length,
).toBe(2);
}),
);
it.effect("static graphql.addSource delegates to extension", () =>
Effect.gen(function* () {
const executor = yield* createExecutor(
makeTestConfig({ plugins: [graphqlPlugin()] as const }),
);
const result = yield* executor.tools.invoke("graphql.addSource", {
endpoint: "http://localhost:4000/graphql",
introspectionJson,
namespace: "via_static",
});
expect(result).toEqual({ toolCount: 2 });
const tools = yield* executor.tools.list();
expect(
tools.filter((t) => t.sourceId === "via_static").length,
).toBe(2);
}),
);
});