|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | + |
| 3 | +vi.mock('vscode', () => import('../__mocks__/vscode.js')); |
| 4 | + |
| 5 | +import * as vscode from 'vscode'; |
| 6 | +import { |
| 7 | + startHereItems, |
| 8 | + showStartHereMenu, |
| 9 | + registerStartHere, |
| 10 | + resetStartHere, |
| 11 | + StartHereStatusBar, |
| 12 | + START_HERE_RETIRED_KEY, |
| 13 | +} from '../startHere'; |
| 14 | + |
| 15 | +type FakeContext = ConstructorParameters<typeof StartHereStatusBar>[0]; |
| 16 | + |
| 17 | +// Minimal ExtensionContext stand-in backed by a plain key/value store, enough for the |
| 18 | +// globalState get/update the retirement gate uses. |
| 19 | +function makeContext(initial: Record<string, unknown> = {}): { |
| 20 | + context: FakeContext; |
| 21 | + store: Record<string, unknown>; |
| 22 | +} { |
| 23 | + const store: Record<string, unknown> = { ...initial }; |
| 24 | + const context = { |
| 25 | + globalState: { |
| 26 | + get: (key: string, def?: unknown) => (key in store ? store[key] : def), |
| 27 | + update: async (key: string, value: unknown) => { |
| 28 | + store[key] = value; |
| 29 | + }, |
| 30 | + }, |
| 31 | + } as unknown as FakeContext; |
| 32 | + return { context, store }; |
| 33 | +} |
| 34 | + |
| 35 | +const STATUS_COMMAND = 'gemstone.startHere.fromStatusBar'; |
| 36 | + |
| 37 | +describe('startHereItems', () => { |
| 38 | + it('offers browse, search, workspace, and the tour, each dispatching a real command', () => { |
| 39 | + const commands = startHereItems().map((i) => i.command); |
| 40 | + expect(commands).toEqual([ |
| 41 | + 'gemstone.findClass', |
| 42 | + 'gemstone.search', |
| 43 | + 'gemstone.openWorkspace', |
| 44 | + 'gemstone.openWalkthrough', |
| 45 | + ]); |
| 46 | + for (const item of startHereItems()) { |
| 47 | + expect(item.label.length).toBeGreaterThan(0); |
| 48 | + expect(item.detail.length).toBeGreaterThan(0); |
| 49 | + } |
| 50 | + }); |
| 51 | +}); |
| 52 | + |
| 53 | +describe('showStartHereMenu', () => { |
| 54 | + beforeEach(() => vi.clearAllMocks()); |
| 55 | + |
| 56 | + it('runs the picked item’s command', async () => { |
| 57 | + vi.mocked(vscode.window.showQuickPick).mockResolvedValueOnce({ |
| 58 | + command: 'gemstone.search', |
| 59 | + } as never); |
| 60 | + await showStartHereMenu(); |
| 61 | + expect(vscode.commands.executeCommand).toHaveBeenCalledWith('gemstone.search'); |
| 62 | + }); |
| 63 | + |
| 64 | + it('does nothing when the menu is dismissed', async () => { |
| 65 | + vi.mocked(vscode.window.showQuickPick).mockResolvedValueOnce(undefined); |
| 66 | + await showStartHereMenu(); |
| 67 | + expect(vscode.commands.executeCommand).not.toHaveBeenCalled(); |
| 68 | + }); |
| 69 | +}); |
| 70 | + |
| 71 | +describe('StartHereStatusBar', () => { |
| 72 | + beforeEach(() => vi.clearAllMocks()); |
| 73 | + |
| 74 | + function setup(initial: Record<string, unknown> = {}) { |
| 75 | + const { context, store } = makeContext(initial); |
| 76 | + const bar = new StartHereStatusBar(context); |
| 77 | + const disposables = bar.register(); |
| 78 | + const item = vi.mocked(vscode.window.createStatusBarItem).mock.results.at(-1)!.value; |
| 79 | + return { bar, item, store, disposables }; |
| 80 | + } |
| 81 | + |
| 82 | + it('shows on connect when not retired', () => { |
| 83 | + const { bar, item } = setup(); |
| 84 | + bar.showForConnection(); |
| 85 | + expect(item.show).toHaveBeenCalledTimes(1); |
| 86 | + }); |
| 87 | + |
| 88 | + it('does not show on connect once retired', () => { |
| 89 | + const { bar, item } = setup({ [START_HERE_RETIRED_KEY]: true }); |
| 90 | + bar.showForConnection(); |
| 91 | + expect(item.show).not.toHaveBeenCalled(); |
| 92 | + }); |
| 93 | + |
| 94 | + it('hides on disconnection without retiring', () => { |
| 95 | + const { bar, item, store } = setup(); |
| 96 | + bar.hideForDisconnection(); |
| 97 | + expect(item.hide).toHaveBeenCalledTimes(1); |
| 98 | + expect(store[START_HERE_RETIRED_KEY]).toBeUndefined(); |
| 99 | + }); |
| 100 | + |
| 101 | + function clickButton() { |
| 102 | + const handler = vi |
| 103 | + .mocked(vscode.commands.registerCommand) |
| 104 | + .mock.calls.find((c) => c[0] === STATUS_COMMAND)?.[1] as () => Promise<void>; |
| 105 | + expect(handler).toBeDefined(); |
| 106 | + return handler(); |
| 107 | + } |
| 108 | + |
| 109 | + it('opens the hub on click and offers a Hide entry', async () => { |
| 110 | + setup(); |
| 111 | + let offered: Array<{ command: string }> = []; |
| 112 | + vi.mocked(vscode.window.showQuickPick).mockImplementationOnce(async (items: unknown) => { |
| 113 | + offered = items as Array<{ command: string }>; |
| 114 | + return undefined; |
| 115 | + }); |
| 116 | + await clickButton(); |
| 117 | + expect(vscode.window.showQuickPick).toHaveBeenCalledTimes(1); |
| 118 | + expect(offered.some((i) => i.command === '__startHere.hide')).toBe(true); |
| 119 | + }); |
| 120 | + |
| 121 | + it('does NOT hide when the hub is dismissed (clicked away)', async () => { |
| 122 | + const { item, store } = setup(); |
| 123 | + vi.mocked(vscode.window.showQuickPick).mockResolvedValueOnce(undefined); |
| 124 | + await clickButton(); |
| 125 | + expect(store[START_HERE_RETIRED_KEY]).toBeUndefined(); |
| 126 | + expect(item.hide).not.toHaveBeenCalled(); |
| 127 | + }); |
| 128 | + |
| 129 | + it('hides persistently only when the Hide entry is chosen', async () => { |
| 130 | + const { item, store } = setup(); |
| 131 | + vi.mocked(vscode.window.showQuickPick).mockResolvedValueOnce({ |
| 132 | + command: '__startHere.hide', |
| 133 | + } as never); |
| 134 | + await clickButton(); |
| 135 | + expect(store[START_HERE_RETIRED_KEY]).toBe(true); |
| 136 | + expect(item.hide).toHaveBeenCalled(); |
| 137 | + // A command is never dispatched for the Hide action. |
| 138 | + expect(vscode.commands.executeCommand).not.toHaveBeenCalled(); |
| 139 | + }); |
| 140 | + |
| 141 | + it('does not offer the Hide entry from the plain Command Palette menu', async () => { |
| 142 | + let offered: Array<{ command: string }> = []; |
| 143 | + vi.mocked(vscode.window.showQuickPick).mockImplementationOnce(async (items: unknown) => { |
| 144 | + offered = items as Array<{ command: string }>; |
| 145 | + return undefined; |
| 146 | + }); |
| 147 | + await showStartHereMenu(); |
| 148 | + expect(offered.some((i) => i.command === '__startHere.hide')).toBe(false); |
| 149 | + }); |
| 150 | +}); |
| 151 | + |
| 152 | +describe('resetStartHere', () => { |
| 153 | + it('un-retires the button so it shows again on the next connect', async () => { |
| 154 | + const { context, store } = makeContext({ [START_HERE_RETIRED_KEY]: true }); |
| 155 | + await resetStartHere(context); |
| 156 | + expect(store[START_HERE_RETIRED_KEY]).toBeUndefined(); |
| 157 | + }); |
| 158 | +}); |
| 159 | + |
| 160 | +describe('registerStartHere', () => { |
| 161 | + beforeEach(() => vi.clearAllMocks()); |
| 162 | + |
| 163 | + it('registers the gemstone.startHere command', () => { |
| 164 | + registerStartHere(); |
| 165 | + expect(vscode.commands.registerCommand).toHaveBeenCalledWith( |
| 166 | + 'gemstone.startHere', |
| 167 | + expect.any(Function), |
| 168 | + ); |
| 169 | + }); |
| 170 | +}); |
0 commit comments