Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce extend api catalog method #34

Open
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

LironMShemen
Copy link
Collaborator

No description provided.

Add the function to types.ts and index.ts
- Add `extendAPICategories` function to `PromptCreator`
- Add `extendJSContext` function to `StepPerformer
- Add `extendAPICatalog` function to `Copilot`
suitable test suites.

test: suitable test suites.

test: add suitable test suites

- Add `Copilot`, `StepPerformer`, `index` and `PromptCreator` suites
- Add utils file
- Add snapshot file for `PromptCreator` test
@LironMShemen LironMShemen force-pushed the Introduce-extendAPICatalog-method branch from a4baefd to 73e1858 Compare January 6, 2025 15:21
Copy link
Collaborator

@asafkorem asafkorem left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job 🙌🏼 👸🏼
Push the fixes in a separate commit for now, we'll rebase things once we'll finish the review

src/Copilot.ts Outdated
Comment on lines 24 to 27
private config: Config;

private constructor(config: Config) {
this.config = config;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you need to save the config?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t need it :)
Maybe I thought of it at the beginning.
Removed.

actionsCategory2,
actionsCategory,
tapButtonContext
} from "./test-utils/expendAPICatalogUtils";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo (extend*)

instance.extendAPICatalog(actionsCategory)
instance.extendAPICatalog(actionsCategory2, tapButtonContext);
expect(mockConfig.frameworkDriver.apiCatalog.categories).toEqual([...actionsCategory]);
expect(mockConfig.frameworkDriver.apiCatalog.categories[0].items).toHaveLength(2);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you dont need this check since you already checked equality

]
}];

export const actionsCategory = [{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why as a list and not just an object?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. Initially, I wanted to allow the user to add more than one category, so I let them insert a list of categories. I have now added the option to insert either a single Category object or a list of Categories.

Comment on lines 37 to 39
export const tapButtonContext = {tapButton: 'new tap button'};
export const functionContext = {function: 'newFlow'};
export const newFunctionContext = {function: 'newFunction'};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you need three different?
you can just use jest.fn()s instead

Comment on lines 224 to 227
// item is added to existing category
instance.extendAPICatalog(actionsCategory)
instance.extendAPICatalog(actionsCategory2, tapButtonContext);
expect(mockConfig.frameworkDriver.apiCatalog.categories).toEqual([...actionsCategory]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it looks like a bug 🤔
we need to merge categories in case they have the same name like in this example (both has the name Actions).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is exactly what happens :)
You can see in the utils file that actionCategory and actionCategory2 have the same title, Actions.

expect(spyStepPerformer).toHaveBeenCalledWith(tapButtonContext);
});

it('should expend the API catalog with an existing category and an additional new category', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo (extend)

fix: changes to allow the function to get either a list of categories or a single category object
@LironMShemen LironMShemen force-pushed the Introduce-extendAPICatalog-method branch from 82eb204 to 7366a89 Compare January 7, 2025 11:30
Copy link
Collaborator

@asafkorem asafkorem left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good job. round 2

Comment on lines 7 to 9
customActionsCategory,
actionsCategory2,
actionsCategory,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

naming is confusing (both refer to actions)
use barCategory and bazCategory1, bazCategory2 or something? just not to mention actions in both categories

src/Copilot.ts Outdated
@@ -109,6 +109,17 @@ export class Copilot {
this.cacheHandler.flushTemporaryCache();
}

/**
* Allow the user to add context and categories (titles and items) to the existing API catalog.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enriches the API catalog with given categories and JS context.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or "Adds ...".
the description should describe what the method does (its effect)

expect(mockCodeEvaluator.evaluate).toHaveBeenCalledWith(PROMPT_RESULT, dummyBarContext1);

// Extended context
const newFixedContext = { ...dummyBarContext1, ...dummyContext };
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think expectedContext or extendedContext is more semantically correct here

Comment on lines +22 to +30
extendJSContext(newContext: any): void {
for (const key in newContext) {
if (key in this.context) {
console.log(`Notice: Context ${key} is overridden by the new context value`);
break;
}
}
this.context = {...this.context, ...newContext};
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extendJSContext(newContext: any): void {
  const overriddenKey = Object.keys(newContext).find(key => key in this.context);
  
  if (overriddenKey) {
    console.log(`Notice: Context ${overriddenKey} is overridden by the new context value`);
  }
  
  this.context = {
    ...this.context,
    ...newContext,
  };
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about this now, we're not merging categories on the first initialization if user passes two categories with the same name.
It makes sense to me if we'll do the same thing there. But IDK, kind of an edge case that doesn't bother me much.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand your point, but I still think it is good practice to keep a title organized and unified.

Comment on lines 347 to 348
});
it('should call relevant functions to extend the catalog', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing empty line

src/types.ts Outdated
Comment on lines 44 to 51

/**
* Extends the API catalog of the testing framework with additional categories and items.
* @param context The variables of the testing framework (i.e. exposes the matching function, expect, etc.).
* @param categories The categories to add to the API catalog.
* @note This can be used to add custom categories and items to the API catalog.
*/
extendAPICatalog: (categories: TestingFrameworkAPICatalogCategory[] | TestingFrameworkAPICatalogCategory, context?: any,) => void;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PERFECT

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

categories and itemsAPIs (categories and JS context)


describe('extentAPICategories', () => {
it('should extend the API catalog with new or exist category', () => {
// Add new category
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test code should be semantic enough so the comment will be redundant

Comment on lines 12 to 24
extendAPICategories(newCategories: TestingFrameworkAPICatalogCategory[] | TestingFrameworkAPICatalogCategory): void {
const categoriesArray = Array.isArray(newCategories) ? newCategories : [newCategories];
for (const category of categoriesArray) {
const existingCategory = this.apiCatalog.categories.find((existingCategory) => existingCategory.title === category.title);
if (existingCategory) {
for (const item of category.items) {
existingCategory.items.push(item);
}
} else {
this.apiCatalog.categories.push(category);
}
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. too many indentations (third-level blocks). let's try to flatten the code:
extendAPICategories(
  newCategories: TestingFrameworkAPICatalogCategory[] | TestingFrameworkAPICatalogCategory
): void {
  const categories = Array.isArray(newCategories) ? newCategories : [newCategories];

  categories.forEach((category) => {
    const existingCategory = this.apiCatalog.categories.find(
      (c) => c.title === category.title
    );

    if (existingCategory) {
      existingCategory.items.push(...category.items);
    } else {
      this.apiCatalog.categories.push(category);
    }
  });
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I think it's better if we won't allow passing a single category as an argument for parameter that is called categories

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants