Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Motivation <!-- List motivation and changes here --> - adds several tests - make actions more flexible - allow function calling ## Issues closed <!-- List closed issues here -->
- Loading branch information
Showing
39 changed files
with
847 additions
and
321 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import "@testing-library/jest-dom"; | ||
|
||
if (typeof CSS === "undefined") { | ||
global.CSS = { | ||
escape: (string_: string) => string_.replaceAll(/([()\\{}])/g, "\\$1"), | ||
} as any; // Cast to 'any' to bypass TypeScript's type checking for the mock. | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
8 changes: 5 additions & 3 deletions
8
...ces/docs/silent-action/color-mode-dark.md → ...rces/actions/functions/color-mode-dark.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 5 additions & 3 deletions
8
...es/docs/silent-action/color-mode-light.md → ...ces/actions/functions/color-mode-light.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 6 additions & 4 deletions
10
resources/docs/silent-action/language-de.md → resources/actions/functions/language-de.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 5 additions & 3 deletions
8
resources/docs/silent-action/language-en.md → resources/actions/functions/language-en.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import { handleCaptainAction, performElementAction } from "../action"; | ||
|
||
import { buildKey } from "#/build-key"; | ||
import { ID } from "#/enums"; | ||
import type { VectorStoreResponse } from "#/types/vector-store"; | ||
|
||
jest.mock("#/build-key", () => ({ | ||
buildKey: jest.fn(), | ||
})); | ||
|
||
describe("handleCaptainAction", () => { | ||
// Mocking window.ipc.send | ||
const mockSend = jest.fn(); | ||
beforeAll(() => { | ||
// Ensure window.ipc exists | ||
global.window.ipc = { send: mockSend } as any; | ||
Check warning on line 16 in src/client/ions/handlers/__tests__/action.test.ts GitHub Actions / release
|
||
}); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("should handle function actions correctly", () => { | ||
const response: VectorStoreResponse = { | ||
id: "1", | ||
score: 0.5, | ||
payload: { | ||
id: "testFunction", | ||
language: "en", | ||
action: "function", | ||
label: "Test Function", | ||
}, | ||
}; | ||
|
||
(buildKey as jest.Mock).mockReturnValue("functionKey"); | ||
|
||
handleCaptainAction(response); | ||
|
||
expect(buildKey).toHaveBeenCalledWith([ID.CAPTAIN_ACTION]); | ||
expect(mockSend).toHaveBeenCalledWith("functionKey", { | ||
action: response.payload.action, | ||
payload: response.payload, | ||
}); | ||
}); | ||
|
||
it("should handle non-function actions correctly", () => { | ||
const response: VectorStoreResponse = { | ||
id: "2", | ||
score: 0.5, | ||
payload: { | ||
id: "testApp", | ||
language: "en", | ||
action: "open", | ||
label: "Test App", | ||
}, | ||
}; | ||
|
||
(buildKey as jest.Mock).mockReturnValue("appKey:open"); | ||
|
||
handleCaptainAction(response); | ||
|
||
expect(buildKey).toHaveBeenCalledWith([ID.APP], { suffix: ":open" }); | ||
expect(mockSend).toHaveBeenCalledWith("appKey:open", { | ||
appId: response.payload.id, | ||
action: response.payload.action, | ||
}); | ||
}); | ||
}); | ||
|
||
describe("performElementAction", () => { | ||
// Setup a DOM element for testing | ||
beforeAll(() => { | ||
document.body.innerHTML = `<div data-captainid="test-element"></div>`; | ||
}); | ||
|
||
it("executes the action on an element when found", () => { | ||
const mockAction = jest.fn(); | ||
performElementAction("test-element", mockAction); | ||
expect(mockAction).toHaveBeenCalledTimes(1); | ||
expect(mockAction).toHaveBeenCalledWith(expect.any(HTMLElement)); | ||
}); | ||
|
||
it("does not execute the action when the element is not found", () => { | ||
const mockAction = jest.fn(); | ||
performElementAction("nonexistent-element", mockAction); | ||
expect(mockAction).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("logs an error when the action function throws", () => { | ||
const mockAction = jest.fn().mockImplementation(() => { | ||
throw new Error("Test error"); | ||
}); | ||
const consoleErrorSpy = jest.spyOn(console, "error").mockImplementation(() => {}); | ||
|
||
performElementAction("test-element", mockAction); | ||
expect(consoleErrorSpy).toHaveBeenCalledWith( | ||
expect.stringContaining( | ||
"Error performing action on element with captainId=test-element:" | ||
), | ||
expect.any(Error) | ||
); | ||
|
||
// Clean up | ||
consoleErrorSpy.mockRestore(); | ||
}); | ||
}); |
Oops, something went wrong.