Skip to content

Commit

Permalink
fix: command completion with the force mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ueokande committed Jul 20, 2024
1 parent 75e2ecd commit c0e6506
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/background/usecases/CommandUseCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class CommandUseCase {
// add original command to completed value
completions.forEach((group) => {
group.items.forEach((item) => {
item.value = name + " " + item.value;
item.value = `${name}${force ? "!" : ""} ${item.value}`;
});
});
return completions;
Expand Down
150 changes: 150 additions & 0 deletions test/background/usecases/CommandUseCase.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import { CommandUseCase } from "../../../src/background/usecases/CommandUseCase";
import { CommandRegistryImpl } from "../../../src/background/command/CommandRegistry";
import type { Command } from "../../../src/background/command/types";
import { describe, beforeAll, test, vi, expect } from "vitest";

const commandA: Command = {
names: () => ["a"],
fullname: () => "a",
description: () => "",
getCompletions: () => {
throw new Error("not implemented");
},
exec: () => {
throw new Error("not implemented");
},
};

const commandB: Command = {
names: () => ["b"],
fullname: () => "b",
description: () => "",
getCompletions: () => {
throw new Error("not implemented");
},
exec: () => {
throw new Error("not implemented");
},
};

describe("CommandUseCase", () => {
let reg: CommandRegistryImpl;

beforeAll(() => {
reg = new CommandRegistryImpl();
reg.register(commandA);
reg.register(commandB);
});

const commandAGetCompletion = vi.spyOn(commandA, "getCompletions");
const commandAExec = vi.spyOn(commandA, "exec");

describe("exec", () => {
test("executes a command", async () => {
const sut = new CommandUseCase(reg);
const ctx = { sender: { tab: { id: 1 }, frameId: 1 } as any };

commandAExec.mockResolvedValue(undefined);

await sut.exec(ctx, "a 123");

expect(commandAExec.mock?.lastCall?.[1]).toBe(false);
expect(commandAExec.mock?.lastCall?.[2]).toBe("123");
});

test("execute a command with force", async () => {
const sut = new CommandUseCase(reg);
const ctx = { sender: { tab: { id: 1 }, frameId: 1 } as any };

commandAExec.mockResolvedValue(undefined);

await sut.exec(ctx, "a! 123");

expect(commandAExec.mock?.lastCall?.[1]).toBe(true);
expect(commandAExec.mock?.lastCall?.[2]).toBe("123");
});

test("command not found", async () => {
const sut = new CommandUseCase(reg);
const ctx = { sender: { tab: { id: 1 }, frameId: 1 } as any };

await expect(() => sut.exec(ctx, "c")).rejects.toThrow(
"c command is not defined",
);
});

test("empty command", async () => {
const sut = new CommandUseCase(reg);
const ctx = { sender: { tab: { id: 1 }, frameId: 1 } as any };

await sut.exec(ctx, "");
// no error
});
});

describe("getCompletions", () => {
test("returns completions of commands", async () => {
const sut = new CommandUseCase(reg);

expect(await sut.getCompletions("")).toEqual([
{
name: "Console Command",
items: [
{ primary: "a", secondary: "", value: "a" },
{ primary: "b", secondary: "", value: "b" },
],
},
]);
expect(await sut.getCompletions("a")).toEqual([
{
name: "Console Command",
items: [{ primary: "a", secondary: "", value: "a" }],
},
]);
});

test("returns completions of command arguments", async () => {
commandAGetCompletion.mockResolvedValue([
{
name: "Group",
items: [{ value: "item1" }, { value: "item2" }],
},
]);

const sut = new CommandUseCase(reg);

expect(await sut.getCompletions("a ")).toEqual([
{
name: "Group",
items: [{ value: "a item1" }, { value: "a item2" }],
},
]);
});

test("returns completions of command arguments with force", async () => {
commandAGetCompletion.mockResolvedValue([
{
name: "Group",
items: [{ value: "item1" }, { value: "item2" }],
},
]);

const sut = new CommandUseCase(reg);

expect(await sut.getCompletions("a! ")).toEqual([
{
name: "Group",
items: [{ value: "a! item1" }, { value: "a! item2" }],
},
]);
});

test("command not found", async () => {
const sut = new CommandUseCase(reg);

await expect(() => sut.getCompletions("c ")).rejects.toThrow(
"c command is not defined",
);
});
});
});

0 comments on commit c0e6506

Please sign in to comment.