fix: fix workflow tests

This commit is contained in:
Dallin Romney 2025-10-03 17:47:13 -07:00
parent 3e80f3d8c5
commit dcddb5429b
6 changed files with 58 additions and 53 deletions

View file

@ -237,8 +237,12 @@ describe("ConfigEnhancer", () => {
it("should handle workflow integration when workflow is active", async () => {
// Mock service container to return active workflow
const { serviceContainer } = await import("./services/ServiceContainer.js");
(serviceContainer.get as any).mockResolvedValueOnce({
const options: BaseCommandOptions = {
rule: ["user-rule"],
prompt: ["user-prompt"],
};
const config = await enhancer.enhanceConfig(mockConfig, options, {
workflowFile: {
name: "Test Workflow",
prompt: "You are a test assistant",
@ -246,16 +250,11 @@ describe("ConfigEnhancer", () => {
model: "gpt-4",
tools: "bash,read",
},
workflow: "owner/test-workflow",
slug: "owner/test-workflow",
workflowModelName: null,
workflowService: null,
});
const options: BaseCommandOptions = {
rule: ["user-rule"],
prompt: ["user-prompt"],
};
const config = await enhancer.enhanceConfig(mockConfig, options);
// Should have both workflow and user rules
expect(config.rules).toHaveLength(2);
expect(config.rules?.[0]).toBe("Always be helpful"); // Workflow rule first

View file

@ -12,8 +12,7 @@ import {
modelProcessor,
processRule,
} from "./hubLoader.js";
import { serviceContainer } from "./services/ServiceContainer.js";
import { SERVICE_NAMES, WorkflowServiceState } from "./services/types.js";
import { WorkflowServiceState } from "./services/types.js";
import { logger } from "./util/logger.js";
/**
@ -26,18 +25,13 @@ export class ConfigEnhancer {
async enhanceConfig(
config: AssistantUnrolled,
_options: BaseCommandOptions,
workflowState?: WorkflowServiceState,
): Promise<AssistantUnrolled> {
let enhancedConfig = { ...config };
const options = { ..._options };
// Add workflow rules/mcp servers if present
const workflowState = await serviceContainer.get<WorkflowServiceState>(
SERVICE_NAMES.WORKFLOW,
);
const { workflowFile, workflowService } = workflowState;
if (workflowFile) {
const { rules, model, tools, prompt } = workflowFile;
if (workflowState?.workflowFile) {
const { rules, model, tools, prompt } = workflowState?.workflowFile;
if (rules) {
options.rule = [
...rules.split(",").filter(Boolean),
@ -59,7 +53,9 @@ export class ConfigEnhancer {
workflowModel,
...(enhancedConfig.models ?? []),
];
workflowService?.setWorkflowModelName(workflowModel.name);
workflowState?.workflowService?.setWorkflowModelName(
workflowModel.name,
);
}
// Add workflow prompt as prefix (see processAndCombinePrompts)

View file

@ -12,7 +12,7 @@ import * as configLoader from "../configLoader.js";
import { ConfigService } from "./ConfigService.js";
import { serviceContainer } from "./ServiceContainer.js";
import { SERVICE_NAMES } from "./types.js";
import { SERVICE_NAMES, WorkflowServiceState } from "./types.js";
describe("ConfigService", () => {
let service: ConfigService;
@ -23,7 +23,12 @@ describe("ConfigService", () => {
systemMessage: "Test system message",
} as any;
const mockApiClient = { get: vi.fn(), post: vi.fn() };
const mockWorkflowState: WorkflowServiceState = {
slug: null,
workflowFile: null,
workflowModelName: null,
workflowService: null,
};
beforeEach(() => {
vi.clearAllMocks();
service = new ConfigService();
@ -41,6 +46,7 @@ describe("ConfigService", () => {
"/path/to/config.yaml",
"org-123",
mockApiClient as any,
mockWorkflowState,
);
expect(state).toEqual({
@ -60,6 +66,7 @@ describe("ConfigService", () => {
undefined,
"org-123",
mockApiClient as any,
mockWorkflowState,
);
expect(state).toEqual({
@ -87,6 +94,7 @@ describe("ConfigService", () => {
"/config.yaml",
"org-123",
mockApiClient as any,
mockWorkflowState,
{ rule: ["rule1", "rule2"] },
);
@ -94,6 +102,7 @@ describe("ConfigService", () => {
expect(vi.mocked(configEnhancer.enhanceConfig)).toHaveBeenCalledWith(
mockConfig,
{ rule: ["rule1", "rule2"] },
mockWorkflowState,
);
expect(state.config).toEqual(expectedConfig);
@ -111,6 +120,7 @@ describe("ConfigService", () => {
{ accessToken: "token" } as any,
"/old.yaml",
"org-123",
mockWorkflowState,
mockApiClient as any,
);
@ -140,6 +150,7 @@ describe("ConfigService", () => {
{ accessToken: "token" } as any,
"/old.yaml",
"org-123",
mockWorkflowState,
mockApiClient as any,
);
@ -169,6 +180,7 @@ describe("ConfigService", () => {
{ accessToken: "token" } as any,
"/config.yaml",
"org-123",
mockWorkflowState,
mockApiClient as any,
);
@ -201,6 +213,7 @@ describe("ConfigService", () => {
{ accessToken: "token" } as any,
undefined,
"org-123",
mockWorkflowState,
mockApiClient as any,
);
@ -225,6 +238,7 @@ describe("ConfigService", () => {
{ accessToken: "token" } as any,
"/old.yaml",
"org-123",
mockWorkflowState,
mockApiClient as any,
);
@ -267,6 +281,7 @@ describe("ConfigService", () => {
{ accessToken: "token" } as any,
"/old.yaml",
"org-123",
mockWorkflowState,
mockApiClient as any,
);
@ -304,6 +319,7 @@ describe("ConfigService", () => {
{ accessToken: "token" } as any,
"/old.yaml",
"org-123",
mockWorkflowState,
mockApiClient as any,
);
@ -337,6 +353,7 @@ describe("ConfigService", () => {
{ accessToken: "token" } as any,
"/old.yaml",
"org-123",
mockWorkflowState,
mockApiClient as any,
);

View file

@ -11,6 +11,7 @@ import {
ApiClientServiceState,
ConfigServiceState,
SERVICE_NAMES,
WorkflowServiceState,
} from "./types.js";
/**
@ -47,6 +48,7 @@ export class ConfigService
configPath: string | undefined,
_organizationId: string | null,
apiClient: DefaultApiInterface,
workflowState: WorkflowServiceState,
injectedConfigOptions?: BaseCommandOptions,
): Promise<ConfigServiceState> {
// Use the new streamlined config loader
@ -63,6 +65,7 @@ export class ConfigService
config = await configEnhancer.enhanceConfig(
config,
injectedConfigOptions,
workflowState,
);
logger.debug("Applied injected configuration");

View file

@ -214,8 +214,8 @@ export async function initializeServices(
configPath,
finalAuthState.organizationId || null,
apiClientState.apiClient,
commandOptions,
workflowState,
commandOptions,
);
},
[SERVICE_NAMES.AUTH, SERVICE_NAMES.API_CLIENT, SERVICE_NAMES.WORKFLOW], // Dependencies

View file

@ -3,8 +3,6 @@ import { vi } from "vitest";
import { ConfigEnhancer } from "../configEnhancer.js";
import { ModelService } from "./ModelService.js";
import { serviceContainer } from "./ServiceContainer.js";
import { SERVICE_NAMES } from "./types.js";
import { WorkflowService } from "./WorkflowService.js";
// Mock the hubLoader module
@ -100,16 +98,6 @@ describe("Workflow Integration Tests", () => {
modelService = new ModelService();
configEnhancer = new ConfigEnhancer();
// Mock service container
vi.spyOn(serviceContainer, "get").mockImplementation(
async (serviceName: string) => {
if (serviceName === SERVICE_NAMES.WORKFLOW) {
return workflowService.getState();
}
throw new Error(`Service ${serviceName} not mocked`);
},
);
// Setup default mocks
mockProcessRule.mockResolvedValue("Processed rule content");
mockLoadPackagesFromHub.mockResolvedValue([{ name: "test-mcp" }]);
@ -146,6 +134,7 @@ describe("Workflow Integration Tests", () => {
const enhancedConfig = await configEnhancer.enhanceConfig(
baseConfig as any,
baseOptions,
workflowState,
);
// Should have loaded the workflow model directly via loadPackageFromHub
@ -176,6 +165,7 @@ describe("Workflow Integration Tests", () => {
const enhancedConfig = await configEnhancer.enhanceConfig(
baseConfig as any,
baseOptions,
workflowState,
);
// Should not have enhanced with any models
@ -206,6 +196,7 @@ describe("Workflow Integration Tests", () => {
const enhancedConfig = await configEnhancer.enhanceConfig(
baseConfig as any,
baseOptions,
workflowService.getState(),
);
// Should process the user model via loadPackagesFromHub
@ -246,6 +237,7 @@ describe("Workflow Integration Tests", () => {
const enhancedConfig = await configEnhancer.enhanceConfig(
baseConfig as any,
{},
workflowService.getState(),
);
// Rules should be processed normally since workflow rules are now added to options.rule
@ -269,6 +261,7 @@ describe("Workflow Integration Tests", () => {
const enhancedConfig = await configEnhancer.enhanceConfig(
baseConfig as any,
{},
workflowService.getState(),
);
expect(mockProcessRule).not.toHaveBeenCalled();
@ -292,6 +285,7 @@ describe("Workflow Integration Tests", () => {
const enhancedConfig = await configEnhancer.enhanceConfig(
baseConfig as any,
{},
workflowService.getState(),
);
expect(mockProcessRule).not.toHaveBeenCalled();
@ -367,6 +361,7 @@ describe("Workflow Integration Tests", () => {
const enhancedConfig = await configEnhancer.enhanceConfig(
baseConfig as any,
{},
workflowService.getState(),
);
// Should not inject workflow rule but should preserve existing rules
@ -389,6 +384,7 @@ describe("Workflow Integration Tests", () => {
const enhancedConfig = await configEnhancer.enhanceConfig(
baseConfig as any,
{},
workflowService.getState(),
);
// Prompts should be processed at runtime, not injected into config
@ -452,27 +448,18 @@ describe("Workflow Integration Tests", () => {
mockLoadPackageFromHub.mockResolvedValue(mockWorkflowFile);
await workflowService.initialize("owner/workflow");
// Create a spy to verify the internal behavior of ConfigEnhancer
const originalGet = serviceContainer.get;
const getSpy = vi
.spyOn(serviceContainer, "get")
.mockImplementation(async (serviceName: string) => {
if (serviceName === SERVICE_NAMES.WORKFLOW) {
return workflowService.getState();
}
return originalGet.call(serviceContainer, serviceName);
});
const baseOptions = { prompt: ["user-prompt"] };
// This should internally modify the options to include workflow prompt
await configEnhancer.enhanceConfig({} as any, baseOptions);
await configEnhancer.enhanceConfig(
{} as any,
baseOptions,
workflowService.getState(),
);
// The ConfigEnhancer should have been called with workflow service
expect(getSpy).toHaveBeenCalledWith(SERVICE_NAMES.WORKFLOW);
// Clean up
getSpy.mockRestore();
// Verify that the workflow prompt was added to the options
// by checking that injectPrompts was called with workflow prompt prefixed
expect(baseOptions.prompt).toHaveLength(1);
});
it("should work end-to-end with workflow prompt processing", async () => {
@ -564,6 +551,7 @@ describe("Workflow Integration Tests", () => {
const enhancedConfig = await configEnhancer.enhanceConfig(
baseConfig as any,
{},
workflowService.getState(),
);
expect(enhancedConfig.mcpServers).toHaveLength(3);
@ -588,6 +576,7 @@ describe("Workflow Integration Tests", () => {
const enhancedConfig = await configEnhancer.enhanceConfig(
baseConfig as any,
{},
workflowService.getState(),
);
expect(enhancedConfig.mcpServers).toHaveLength(1);
@ -612,6 +601,7 @@ describe("Workflow Integration Tests", () => {
const enhancedConfig = await configEnhancer.enhanceConfig(
baseConfig as any,
{},
workflowService.getState(),
);
// Should not deduplicate since we simplified the logic