Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 40 additions & 17 deletions src/extension/src/command-tree-provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ describe("command-tree-provider", () => {
expect(result).toHaveLength(2);
expect(result[0]).toBeInstanceOf(CommandTreeItem);
expect(result[0].label).toBe("Test Command 1");
expect(result[0].commandString).toBe("echo hello");
expect(result[0].useVsCodeApi).toBe(false);
expect(result[0].terminalName).toBeUndefined();
expect((result[0] as CommandTreeItem).commandString).toBe("echo hello");
expect((result[0] as CommandTreeItem).useVsCodeApi).toBe(false);
expect((result[0] as CommandTreeItem).terminalName).toBeUndefined();
Comment on lines +22 to +24

Choose a reason for hiding this comment

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

medium

These repetitive type casts make the code harder to read. Since you've already asserted the type with toBeInstanceOf, you can introduce a variable to avoid repeated casting. This pattern can be applied to other similar assertions in this file.

For example:

const commandItem = result[0] as CommandTreeItem;
expect(commandItem.commandString).toBe("echo hello");
expect(commandItem.useVsCodeApi).toBe(false);
expect(commandItem.terminalName).toBeUndefined();


expect(result[1]).toBeInstanceOf(CommandTreeItem);
expect(result[1].label).toBe("Test Command 2");
expect(result[1].commandString).toBe("ls -la");
expect(result[1].useVsCodeApi).toBe(false);
expect(result[1].terminalName).toBeUndefined();
expect((result[1] as CommandTreeItem).commandString).toBe("ls -la");
expect((result[1] as CommandTreeItem).useVsCodeApi).toBe(false);
expect((result[1] as CommandTreeItem).terminalName).toBeUndefined();
});

it("should create command tree items with VS Code API and terminal name", () => {
Expand All @@ -44,9 +44,9 @@ describe("command-tree-provider", () => {

expect(result).toHaveLength(1);
expect(result[0].label).toBe("VS Code Command");
expect(result[0].commandString).toBe("workbench.action.openSettings");
expect(result[0].useVsCodeApi).toBe(true);
expect(result[0].terminalName).toBe("settings-terminal");
expect((result[0] as CommandTreeItem).commandString).toBe("workbench.action.openSettings");
expect((result[0] as CommandTreeItem).useVsCodeApi).toBe(true);
expect((result[0] as CommandTreeItem).terminalName).toBe("settings-terminal");
});

it("should handle commands with empty command string", () => {
Expand All @@ -58,8 +58,8 @@ describe("command-tree-provider", () => {
const result = createTreeItemsFromGroup(commands);

expect(result).toHaveLength(2);
expect(result[0].commandString).toBe("");
expect(result[1].commandString).toBe("");
expect((result[0] as CommandTreeItem).commandString).toBe("");
expect((result[1] as CommandTreeItem).commandString).toBe("");
});

it("should handle empty commands array", () => {
Expand Down Expand Up @@ -107,12 +107,35 @@ describe("command-tree-provider", () => {
const result = createTreeItemsFromGroup(commands);

expect(result).toHaveLength(3);
expect(result[0].useVsCodeApi).toBe(false);
expect(result[0].terminalName).toBeUndefined();
expect(result[1].useVsCodeApi).toBe(false);
expect(result[1].terminalName).toBe("test-terminal");
expect(result[2].useVsCodeApi).toBe(true);
expect(result[2].terminalName).toBeUndefined();
expect((result[0] as CommandTreeItem).useVsCodeApi).toBe(false);
expect((result[0] as CommandTreeItem).terminalName).toBeUndefined();
expect((result[1] as CommandTreeItem).useVsCodeApi).toBe(false);
expect((result[1] as CommandTreeItem).terminalName).toBe("test-terminal");
expect((result[2] as CommandTreeItem).useVsCodeApi).toBe(true);
expect((result[2] as CommandTreeItem).terminalName).toBeUndefined();
});

it("should handle nested groups", () => {
const commands: ButtonConfig[] = [
{ name: "Simple Command", command: "echo simple" },
{
name: "Nested Group",
group: [
{ name: "Sub Command 1", command: "echo sub1" },
{ name: "Sub Command 2", command: "echo sub2" },
],
},
];

const result = createTreeItemsFromGroup(commands);

expect(result).toHaveLength(2);
expect(result[0]).toBeInstanceOf(CommandTreeItem);
expect((result[0] as CommandTreeItem).commandString).toBe("echo simple");

expect(result[1]).toBeInstanceOf(GroupTreeItem);
expect(result[1].label).toBe("Nested Group");
expect((result[1] as GroupTreeItem).commands).toHaveLength(2);
});
Comment on lines +118 to 139

Choose a reason for hiding this comment

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

medium

This test case is a great addition for verifying group handling. However, it only tests a single level of grouping. Given the PR's goal is to fix nested group display, it would be more robust to test at least two levels of nesting to ensure the recursive nature of group display works as expected. This would involve checking the children of the created GroupTreeItem.

    it("should handle deeply nested groups", () => {
      const commands: ButtonConfig[] = [
        {
          name: "L1 Group",
          group: [
            { name: "L1 Cmd", command: "echo 1" },
            { name: "L2 Group", group: [{ name: "L2 Cmd", command: "echo 2" }] },
          ],
        },
      ];

      const l1Items = createTreeItemsFromGroup(commands);

      expect(l1Items).toHaveLength(1);
      const l1Group = l1Items[0] as GroupTreeItem;
      expect(l1Group).toBeInstanceOf(GroupTreeItem);
      expect(l1Group.label).toBe("L1 Group");

      const l2Items = createTreeItemsFromGroup(l1Group.commands);
      expect(l2Items).toHaveLength(2);
      const l2Group = l2Items[1] as GroupTreeItem;
      expect(l2Group).toBeInstanceOf(GroupTreeItem);
      expect(l2Group.label).toBe("L2 Group");
      expect(l2Group.commands[0].name).toBe("L2 Cmd");
    });

});

Expand Down
23 changes: 13 additions & 10 deletions src/extension/src/command-tree-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,19 @@ type TreeItem = CommandTreeItem | GroupTreeItem;

export const createTreeItemsFromGroup = (
commands: ButtonConfig[]
): CommandTreeItem[] => {
return commands.map(
(cmd) =>
new CommandTreeItem(
cmd.name,
cmd.command || "",
cmd.useVsCodeApi || false,
cmd.terminalName
)
);
): TreeItem[] => {
return commands.map((cmd) => {
if (cmd.group) {
return new GroupTreeItem(cmd.name, cmd.group);
}

return new CommandTreeItem(
cmd.name,
cmd.command || "",
cmd.useVsCodeApi || false,
cmd.terminalName
);
});
};

export const createRootTreeItems = (buttons: ButtonConfig[]): TreeItem[] => {
Expand Down