|
| 1 | +import { mkdtempSync, realpathSync } from 'node:fs' |
| 2 | +import { |
| 3 | + lstat, |
| 4 | + mkdir, |
| 5 | + readFile, |
| 6 | + rm, |
| 7 | + stat, |
| 8 | + symlink, |
| 9 | + writeFile, |
| 10 | +} from 'node:fs/promises' |
| 11 | +import type * as NodeOs from 'node:os' |
| 12 | +import { tmpdir } from 'node:os' |
| 13 | +import { join } from 'node:path' |
| 14 | + |
| 15 | +import { afterAll, afterEach, describe, expect, it, vi } from 'vitest' |
| 16 | + |
| 17 | +const sharedHome = realpathSync( |
| 18 | + mkdtempSync(join(tmpdir(), 'skills-cli-command-')), |
| 19 | +) |
| 20 | +const commandPath = join(sharedHome, '.local', 'bin', 'skills-desktop') |
| 21 | + |
| 22 | +vi.mock('node:os', async () => { |
| 23 | + const actual = await vi.importActual<typeof NodeOs>('node:os') |
| 24 | + return { |
| 25 | + ...actual, |
| 26 | + homedir: () => sharedHome, |
| 27 | + } |
| 28 | +}) |
| 29 | + |
| 30 | +const servicePromise = (async () => import('./cliCommandService'))() |
| 31 | + |
| 32 | +describe('cliCommandService', () => { |
| 33 | + afterEach(async () => { |
| 34 | + await rm(join(sharedHome, '.local'), { recursive: true, force: true }) |
| 35 | + }) |
| 36 | + |
| 37 | + afterAll(async () => { |
| 38 | + await rm(sharedHome, { recursive: true, force: true }) |
| 39 | + }) |
| 40 | + |
| 41 | + it('reports the command as not installed when ~/.local/bin/skills-desktop is missing', async () => { |
| 42 | + // Arrange |
| 43 | + const { getCliCommandStatus } = await servicePromise |
| 44 | + |
| 45 | + // Act |
| 46 | + const status = await getCliCommandStatus() |
| 47 | + |
| 48 | + // Assert |
| 49 | + expect(status).toEqual({ |
| 50 | + status: 'not-installed', |
| 51 | + commandName: 'skills-desktop', |
| 52 | + commandPath, |
| 53 | + message: 'Command is not installed.', |
| 54 | + }) |
| 55 | + }) |
| 56 | + |
| 57 | + it('installs an executable shim that opens the Skills Desktop bundle', async () => { |
| 58 | + // Arrange |
| 59 | + const { installCliCommand } = await servicePromise |
| 60 | + |
| 61 | + // Act |
| 62 | + const result = await installCliCommand() |
| 63 | + |
| 64 | + // Assert |
| 65 | + expect(result.ok).toBe(true) |
| 66 | + expect(result.status.status).toBe('installed') |
| 67 | + expect(result.message).toBe(`Command installed at ${commandPath}.`) |
| 68 | + expect(await readFile(commandPath, 'utf-8')).toBe(`#!/bin/sh |
| 69 | +# >>> Skills Desktop CLI shim >>> |
| 70 | +# This file is managed by Skills Desktop from Settings. |
| 71 | +exec open -b "io.laststance.skills-desktop" |
| 72 | +# <<< Skills Desktop CLI shim <<< |
| 73 | +`) |
| 74 | + expect((await stat(commandPath)).mode & 0o777).toBe(0o755) |
| 75 | + }) |
| 76 | + |
| 77 | + it('refuses to overwrite an unmanaged file that already uses the command path', async () => { |
| 78 | + // Arrange |
| 79 | + const { installCliCommand } = await servicePromise |
| 80 | + await mkdir(join(sharedHome, '.local', 'bin'), { recursive: true }) |
| 81 | + await writeFile(commandPath, '#!/bin/sh\necho unmanaged\n', 'utf-8') |
| 82 | + |
| 83 | + // Act |
| 84 | + const result = await installCliCommand() |
| 85 | + |
| 86 | + // Assert |
| 87 | + expect(result).toEqual({ |
| 88 | + ok: false, |
| 89 | + status: { |
| 90 | + status: 'blocked', |
| 91 | + commandName: 'skills-desktop', |
| 92 | + commandPath, |
| 93 | + message: `${commandPath} is already occupied by an unmanaged file.`, |
| 94 | + }, |
| 95 | + message: `${commandPath} is already occupied by an unmanaged file.`, |
| 96 | + }) |
| 97 | + expect(await readFile(commandPath, 'utf-8')).toBe( |
| 98 | + '#!/bin/sh\necho unmanaged\n', |
| 99 | + ) |
| 100 | + }) |
| 101 | + |
| 102 | + it('refuses to remove a user-edited script that contains the managed markers', async () => { |
| 103 | + // Arrange |
| 104 | + const { removeCliCommand } = await servicePromise |
| 105 | + const editedScript = `#!/bin/sh |
| 106 | +# >>> Skills Desktop CLI shim >>> |
| 107 | +# This file is managed by Skills Desktop from Settings. |
| 108 | +echo "custom logic" |
| 109 | +exec open -b "io.laststance.skills-desktop" |
| 110 | +# <<< Skills Desktop CLI shim <<< |
| 111 | +` |
| 112 | + await mkdir(join(sharedHome, '.local', 'bin'), { recursive: true }) |
| 113 | + await writeFile(commandPath, editedScript, 'utf-8') |
| 114 | + |
| 115 | + // Act |
| 116 | + const result = await removeCliCommand() |
| 117 | + |
| 118 | + // Assert |
| 119 | + expect(result).toEqual({ |
| 120 | + ok: false, |
| 121 | + status: { |
| 122 | + status: 'blocked', |
| 123 | + commandName: 'skills-desktop', |
| 124 | + commandPath, |
| 125 | + message: `${commandPath} is already occupied by an unmanaged file.`, |
| 126 | + }, |
| 127 | + message: `${commandPath} is already occupied by an unmanaged file.`, |
| 128 | + }) |
| 129 | + expect(await readFile(commandPath, 'utf-8')).toBe(editedScript) |
| 130 | + }) |
| 131 | + |
| 132 | + it('removes the managed shim and leaves the command path missing afterward', async () => { |
| 133 | + // Arrange |
| 134 | + const { installCliCommand, removeCliCommand } = await servicePromise |
| 135 | + await installCliCommand() |
| 136 | + |
| 137 | + // Act |
| 138 | + const result = await removeCliCommand() |
| 139 | + |
| 140 | + // Assert |
| 141 | + expect(result).toEqual({ |
| 142 | + ok: true, |
| 143 | + status: { |
| 144 | + status: 'not-installed', |
| 145 | + commandName: 'skills-desktop', |
| 146 | + commandPath, |
| 147 | + message: 'Command is not installed.', |
| 148 | + }, |
| 149 | + message: 'Command removed.', |
| 150 | + }) |
| 151 | + await expect(lstat(commandPath)).rejects.toMatchObject({ code: 'ENOENT' }) |
| 152 | + }) |
| 153 | + |
| 154 | + it('refuses to remove an unmanaged symlink that occupies the command path', async () => { |
| 155 | + // Arrange |
| 156 | + const { removeCliCommand } = await servicePromise |
| 157 | + await mkdir(join(sharedHome, '.local', 'bin'), { recursive: true }) |
| 158 | + await symlink('/usr/bin/open', commandPath) |
| 159 | + |
| 160 | + // Act |
| 161 | + const result = await removeCliCommand() |
| 162 | + |
| 163 | + // Assert |
| 164 | + expect(result).toEqual({ |
| 165 | + ok: false, |
| 166 | + status: { |
| 167 | + status: 'blocked', |
| 168 | + commandName: 'skills-desktop', |
| 169 | + commandPath, |
| 170 | + message: `${commandPath} is already occupied by an unmanaged symlink.`, |
| 171 | + }, |
| 172 | + message: `${commandPath} is already occupied by an unmanaged symlink.`, |
| 173 | + }) |
| 174 | + expect((await lstat(commandPath)).isSymbolicLink()).toBe(true) |
| 175 | + }) |
| 176 | +}) |
0 commit comments