-
Notifications
You must be signed in to change notification settings - Fork 3
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
base: master
Are you sure you want to change the base?
Conversation
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
a4baefd
to
73e1858
Compare
There was a problem hiding this 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
private config: Config; | ||
|
||
private constructor(config: Config) { | ||
this.config = config; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
src/Copilot.test.ts
Outdated
actionsCategory2, | ||
actionsCategory, | ||
tapButtonContext | ||
} from "./test-utils/expendAPICatalogUtils"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo (extend*)
src/Copilot.test.ts
Outdated
instance.extendAPICatalog(actionsCategory) | ||
instance.extendAPICatalog(actionsCategory2, tapButtonContext); | ||
expect(mockConfig.frameworkDriver.apiCatalog.categories).toEqual([...actionsCategory]); | ||
expect(mockConfig.frameworkDriver.apiCatalog.categories[0].items).toHaveLength(2); |
There was a problem hiding this comment.
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 = [{ |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
export const tapButtonContext = {tapButton: 'new tap button'}; | ||
export const functionContext = {function: 'newFlow'}; | ||
export const newFunctionContext = {function: 'newFunction'}; |
There was a problem hiding this comment.
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
src/Copilot.test.ts
Outdated
// item is added to existing category | ||
instance.extendAPICatalog(actionsCategory) | ||
instance.extendAPICatalog(actionsCategory2, tapButtonContext); | ||
expect(mockConfig.frameworkDriver.apiCatalog.categories).toEqual([...actionsCategory]); |
There was a problem hiding this comment.
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
).
There was a problem hiding this comment.
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
.
src/Copilot.test.ts
Outdated
expect(spyStepPerformer).toHaveBeenCalledWith(tapButtonContext); | ||
}); | ||
|
||
it('should expend the API catalog with an existing category and an additional new category', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo (extend)
82eb204
to
7366a89
Compare
There was a problem hiding this 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
src/Copilot.test.ts
Outdated
customActionsCategory, | ||
actionsCategory2, | ||
actionsCategory, |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
src/actions/StepPerformer.test.ts
Outdated
expect(mockCodeEvaluator.evaluate).toHaveBeenCalledWith(PROMPT_RESULT, dummyBarContext1); | ||
|
||
// Extended context | ||
const newFixedContext = { ...dummyBarContext1, ...dummyContext }; |
There was a problem hiding this comment.
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
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}; | ||
} |
There was a problem hiding this comment.
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,
};
}
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
src/integration tests/index.test.ts
Outdated
}); | ||
it('should call relevant functions to extend the catalog', () => { |
There was a problem hiding this comment.
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
|
||
/** | ||
* 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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PERFECT
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
categories and items
→ APIs (categories and JS context)
src/utils/PromptCreator.test.ts
Outdated
|
||
describe('extentAPICategories', () => { | ||
it('should extend the API catalog with new or exist category', () => { | ||
// Add new category |
There was a problem hiding this comment.
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
src/utils/PromptCreator.ts
Outdated
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); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 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);
}
});
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- I think it's better if we won't allow passing a single category as an argument for parameter that is called
categories
No description provided.