Jelajahi Sumber

修复升级器解析插件配置

LSamien 2 bulan lalu
induk
melakukan
80437cc967
2 mengubah file dengan 24 tambahan dan 4 penghapusan
  1. 8 3
      bin/hermes-yaoyao-updater.mjs
  2. 16 1
      tests/updater.test.ts

+ 8 - 3
bin/hermes-yaoyao-updater.mjs

@@ -124,10 +124,15 @@ function stopDashboard() {
   try { run('hermes', ['dashboard', '--stop'], { timeout: 30_000 }) } catch { /* already stopped */ }
 }
 
-function enabledPlugins() {
-  const raw = run('hermes', ['config', 'get', 'plugins.enabled']).trim()
+export function enabledPlugins(runCommand = run) {
+  const raw = runCommand('hermes', ['config', 'get', '--json', 'plugins.enabled']).trim()
   if (!raw) return []
-  const value = JSON.parse(raw)
+  let value
+  try {
+    value = JSON.parse(raw)
+  } catch {
+    fail('Hermes plugins.enabled 未返回有效 JSON')
+  }
   if (!Array.isArray(value) || value.some(item => typeof item !== 'string')) {
     fail('Hermes plugins.enabled 不是字符串数组')
   }

+ 16 - 1
tests/updater.test.ts

@@ -2,7 +2,7 @@ import { lstatSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync
 import { tmpdir } from 'node:os'
 import { join } from 'node:path'
 import { afterEach, describe, expect, it } from 'vitest'
-import { backupPlugin, currentTarget, formatCommandFailure, restorePlugin, switchCurrent, validateManifest } from '../bin/hermes-yaoyao-updater.mjs'
+import { backupPlugin, currentTarget, enabledPlugins, formatCommandFailure, restorePlugin, switchCurrent, validateManifest } from '../bin/hermes-yaoyao-updater.mjs'
 
 const roots: string[] = []
 afterEach(() => {
@@ -48,6 +48,21 @@ describe('standalone updater primitives', () => {
     expect(message).not.toContain('\u001B')
   })
 
+  it('requests Hermes plugin configuration as JSON', () => {
+    const calls: Array<{ command: string, args: string[] }> = []
+    const plugins = enabledPlugins((command: string, args: string[]) => {
+      calls.push({ command, args })
+      return '["yaoyao", "other"]\n'
+    })
+
+    expect(plugins).toEqual(['yaoyao', 'other'])
+    expect(calls).toEqual([{
+      command: 'hermes',
+      args: ['config', 'get', '--json', 'plugins.enabled'],
+    }])
+    expect(() => enabledPlugins(() => '- yaoyao\n')).toThrow('未返回有效 JSON')
+  })
+
   it('backs up and restores plugin code without touching durable plugin data', () => {
     const root = mkdtempSync(join(tmpdir(), 'hermes-updater-plugin-'))
     roots.push(root)