Skip to content

Commit

Permalink
fix: change dummyFucntions to jest.fn() and remove redundant copilo…
Browse files Browse the repository at this point in the history
…t test
  • Loading branch information
LironMShemen committed Jan 7, 2025
1 parent 8375e40 commit 82eb204
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 22 deletions.
11 changes: 5 additions & 6 deletions src/Copilot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
customActionsCategory,
actionsCategory2,
actionsCategory,
tapButtonContext
dummyContext
} from "./test-utils/extendAPICatalogUtils";

jest.mock('@/actions/StepPerformer');
Expand Down Expand Up @@ -211,10 +211,10 @@ describe('Copilot', () => {
it('should extend the API catalog with a new category and context', () => {
Copilot.init(mockConfig);
const instance = Copilot.getInstance();
instance.extendAPICatalog(actionsCategory, tapButtonContext);
instance.extendAPICatalog(actionsCategory, dummyContext);

expect(mockConfig.frameworkDriver.apiCatalog.categories).toEqual([actionsCategory]);
expect(spyStepPerformer).toHaveBeenCalledWith(tapButtonContext);
expect(spyStepPerformer).toHaveBeenCalledWith(dummyContext);
});

it('should extend the API catalog with an existing category and an additional new category', () => {
Expand All @@ -223,10 +223,9 @@ describe('Copilot', () => {

// item is added to existing category
instance.extendAPICatalog(actionsCategory)
instance.extendAPICatalog(actionsCategory2, tapButtonContext);
instance.extendAPICatalog(actionsCategory2, dummyContext);
expect(mockConfig.frameworkDriver.apiCatalog.categories).toEqual([actionsCategory]);
expect(mockConfig.frameworkDriver.apiCatalog.categories[0].items).toHaveLength(2);
expect(spyStepPerformer).toHaveBeenCalledWith(tapButtonContext);
expect(spyStepPerformer).toHaveBeenCalledWith(dummyContext);

// another category is added
instance.extendAPICatalog(customActionsCategory);
Expand Down
20 changes: 10 additions & 10 deletions src/actions/StepPerformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {SnapshotManager} from '@/utils/SnapshotManager';
import {CacheHandler} from '@/utils/CacheHandler';
import {PromptHandler, TestingFrameworkAPICatalog} from '@/types';
import * as crypto from 'crypto';
import {functionContext, tapButtonContext, newFunctionContext} from "../test-utils/extendAPICatalogUtils";
import {dummyContext, dummyBarContext1, dummyBarContext2} from "../test-utils/extendAPICatalogUtils";

jest.mock('fs');
jest.mock('crypto');
Expand Down Expand Up @@ -288,33 +288,33 @@ describe('StepPerformer', () => {
describe('extendJSContext', () =>{
it('should extend the context with the given object', async () => {
// Initial context
stepPerformer.extendJSContext(functionContext);
stepPerformer.extendJSContext(dummyBarContext1);

setupMocks();
await stepPerformer.perform(INTENT);
expect(mockCodeEvaluator.evaluate).toHaveBeenCalledWith(PROMPT_RESULT, functionContext);
expect(mockCodeEvaluator.evaluate).toHaveBeenCalledWith(PROMPT_RESULT, dummyBarContext1);

// Extended context
const newFixedContext = { ...functionContext, ...tapButtonContext };
stepPerformer.extendJSContext(tapButtonContext);
const newFixedContext = { ...dummyBarContext1, ...dummyContext };
stepPerformer.extendJSContext(dummyContext);

await stepPerformer.perform(INTENT);
expect(mockCodeEvaluator.evaluate).toHaveBeenCalledWith(PROMPT_RESULT, newFixedContext);
});

it('should log when a context is overridden', async () => {
jest.spyOn(console, 'log');
stepPerformer.extendJSContext(functionContext);
stepPerformer.extendJSContext(dummyBarContext1);

setupMocks();
await stepPerformer.perform(INTENT);
expect(mockCodeEvaluator.evaluate).toHaveBeenCalledWith(PROMPT_RESULT, functionContext);
expect(mockCodeEvaluator.evaluate).toHaveBeenCalledWith(PROMPT_RESULT, dummyBarContext1);

stepPerformer.extendJSContext(newFunctionContext);
expect(console.log).toHaveBeenCalledWith('Notice: Context function is overridden by the new context value');
stepPerformer.extendJSContext(dummyBarContext2);
expect(console.log).toHaveBeenCalledWith('Notice: Context bar is overridden by the new context value');

await stepPerformer.perform(INTENT);
expect(mockCodeEvaluator.evaluate).toHaveBeenCalledWith(PROMPT_RESULT, newFunctionContext);
expect(mockCodeEvaluator.evaluate).toHaveBeenCalledWith(PROMPT_RESULT, dummyBarContext2);

});
});
Expand Down
4 changes: 2 additions & 2 deletions src/integration tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as crypto from 'crypto';
import {mockedCacheFile, mockCache} from "../test-utils/cache";
import {PromptCreator} from "../utils/PromptCreator";
import {StepPerformer} from "../actions/StepPerformer";
import {customActionsCategory, actionsCategory, tapButtonContext} from "../test-utils/extendAPICatalogUtils";
import {customActionsCategory, actionsCategory, dummyContext} from "../test-utils/extendAPICatalogUtils";

jest.mock('crypto');
jest.mock('fs');
Expand Down Expand Up @@ -350,7 +350,7 @@ describe('Copilot Integration Tests', () => {
copilot.extendAPICatalog(customActionsCategory);
expect(spyPromptCreator).toHaveBeenCalledTimes(1);

copilot.extendAPICatalog(actionsCategory, tapButtonContext);
copilot.extendAPICatalog(actionsCategory, dummyContext);
expect(spyPromptCreator).toHaveBeenCalledTimes(2);
expect(spyStepPerformer).toHaveBeenCalledTimes(1);
});
Expand Down
7 changes: 3 additions & 4 deletions src/test-utils/extendAPICatalogUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export const actionsCategory2 = {
]
};

export const tapButtonContext = {tapButton: 'new tap button'};
export const functionContext = {function: 'newFlow'};
export const newFunctionContext = {function: 'newFunction'};

export const dummyContext = {foo: jest.fn()};
export const dummyBarContext1 = {bar: jest.fn()};
export const dummyBarContext2 = {bar: jest.fn()};

0 comments on commit 82eb204

Please sign in to comment.