|
| 1 | +import { existsSync, mkdirSync, mkdtempSync, readFileSync, writeFileSync } from 'node:fs'; |
| 2 | +import { tmpdir } from 'node:os'; |
| 3 | +import { join } from 'node:path'; |
| 4 | +import type { |
| 5 | + InstalledLinkCodePlugin, |
| 6 | + LinkCodePluginManifest, |
| 7 | + LinkCodePluginRelease, |
| 8 | +} from '@linkcode/schema'; |
| 9 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 10 | +import { makePluginTmpDir, pluginPackageDir, pluginRegistryPath } from '../plugin-store/paths'; |
| 11 | +import { DaemonLinkCodePluginStore } from '../plugin-store/store'; |
| 12 | +import { createInMemoryVault } from './fixtures/in-memory-vault'; |
| 13 | + |
| 14 | +const mocks = vi.hoisted(() => ({ |
| 15 | + downloadVerified: vi.fn(), |
| 16 | + tarExtract: vi.fn(), |
| 17 | +})); |
| 18 | + |
| 19 | +vi.mock('@linkcode/assets', () => ({ downloadVerified: mocks.downloadVerified })); |
| 20 | +vi.mock('tar', () => ({ extract: mocks.tarExtract })); |
| 21 | + |
| 22 | +let savedHome: string | undefined; |
| 23 | + |
| 24 | +beforeEach(() => { |
| 25 | + savedHome = process.env.HOME; |
| 26 | + process.env.HOME = mkdtempSync(join(tmpdir(), 'linkcode-plugin-store-')); |
| 27 | + process.env.LINKCODE_CHANNEL = 'release'; |
| 28 | + mocks.downloadVerified.mockReset().mockResolvedValue(undefined); |
| 29 | + mocks.tarExtract.mockReset(); |
| 30 | +}); |
| 31 | + |
| 32 | +afterEach(() => { |
| 33 | + process.env.HOME = savedHome; |
| 34 | + delete process.env.LINKCODE_CHANNEL; |
| 35 | + vi.restoreAllMocks(); |
| 36 | +}); |
| 37 | + |
| 38 | +function manifest(version: string, componentName = 'latex'): LinkCodePluginManifest { |
| 39 | + return { |
| 40 | + manifestVersion: 1, |
| 41 | + id: 'arcbox/latex', |
| 42 | + version, |
| 43 | + keywords: [], |
| 44 | + components: [{ kind: 'skill', name: componentName, entry: 'skills/latex/SKILL.md' }], |
| 45 | + assets: [], |
| 46 | + }; |
| 47 | +} |
| 48 | + |
| 49 | +function record(version: string): InstalledLinkCodePlugin { |
| 50 | + return { |
| 51 | + id: 'arcbox/latex', |
| 52 | + version, |
| 53 | + marketplaceId: 'linkcode-official', |
| 54 | + integrity: 'sha256-7bZ8YaunaCifbaRByeb1I8+v9PiypXCFI+8pxUP46I4=', |
| 55 | + enabled: true, |
| 56 | + path: pluginPackageDir('arcbox/latex', version), |
| 57 | + }; |
| 58 | +} |
| 59 | + |
| 60 | +function writePackage(installed: InstalledLinkCodePlugin, packageManifest: unknown): void { |
| 61 | + mkdirSync(installed.path, { recursive: true }); |
| 62 | + writeFileSync(join(installed.path, 'manifest.json'), JSON.stringify(packageManifest)); |
| 63 | +} |
| 64 | + |
| 65 | +function writeRegistry(records: InstalledLinkCodePlugin[]): void { |
| 66 | + const path = pluginRegistryPath(); |
| 67 | + mkdirSync(join(path, '..'), { recursive: true }); |
| 68 | + writeFileSync(path, JSON.stringify(records)); |
| 69 | +} |
| 70 | + |
| 71 | +function settingsManifest(version: string): LinkCodePluginManifest { |
| 72 | + return { |
| 73 | + ...manifest(version), |
| 74 | + settings: { |
| 75 | + account: { type: 'string', label: 'Account' }, |
| 76 | + authcode: { type: 'password', label: 'Authorization code', secret: true }, |
| 77 | + }, |
| 78 | + }; |
| 79 | +} |
| 80 | + |
| 81 | +describe('DaemonLinkCodePluginStore', () => { |
| 82 | + it('allocates a unique staging directory for concurrent installs of the same release', () => { |
| 83 | + expect(makePluginTmpDir('arcbox/latex', '0.2.0')).not.toBe( |
| 84 | + makePluginTmpDir('arcbox/latex', '0.2.0'), |
| 85 | + ); |
| 86 | + }); |
| 87 | + |
| 88 | + it('uses the most recently installed record for legacy duplicate plugin ids', () => { |
| 89 | + const v1 = record('0.1.0'); |
| 90 | + const v2 = record('0.2.0'); |
| 91 | + writePackage(v1, { ...manifest('0.1.0'), futureManifestField: 'ignored' }); |
| 92 | + writePackage(v2, manifest('0.2.0')); |
| 93 | + writeRegistry([v1, v2]); |
| 94 | + |
| 95 | + const store = new DaemonLinkCodePluginStore(createInMemoryVault()); |
| 96 | + |
| 97 | + expect(store.list()).toMatchObject([{ installed: { version: '0.2.0' } }]); |
| 98 | + expect(store.get('arcbox/latex')?.installed.version).toBe('0.2.0'); |
| 99 | + }); |
| 100 | + |
| 101 | + it('replaces an older package and returns the verified on-disk manifest', async () => { |
| 102 | + const v0 = record('0.0.1'); |
| 103 | + const v1 = record('0.1.0'); |
| 104 | + writePackage(v0, manifest('0.0.1')); |
| 105 | + writePackage(v1, manifest('0.1.0')); |
| 106 | + writeRegistry([v0, v1]); |
| 107 | + mocks.tarExtract.mockImplementation(({ cwd }: { cwd: string }) => { |
| 108 | + writeFileSync(join(cwd, 'manifest.json'), JSON.stringify(manifest('0.2.0', 'package-skill'))); |
| 109 | + }); |
| 110 | + const release = { |
| 111 | + manifest: manifest('0.2.0', 'index-skill'), |
| 112 | + artifact: { |
| 113 | + urls: ['https://plugins.example/arcbox-latex-0.2.0.tgz'], |
| 114 | + integrity: 'sha256-7bZ8YaunaCifbaRByeb1I8+v9PiypXCFI+8pxUP46I4=', |
| 115 | + format: 'tgz', |
| 116 | + }, |
| 117 | + } satisfies LinkCodePluginRelease; |
| 118 | + const store = new DaemonLinkCodePluginStore(createInMemoryVault()); |
| 119 | + |
| 120 | + const installed = await store.install(release, 'linkcode-official'); |
| 121 | + |
| 122 | + expect(installed.manifest.components[0]?.name).toBe('package-skill'); |
| 123 | + expect(store.get('arcbox/latex')?.manifest.components[0]?.name).toBe('package-skill'); |
| 124 | + expect(existsSync(v0.path)).toBe(false); |
| 125 | + expect(existsSync(v1.path)).toBe(false); |
| 126 | + expect(JSON.parse(readFileSync(pluginRegistryPath(), 'utf8'))).toMatchObject([ |
| 127 | + { id: 'arcbox/latex', version: '0.2.0' }, |
| 128 | + ]); |
| 129 | + }); |
| 130 | + |
| 131 | + it('rolls back config and secret changes when the vault rejects a settings update', () => { |
| 132 | + const installed = record('0.1.0'); |
| 133 | + writePackage(installed, settingsManifest('0.1.0')); |
| 134 | + writeRegistry([installed]); |
| 135 | + const baseVault = createInMemoryVault(); |
| 136 | + const store = new DaemonLinkCodePluginStore(baseVault); |
| 137 | + store.setSettings('arcbox/latex', { |
| 138 | + set: { account: 'old@example.com', authcode: 'old-secret' }, |
| 139 | + }); |
| 140 | + const vaultFailure = new Error('vault unavailable'); |
| 141 | + const flakyVault = { |
| 142 | + ...baseVault, |
| 143 | + namespace(name: Parameters<typeof baseVault.namespace>[0]) { |
| 144 | + const secrets = baseVault.namespace(name); |
| 145 | + if (name !== 'plugin') return secrets; |
| 146 | + return { |
| 147 | + ...secrets, |
| 148 | + set(key: string, value: string) { |
| 149 | + if (key === 'arcbox/latex.authcode' && value === 'new-secret') throw vaultFailure; |
| 150 | + secrets.set(key, value); |
| 151 | + }, |
| 152 | + }; |
| 153 | + }, |
| 154 | + }; |
| 155 | + |
| 156 | + expect(() => |
| 157 | + new DaemonLinkCodePluginStore(flakyVault).setSettings('arcbox/latex', { |
| 158 | + set: { account: 'new@example.com', authcode: 'new-secret' }, |
| 159 | + }), |
| 160 | + ).toThrow(vaultFailure); |
| 161 | + |
| 162 | + expect(store.getSettings('arcbox/latex')).toEqual({ |
| 163 | + account: 'old@example.com', |
| 164 | + authcode: 'old-secret', |
| 165 | + }); |
| 166 | + }); |
| 167 | +}); |
0 commit comments