-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…ability-to-generate-message adding capability to create/extract message from command palette | Sherlock 🕵️♂️
- Loading branch information
Showing
14 changed files
with
281 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"vs-code-extension": minor | ||
--- | ||
|
||
add commands to command palette |
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
156 changes: 156 additions & 0 deletions
156
inlang/source-code/ide-extension/src/commands/createMessage.test.ts
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,156 @@ | ||
import { describe, it, beforeEach, afterEach, vi, expect } from "vitest" | ||
import { createMessageCommand } from "./createMessage.js" | ||
import { state } from "../utilities/state.js" | ||
import { msg } from "../utilities/messages/msg.js" | ||
import { window } from "vscode" | ||
import { telemetry } from "../services/telemetry/index.js" | ||
import { CONFIGURATION } from "../configuration.js" | ||
|
||
vi.mock("vscode", () => ({ | ||
commands: { | ||
registerCommand: vi.fn(), | ||
}, | ||
window: { | ||
showInputBox: vi.fn(), | ||
showErrorMessage: vi.fn(), | ||
}, | ||
})) | ||
|
||
vi.mock("../utilities/state", () => ({ | ||
state: vi.fn(), | ||
})) | ||
|
||
vi.mock("../utilities/messages/msg", () => ({ | ||
msg: vi.fn(), | ||
})) | ||
|
||
vi.mock("../services/telemetry/index", () => ({ | ||
telemetry: { | ||
capture: vi.fn(), | ||
}, | ||
})) | ||
|
||
vi.mock("../configuration", () => ({ | ||
CONFIGURATION: { | ||
EVENTS: { | ||
ON_DID_CREATE_MESSAGE: { | ||
fire: vi.fn(), | ||
}, | ||
}, | ||
}, | ||
})) | ||
|
||
describe("createMessageCommand", () => { | ||
const mockState = { | ||
project: { | ||
settings: vi.fn().mockReturnThis(), | ||
customApi: vi.fn().mockReturnThis(), | ||
query: { | ||
messages: { | ||
create: vi.fn(), | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
beforeEach(() => { | ||
vi.resetAllMocks() | ||
// @ts-expect-error | ||
state.mockReturnValue(mockState) | ||
}) | ||
|
||
afterEach(() => { | ||
vi.restoreAllMocks() | ||
}) | ||
|
||
it("should warn if sourceLanguageTag is undefined", async () => { | ||
mockState.project.settings.mockReturnValueOnce({ sourceLanguageTag: undefined }) | ||
|
||
await createMessageCommand.callback() | ||
|
||
expect(msg).toHaveBeenCalledWith( | ||
"The `sourceLanguageTag` is not defined in the project but required to create a message.", | ||
"warn", | ||
"notification" | ||
) | ||
}) | ||
|
||
it("should return if message content input is cancelled", async () => { | ||
mockState.project.settings.mockReturnValueOnce({ sourceLanguageTag: "en" }) | ||
// @ts-expect-error | ||
window.showInputBox.mockResolvedValueOnce(undefined) | ||
|
||
await createMessageCommand.callback() | ||
|
||
expect(window.showInputBox).toHaveBeenCalledWith({ | ||
title: "Enter the message content:", | ||
}) | ||
expect(msg).not.toHaveBeenCalled() | ||
}) | ||
|
||
it("should return if message ID input is cancelled", async () => { | ||
mockState.project.settings.mockReturnValueOnce({ sourceLanguageTag: "en" }) | ||
// @ts-expect-error | ||
window.showInputBox.mockResolvedValueOnce("Message content") | ||
// @ts-expect-error | ||
window.showInputBox.mockResolvedValueOnce(undefined) | ||
|
||
await createMessageCommand.callback() | ||
|
||
expect(window.showInputBox).toHaveBeenCalledWith({ | ||
title: "Enter the ID:", | ||
}) | ||
expect(msg).not.toHaveBeenCalled() | ||
}) | ||
|
||
it("should show error message if message creation fails", async () => { | ||
mockState.project.settings.mockReturnValueOnce({ sourceLanguageTag: "en" }) | ||
// @ts-expect-error | ||
window.showInputBox.mockResolvedValueOnce("Message content") | ||
// @ts-expect-error | ||
window.showInputBox.mockResolvedValueOnce("messageId") | ||
mockState.project.query.messages.create.mockReturnValueOnce(false) | ||
|
||
await createMessageCommand.callback() | ||
|
||
expect(window.showErrorMessage).toHaveBeenCalledWith( | ||
"Couldn't upsert new message with id messageId." | ||
) | ||
}) | ||
|
||
it("should create message and show success message", async () => { | ||
mockState.project.settings.mockReturnValueOnce({ sourceLanguageTag: "en" }) | ||
// @ts-expect-error | ||
window.showInputBox.mockResolvedValueOnce("Message content") | ||
// @ts-expect-error | ||
window.showInputBox.mockResolvedValueOnce("messageId") | ||
mockState.project.query.messages.create.mockReturnValueOnce(true) | ||
|
||
await createMessageCommand.callback() | ||
|
||
expect(mockState.project.query.messages.create).toHaveBeenCalledWith({ | ||
data: { | ||
id: "messageId", | ||
alias: {}, | ||
selectors: [], | ||
variants: [ | ||
{ | ||
languageTag: "en", | ||
match: [], | ||
pattern: [ | ||
{ | ||
type: "Text", | ||
value: "Message content", | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
}) | ||
expect(CONFIGURATION.EVENTS.ON_DID_CREATE_MESSAGE.fire).toHaveBeenCalled() | ||
expect(telemetry.capture).toHaveBeenCalledWith({ | ||
event: "IDE-EXTENSION command executed: Create Message", | ||
}) | ||
expect(msg).toHaveBeenCalledWith("Message created.") | ||
}) | ||
}) |
76 changes: 76 additions & 0 deletions
76
inlang/source-code/ide-extension/src/commands/createMessage.ts
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,76 @@ | ||
import { state } from "../utilities/state.js" | ||
import { msg } from "../utilities/messages/msg.js" | ||
import { commands, window } from "vscode" | ||
import { telemetry } from "../services/telemetry/index.js" | ||
import type { Message } from "@inlang/sdk" | ||
import { CONFIGURATION } from "../configuration.js" | ||
|
||
/** | ||
* Helps the user to create messages by prompting for the message content. | ||
*/ | ||
export const createMessageCommand = { | ||
command: "sherlock.createMessage", | ||
title: "Sherlock: Create Message", | ||
register: commands.registerCommand, | ||
callback: async function () { | ||
const sourceLanguageTag = state().project.settings().sourceLanguageTag | ||
|
||
// guard | ||
if (sourceLanguageTag === undefined) { | ||
return msg( | ||
"The `sourceLanguageTag` is not defined in the project but required to create a message.", | ||
"warn", | ||
"notification" | ||
) | ||
} | ||
|
||
const messageValue = await window.showInputBox({ | ||
title: "Enter the message content:", | ||
}) | ||
if (messageValue === undefined) { | ||
return | ||
} | ||
|
||
const messageId = await window.showInputBox({ | ||
title: "Enter the ID:", | ||
}) | ||
if (messageId === undefined) { | ||
return | ||
} | ||
|
||
const message: Message = { | ||
id: messageId, | ||
alias: {}, | ||
selectors: [], | ||
variants: [ | ||
{ | ||
languageTag: sourceLanguageTag, | ||
match: [], | ||
pattern: [ | ||
{ | ||
type: "Text", | ||
value: messageValue, | ||
}, | ||
], | ||
}, | ||
], | ||
} | ||
|
||
// create message | ||
const success = state().project.query.messages.create({ | ||
data: message, | ||
}) | ||
|
||
if (!success) { | ||
return window.showErrorMessage(`Couldn't upsert new message with id ${messageId}.`) | ||
} | ||
|
||
// Emit event to notify that a message was created | ||
CONFIGURATION.EVENTS.ON_DID_CREATE_MESSAGE.fire() | ||
|
||
telemetry.capture({ | ||
event: "IDE-EXTENSION command executed: Create Message", | ||
}) | ||
return msg("Message created.") | ||
}, | ||
} as const |
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
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
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
Oops, something went wrong.