Implement analysis defaults module with default value resolution for CallStatus, NodeStatus, EdgeType, and node attributes
This commit is contained in:
@@ -1 +1,29 @@
|
||||
export {};
|
||||
import type { CallStatus, NodeStatus, EdgeType } from "../schema/enums.js";
|
||||
import type { OperationNodeAttrs } from "../schema/node.js";
|
||||
|
||||
export const defaultCallStatus: CallStatus = "pending";
|
||||
|
||||
export const defaultNodeStatus: NodeStatus = "idle";
|
||||
|
||||
export function defaultEdgeType(edgeType?: string): EdgeType {
|
||||
if (edgeType !== undefined && edgeType !== null) {
|
||||
return edgeType as EdgeType;
|
||||
}
|
||||
return "typed";
|
||||
}
|
||||
|
||||
export function resolveDefaultNodeAttrs(
|
||||
attrs: Partial<OperationNodeAttrs>,
|
||||
defaults: Partial<OperationNodeAttrs> = {},
|
||||
): OperationNodeAttrs {
|
||||
return {
|
||||
name: attrs.name ?? defaults.name ?? "",
|
||||
namespace: attrs.namespace ?? defaults.namespace ?? "",
|
||||
version: attrs.version ?? defaults.version ?? "",
|
||||
type: attrs.type ?? defaults.type ?? "query",
|
||||
inputSchema: attrs.inputSchema ?? defaults.inputSchema ?? {},
|
||||
outputSchema: attrs.outputSchema ?? defaults.outputSchema ?? {},
|
||||
description: attrs.description ?? defaults.description,
|
||||
tags: attrs.tags ?? defaults.tags,
|
||||
};
|
||||
}
|
||||
@@ -1 +1,6 @@
|
||||
export {};
|
||||
export {
|
||||
defaultCallStatus,
|
||||
defaultNodeStatus,
|
||||
defaultEdgeType,
|
||||
resolveDefaultNodeAttrs,
|
||||
} from "./defaults.js";
|
||||
119
test/analysis/defaults.test.ts
Normal file
119
test/analysis/defaults.test.ts
Normal file
@@ -0,0 +1,119 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import {
|
||||
defaultCallStatus,
|
||||
defaultNodeStatus,
|
||||
defaultEdgeType,
|
||||
resolveDefaultNodeAttrs,
|
||||
} from "../../src/analysis/defaults.js";
|
||||
|
||||
describe("analysis/defaults", () => {
|
||||
describe("defaultCallStatus", () => {
|
||||
it('is "pending"', () => {
|
||||
expect(defaultCallStatus).toBe("pending");
|
||||
});
|
||||
});
|
||||
|
||||
describe("defaultNodeStatus", () => {
|
||||
it('is "idle"', () => {
|
||||
expect(defaultNodeStatus).toBe("idle");
|
||||
});
|
||||
});
|
||||
|
||||
describe("defaultEdgeType", () => {
|
||||
it('returns "typed" when no argument provided', () => {
|
||||
expect(defaultEdgeType()).toBe("typed");
|
||||
});
|
||||
|
||||
it('returns "typed" when undefined provided', () => {
|
||||
expect(defaultEdgeType(undefined)).toBe("typed");
|
||||
});
|
||||
|
||||
it("returns the provided edge type", () => {
|
||||
expect(defaultEdgeType("triggered")).toBe("triggered");
|
||||
expect(defaultEdgeType("depends_on")).toBe("depends_on");
|
||||
expect(defaultEdgeType("typed")).toBe("typed");
|
||||
expect(defaultEdgeType("sequential")).toBe("sequential");
|
||||
expect(defaultEdgeType("conditional")).toBe("conditional");
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveDefaultNodeAttrs", () => {
|
||||
it("fills all required fields from defaults when attrs is empty", () => {
|
||||
const defaults = {
|
||||
name: "op",
|
||||
namespace: "ns",
|
||||
version: "1.0.0",
|
||||
type: "query" as const,
|
||||
inputSchema: { type: "object" },
|
||||
outputSchema: { type: "object" },
|
||||
};
|
||||
const result = resolveDefaultNodeAttrs({}, defaults);
|
||||
expect(result).toEqual({
|
||||
name: "op",
|
||||
namespace: "ns",
|
||||
version: "1.0.0",
|
||||
type: "query",
|
||||
inputSchema: { type: "object" },
|
||||
outputSchema: { type: "object" },
|
||||
description: undefined,
|
||||
tags: undefined,
|
||||
});
|
||||
});
|
||||
|
||||
it("merges partial attrs with defaults", () => {
|
||||
const defaults = {
|
||||
name: "op",
|
||||
namespace: "ns",
|
||||
version: "1.0.0",
|
||||
type: "query" as const,
|
||||
inputSchema: { type: "object" },
|
||||
outputSchema: { type: "object" },
|
||||
};
|
||||
const result = resolveDefaultNodeAttrs(
|
||||
{ name: "custom", namespace: "other" },
|
||||
defaults,
|
||||
);
|
||||
expect(result.name).toBe("custom");
|
||||
expect(result.namespace).toBe("other");
|
||||
expect(result.version).toBe("1.0.0");
|
||||
expect(result.type).toBe("query");
|
||||
});
|
||||
|
||||
it("uses built-in defaults when no defaults object provided", () => {
|
||||
const result = resolveDefaultNodeAttrs({ name: "myop" });
|
||||
expect(result).toEqual({
|
||||
name: "myop",
|
||||
namespace: "",
|
||||
version: "",
|
||||
type: "query",
|
||||
inputSchema: {},
|
||||
outputSchema: {},
|
||||
description: undefined,
|
||||
tags: undefined,
|
||||
});
|
||||
});
|
||||
|
||||
it("preserves optional fields from attrs", () => {
|
||||
const result = resolveDefaultNodeAttrs({
|
||||
name: "op",
|
||||
namespace: "ns",
|
||||
version: "1.0.0",
|
||||
type: "mutation",
|
||||
inputSchema: {},
|
||||
outputSchema: {},
|
||||
description: "test op",
|
||||
tags: ["a", "b"],
|
||||
});
|
||||
expect(result.description).toBe("test op");
|
||||
expect(result.tags).toEqual(["a", "b"]);
|
||||
});
|
||||
|
||||
it("prefers attrs over defaults", () => {
|
||||
const result = resolveDefaultNodeAttrs(
|
||||
{ type: "subscription" },
|
||||
{ type: "query" },
|
||||
);
|
||||
expect(result.type).toBe("subscription");
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user