mirror of
https://wget.la/https://github.com/Wxw-Gu/WechatExplorer
synced 2026-12-01 01:05:30 +08:00
feat: windows自动化下统一逻辑
This commit is contained in:
1 parent
915ee73f95
commit
450668f5e6
14 files changed
+1907
-64
No files matched your search
@@ -0,0 +1,190 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
import {
|
||||
isDefinitelyBlocked,
|
||||
isEntitlementBlocked,
|
||||
resolveExitNoticeGate,
|
||||
resolveSwitchPresentation
|
||||
} from '../../src/main/services/exit-notice-gate'
|
||||
import type { MessageInsertCapability } from '../../src/main/services/msg-insert-capability-service'
|
||||
|
||||
/** 开关呈现:授权不成立时,两个开关必须同时显示关闭。 */
|
||||
|
||||
function capability(over: Partial<MessageInsertCapability>): MessageInsertCapability {
|
||||
return {
|
||||
entitlementState: 'valid',
|
||||
nativeComponentState: 'ready',
|
||||
databaseReady: true,
|
||||
canEnable: true,
|
||||
canDisable: true,
|
||||
reasonCode: 'CAPABILITY_READY',
|
||||
message: '授权与组件均已就绪',
|
||||
checkedAt: 0,
|
||||
...over
|
||||
}
|
||||
}
|
||||
|
||||
describe('退群内提示开关判定', () => {
|
||||
it('授权有效 + 组件就绪 + 库已连接 + 用户开着 → 显示开、确实在跑', () => {
|
||||
const d = resolveExitNoticeGate(capability({}), true)
|
||||
expect(d).toEqual({ desiredEnabled: true, effectiveEnabled: true, persistDisable: false })
|
||||
})
|
||||
|
||||
it('用户本来关着 → 显示关,不落盘', () => {
|
||||
const d = resolveExitNoticeGate(capability({}), false)
|
||||
expect(d).toEqual({ desiredEnabled: false, effectiveEnabled: false, persistDisable: false })
|
||||
})
|
||||
|
||||
it('授权过期(invalid)→ 显示关闭,并把本地开关落盘关掉', () => {
|
||||
const cap = capability({
|
||||
entitlementState: 'invalid',
|
||||
canEnable: false,
|
||||
reasonCode: 'LICENSE_EXPIRED',
|
||||
message: '授权已过期,请重新激活后再开启'
|
||||
})
|
||||
const d = resolveExitNoticeGate(cap, true)
|
||||
expect(d.desiredEnabled).toBe(false)
|
||||
expect(d.effectiveEnabled).toBe(false)
|
||||
expect(d.persistDisable).toBe(true)
|
||||
})
|
||||
|
||||
it('授权服务探测失败(unavailable)→ 显示关闭,但不改写用户设置', () => {
|
||||
const cap = capability({
|
||||
entitlementState: 'unavailable',
|
||||
canEnable: false,
|
||||
reasonCode: 'LICENSE_UNAVAILABLE',
|
||||
message: '暂时无法确认授权状态'
|
||||
})
|
||||
const d = resolveExitNoticeGate(cap, true)
|
||||
expect(d.desiredEnabled).toBe(false)
|
||||
expect(d.effectiveEnabled).toBe(false)
|
||||
/* 微信刚启动时 4290 还没起来,这时落盘会把正常正常用户的开关误关 */
|
||||
expect(d.persistDisable).toBe(false)
|
||||
expect(isDefinitelyBlocked(cap)).toBe(false)
|
||||
expect(isEntitlementBlocked(cap)).toBe(true)
|
||||
})
|
||||
|
||||
it('组件缺失 → 显示关闭,落盘关掉(OSS 包的典型状态)', () => {
|
||||
const cap = capability({
|
||||
nativeComponentState: 'missing',
|
||||
canEnable: false,
|
||||
reasonCode: 'COMPONENT_MISSING',
|
||||
message: '未安装发送能力组件'
|
||||
})
|
||||
const d = resolveExitNoticeGate(cap, true)
|
||||
expect(d.desiredEnabled).toBe(false)
|
||||
expect(d.persistDisable).toBe(true)
|
||||
})
|
||||
|
||||
it('组件架构不匹配 → 显示关闭并落盘', () => {
|
||||
const cap = capability({
|
||||
nativeComponentState: 'incompatible',
|
||||
canEnable: false,
|
||||
reasonCode: 'COMPONENT_INVALID'
|
||||
})
|
||||
expect(resolveExitNoticeGate(cap, true).persistDisable).toBe(true)
|
||||
})
|
||||
|
||||
it('授权与组件都就绪、只是微信库没连 → 保留用户意愿,不显示成已生效', () => {
|
||||
const cap = capability({
|
||||
databaseReady: false,
|
||||
canEnable: false,
|
||||
reasonCode: 'DATABASE_UNAVAILABLE',
|
||||
message: '微信数据库尚未连接'
|
||||
})
|
||||
const d = resolveExitNoticeGate(cap, true)
|
||||
expect(d.desiredEnabled).toBe(true)
|
||||
expect(d.effectiveEnabled).toBe(false)
|
||||
/* 库连上是暂时的,不能因此把人家的开关关掉 */
|
||||
expect(d.persistDisable).toBe(false)
|
||||
})
|
||||
|
||||
it('平台不支持(公开版/非 Windows-mac 包)→ 显示关闭并落盘', () => {
|
||||
const cap = capability({
|
||||
entitlementState: 'unsupported',
|
||||
canEnable: false,
|
||||
reasonCode: 'UNSUPPORTED_PLATFORM',
|
||||
databaseReady: false
|
||||
})
|
||||
const d = resolveExitNoticeGate(cap, true)
|
||||
expect(d.desiredEnabled).toBe(false)
|
||||
expect(d.persistDisable).toBe(true)
|
||||
})
|
||||
|
||||
it('授权正在验证(pending)→ 先按关闭显示,不改写设置', () => {
|
||||
const cap = capability({
|
||||
entitlementState: 'pending',
|
||||
canEnable: false,
|
||||
reasonCode: 'LICENSE_PENDING'
|
||||
})
|
||||
const d = resolveExitNoticeGate(cap, true)
|
||||
expect(d.desiredEnabled).toBe(false)
|
||||
expect(d.persistDisable).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('两个微信内开关的呈现(防撤回 / 退群提示必须一致)', () => {
|
||||
const expired = capability({
|
||||
entitlementState: 'invalid',
|
||||
canEnable: false,
|
||||
reasonCode: 'LICENSE_EXPIRED'
|
||||
})
|
||||
const unavailable = capability({
|
||||
entitlementState: 'unavailable',
|
||||
canEnable: false,
|
||||
reasonCode: 'LICENSE_UNAVAILABLE'
|
||||
})
|
||||
const noComponent = capability({
|
||||
nativeComponentState: 'missing',
|
||||
canEnable: false,
|
||||
reasonCode: 'COMPONENT_MISSING'
|
||||
})
|
||||
|
||||
it('报障场景:库里 recall=1 且本地退群=true,没有授权 → 两个都显示关闭', () => {
|
||||
for (const cap of [expired, unavailable, noComponent]) {
|
||||
expect(resolveSwitchPresentation('recall_notice', true, cap)).toEqual({
|
||||
desiredEnabled: false,
|
||||
effectiveEnabled: false
|
||||
})
|
||||
expect(resolveSwitchPresentation('group_exit_notice', true, cap)).toEqual({
|
||||
desiredEnabled: false,
|
||||
effectiveEnabled: false
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
it('授权与组件都就绪、库已连接 → 两个都保持用户设置', () => {
|
||||
const ok = capability({})
|
||||
expect(resolveSwitchPresentation('recall_notice', true, ok)).toEqual({
|
||||
desiredEnabled: true,
|
||||
effectiveEnabled: true
|
||||
})
|
||||
expect(resolveSwitchPresentation('group_exit_notice', true, ok)).toEqual({
|
||||
desiredEnabled: true,
|
||||
effectiveEnabled: true
|
||||
})
|
||||
})
|
||||
|
||||
it('只是微信库没连 → 保留用户意愿但 effective 为 false', () => {
|
||||
const noDb = capability({
|
||||
databaseReady: false,
|
||||
canEnable: false,
|
||||
reasonCode: 'DATABASE_UNAVAILABLE'
|
||||
})
|
||||
expect(resolveSwitchPresentation('recall_notice', true, noDb)).toEqual({
|
||||
desiredEnabled: true,
|
||||
effectiveEnabled: false
|
||||
})
|
||||
expect(resolveSwitchPresentation('group_exit_notice', true, noDb)).toEqual({
|
||||
desiredEnabled: true,
|
||||
effectiveEnabled: false
|
||||
})
|
||||
})
|
||||
|
||||
it('用户本来就关着 + 无授权 → 依然是关闭,不会被"授权没问题"之外的逻辑打开', () => {
|
||||
expect(resolveSwitchPresentation('recall_notice', false, expired).desiredEnabled).toBe(false)
|
||||
expect(resolveSwitchPresentation('group_exit_notice', false, expired).desiredEnabled).toBe(
|
||||
false
|
||||
)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,134 @@
|
||||
import crypto from 'node:crypto'
|
||||
import fs from 'node:fs'
|
||||
import os from 'node:os'
|
||||
import path from 'node:path'
|
||||
|
||||
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
|
||||
|
||||
import {
|
||||
resetComponentCache,
|
||||
resolveExternalComponent
|
||||
} from '../../src/main/services/external-component-locator'
|
||||
|
||||
/** 组件发现必须 fail-closed:指针/目录/哈希任一不对都当没装。 */
|
||||
|
||||
let tmpRoot = ''
|
||||
let localAppData = ''
|
||||
|
||||
function writeFile(filePath: string, content: Buffer | string): Buffer {
|
||||
fs.mkdirSync(path.dirname(filePath), { recursive: true })
|
||||
const buf = Buffer.isBuffer(content) ? content : Buffer.from(content)
|
||||
fs.writeFileSync(filePath, buf)
|
||||
return buf
|
||||
}
|
||||
|
||||
function sha256(buf: Buffer): string {
|
||||
return crypto.createHash('sha256').update(buf).digest('hex')
|
||||
}
|
||||
|
||||
function buildBundle(options: { dirty?: boolean; corruptHash?: boolean } = {}): string {
|
||||
const versionDir = path.join(localAppData, 'TraceMemo', 'Pro', 'windows-x64', '1.0.0')
|
||||
const wcdbApi = writeFile(path.join(versionDir, 'wcdb_api.dll'), Buffer.alloc(2048, 0x11))
|
||||
const wcdb = writeFile(path.join(versionDir, 'WCDB.dll'), Buffer.alloc(4096, 0x22))
|
||||
const helper = writeFile(path.join(versionDir, 'tm-wcdb-config-helper.exe'), Buffer.alloc(1024, 0x33))
|
||||
|
||||
const manifest = {
|
||||
schemaVersion: 1,
|
||||
componentVersion: '1.0.0',
|
||||
worktreeDirty: options.dirty === true,
|
||||
files: [
|
||||
{ path: 'wcdb_api.dll', size: wcdbApi.length, sha256: sha256(wcdbApi) },
|
||||
{ path: 'WCDB.dll', size: wcdb.length, sha256: sha256(wcdb) },
|
||||
{
|
||||
path: 'tm-wcdb-config-helper.exe',
|
||||
size: helper.length,
|
||||
sha256: options.corruptHash ? 'a'.repeat(64) : sha256(helper)
|
||||
}
|
||||
]
|
||||
}
|
||||
writeFile(path.join(versionDir, 'manifest.json'), JSON.stringify(manifest, null, 2))
|
||||
writeFile(
|
||||
path.join(localAppData, 'TraceMemo', 'Pro', 'windows-x64', 'current.json'),
|
||||
JSON.stringify({ version: '1.0.0', dir: versionDir }, null, 2)
|
||||
)
|
||||
return versionDir
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
tmpRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'tm-locator-'))
|
||||
localAppData = path.join(tmpRoot, 'LocalAppData')
|
||||
fs.mkdirSync(localAppData, { recursive: true })
|
||||
resetComponentCache()
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
fs.rmSync(tmpRoot, { recursive: true, force: true })
|
||||
resetComponentCache()
|
||||
})
|
||||
|
||||
const env = (): NodeJS.ProcessEnv => ({ LOCALAPPDATA: localAppData })
|
||||
|
||||
describe('resolveExternalComponent', () => {
|
||||
it('没有 current.json 时返回 null(= 组件没装)', () => {
|
||||
expect(resolveExternalComponent(env(), 'win32')).toBeNull()
|
||||
})
|
||||
|
||||
it('非 Windows 直接返回 null', () => {
|
||||
buildBundle()
|
||||
expect(resolveExternalComponent(env(), 'darwin')).toBeNull()
|
||||
})
|
||||
|
||||
it('校验通过时返回 helper 路径与版本', () => {
|
||||
const dir = buildBundle()
|
||||
const found = resolveExternalComponent(env(), 'win32')
|
||||
expect(found).not.toBeNull()
|
||||
expect(found?.helperPath).toBe(path.join(dir, 'tm-wcdb-config-helper.exe'))
|
||||
expect(found?.version).toBe('1.0.0')
|
||||
expect(found?.dirty).toBe(false)
|
||||
})
|
||||
|
||||
it('dirty 标记透传(内部调试构建)', () => {
|
||||
buildBundle({ dirty: true })
|
||||
expect(resolveExternalComponent(env(), 'win32')?.dirty).toBe(true)
|
||||
})
|
||||
|
||||
it('哈希不符时当作没装(不能返回可疑路径)', () => {
|
||||
buildBundle({ corruptHash: true })
|
||||
expect(resolveExternalComponent(env(), 'win32')).toBeNull()
|
||||
})
|
||||
|
||||
it('指针指向的目录不存在时返回 null', () => {
|
||||
writeFile(
|
||||
path.join(localAppData, 'TraceMemo', 'Pro', 'windows-x64', 'current.json'),
|
||||
JSON.stringify({ version: '1.0.0', dir: path.join(tmpRoot, 'gone') })
|
||||
)
|
||||
expect(resolveExternalComponent(env(), 'win32')).toBeNull()
|
||||
})
|
||||
|
||||
it('manifest 缺失时返回 null', () => {
|
||||
const dir = buildBundle()
|
||||
fs.rmSync(path.join(dir, 'manifest.json'))
|
||||
expect(resolveExternalComponent(env(), 'win32')).toBeNull()
|
||||
})
|
||||
|
||||
it('helper 缺失时返回 null', () => {
|
||||
const dir = buildBundle()
|
||||
fs.rmSync(path.join(dir, 'tm-wcdb-config-helper.exe'))
|
||||
resetComponentCache()
|
||||
expect(resolveExternalComponent(env(), 'win32')).toBeNull()
|
||||
})
|
||||
|
||||
it('文件被替换后校验会重新计算(不吃旧缓存)', () => {
|
||||
const dir = buildBundle()
|
||||
expect(resolveExternalComponent(env(), 'win32')).not.toBeNull()
|
||||
|
||||
/* 篡改 WCDB.dll 内容但不改 mtime 之外的东西:manifest mtime 变了就要重算 */
|
||||
const target = path.join(dir, 'WCDB.dll')
|
||||
fs.writeFileSync(target, Buffer.alloc(4096, 0x99))
|
||||
const manifestPath = path.join(dir, 'manifest.json')
|
||||
const stat = fs.statSync(manifestPath)
|
||||
fs.utimesSync(manifestPath, stat.atime, new Date(stat.mtimeMs + 5000))
|
||||
|
||||
expect(resolveExternalComponent(env(), 'win32')).toBeNull()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,266 @@
|
||||
import fs from 'node:fs'
|
||||
import os from 'node:os'
|
||||
import path from 'node:path'
|
||||
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
import {
|
||||
MessageInsertCapabilityService,
|
||||
inspectNativeComponent,
|
||||
mapLicenseStatus,
|
||||
type CapabilityInput,
|
||||
type LicenseProbeResult,
|
||||
type CapabilityDeps
|
||||
} from '../../src/main/services/msg-insert-capability-service'
|
||||
|
||||
/** 判定矩阵:不确定即拒、三种原因可区分、关闭不受授权影响。 */
|
||||
|
||||
const tmpDirs: string[] = []
|
||||
|
||||
function makeTempDir(): string {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'tm-cap-'))
|
||||
tmpDirs.push(dir)
|
||||
return dir
|
||||
}
|
||||
|
||||
/** 造一个最小的合法 PE:MZ + e_lfanew + PE 签名 + machine */
|
||||
function writeFakePe(machine = 0x8664, size = 4096): string {
|
||||
const dir = makeTempDir()
|
||||
const file = path.join(dir, 'tm-wcdb-config-helper.exe')
|
||||
const buf = Buffer.alloc(size)
|
||||
buf.write('MZ', 0, 'ascii')
|
||||
buf.writeUInt32LE(0x80, 0x3c)
|
||||
buf.write('PE\0\0', 0x80, 'ascii')
|
||||
buf.writeUInt16LE(machine, 0x80 + 4)
|
||||
fs.writeFileSync(file, buf)
|
||||
return file
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
for (const dir of tmpDirs.splice(0)) fs.rmSync(dir, { recursive: true, force: true })
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
function makeService(options: {
|
||||
platform?: NodeJS.Platform
|
||||
probe?: () => Promise<LicenseProbeResult>
|
||||
component?: string | null
|
||||
macBundle?: boolean
|
||||
now?: () => number
|
||||
}): { service: MessageInsertCapabilityService; probeCalls: () => number } {
|
||||
let calls = 0
|
||||
const deps: CapabilityDeps = {
|
||||
platform: options.platform ?? 'win32',
|
||||
hasMacNativeBundle: () => options.macBundle === true,
|
||||
isMacRuntimeUnavailable: () => options.macBundle !== true,
|
||||
resolveNativeComponent: () => (options.component === undefined ? null : options.component),
|
||||
probeLicense: async () => {
|
||||
calls += 1
|
||||
if (options.probe) return options.probe()
|
||||
return { ok: true, status: 'LICENSE_OK' }
|
||||
},
|
||||
now: options.now
|
||||
}
|
||||
return { service: new MessageInsertCapabilityService(deps), probeCalls: () => calls }
|
||||
}
|
||||
|
||||
const dbReady: CapabilityInput = { databaseReady: true }
|
||||
const dbDown: CapabilityInput = { databaseReady: false }
|
||||
|
||||
describe('mapLicenseStatus', () => {
|
||||
it('只有 LICENSE_OK 算有效', () => {
|
||||
expect(mapLicenseStatus('LICENSE_OK').entitlement).toBe('valid')
|
||||
for (const status of [
|
||||
'LICENSE_EXPIRED',
|
||||
'LICENSE_MACHINE_MISMATCH',
|
||||
'LICENSE_REVOKED',
|
||||
'LICENSE_INVALID',
|
||||
'LICENSE_TAMPERED',
|
||||
'LICENSE_NOT_FOUND'
|
||||
]) {
|
||||
expect(mapLicenseStatus(status).entitlement).toBe('invalid')
|
||||
}
|
||||
})
|
||||
|
||||
it('校验中与网络错误都不算有效,也不能算「没买」', () => {
|
||||
expect(mapLicenseStatus('LICENSE_CHECKING').entitlement).toBe('pending')
|
||||
expect(mapLicenseStatus('LICENSE_NETWORK_ERROR').entitlement).toBe('unavailable')
|
||||
expect(mapLicenseStatus('LICENSE_SERVER_ERROR').entitlement).toBe('unavailable')
|
||||
expect(mapLicenseStatus(undefined).entitlement).toBe('unavailable')
|
||||
expect(mapLicenseStatus('SOMETHING_NEW').entitlement).toBe('unavailable')
|
||||
})
|
||||
})
|
||||
|
||||
describe('inspectNativeComponent', () => {
|
||||
it('缺失与空文件都算 missing', () => {
|
||||
expect(inspectNativeComponent(null, 'x64', () => null)).toBe('missing')
|
||||
const dir = makeTempDir()
|
||||
const empty = path.join(dir, 'empty.exe')
|
||||
fs.writeFileSync(empty, Buffer.alloc(16))
|
||||
expect(inspectNativeComponent(empty, 'x64', (p) => ({ size: fs.statSync(p).size }))).toBe('missing')
|
||||
})
|
||||
|
||||
it('不是 PE 的算 invalid', () => {
|
||||
const dir = makeTempDir()
|
||||
const junk = path.join(dir, 'junk.exe')
|
||||
fs.writeFileSync(junk, Buffer.alloc(4096, 0x41))
|
||||
expect(inspectNativeComponent(junk, 'x64', (p) => ({ size: fs.statSync(p).size }))).toBe('invalid')
|
||||
})
|
||||
|
||||
it('架构不符算 incompatible', () => {
|
||||
const arm = writeFakePe(0xaa64)
|
||||
expect(inspectNativeComponent(arm, 'x64', (p) => ({ size: fs.statSync(p).size }))).toBe('incompatible')
|
||||
const x64 = writeFakePe(0x8664)
|
||||
expect(inspectNativeComponent(x64, 'x64', (p) => ({ size: fs.statSync(p).size }))).toBe('ready')
|
||||
})
|
||||
})
|
||||
|
||||
describe('Windows 能力判定', () => {
|
||||
it('Hook 无响应:不可开启,原因是有界失败而不是「未授权」', async () => {
|
||||
const { service } = makeService({ probe: async () => ({ ok: false, error: 'ECONNREFUSED' }) })
|
||||
const cap = await service.refresh(dbReady)
|
||||
expect(cap.canEnable).toBe(false)
|
||||
expect(cap.entitlementState).toBe('unavailable')
|
||||
expect(cap.reasonCode).toBe('LICENSE_UNAVAILABLE')
|
||||
})
|
||||
|
||||
it('没有授权文件:要求先激活', async () => {
|
||||
const { service } = makeService({ probe: async () => ({ ok: true, status: 'LICENSE_NOT_FOUND' }) })
|
||||
const cap = await service.refresh(dbReady)
|
||||
expect(cap.reasonCode).toBe('LICENSE_REQUIRED')
|
||||
expect(cap.canEnable).toBe(false)
|
||||
})
|
||||
|
||||
it('LICENSE_CHECKING 不能当有效', async () => {
|
||||
const { service } = makeService({ probe: async () => ({ ok: true, status: 'LICENSE_CHECKING' }) })
|
||||
const cap = await service.refresh(dbReady)
|
||||
expect(cap.entitlementState).toBe('pending')
|
||||
expect(cap.reasonCode).toBe('LICENSE_PENDING')
|
||||
expect(cap.canEnable).toBe(false)
|
||||
})
|
||||
|
||||
it('过期 / 设备不匹配 / 吊销 各自有专属原因', async () => {
|
||||
const cases: Array<[string, string]> = [
|
||||
['LICENSE_EXPIRED', 'LICENSE_EXPIRED'],
|
||||
['LICENSE_MACHINE_MISMATCH', 'LICENSE_MACHINE_MISMATCH'],
|
||||
['LICENSE_REVOKED', 'LICENSE_REVOKED'],
|
||||
['LICENSE_INVALID', 'LICENSE_INVALID']
|
||||
]
|
||||
for (const [status, reason] of cases) {
|
||||
const { service } = makeService({ probe: async () => ({ ok: true, status }) })
|
||||
const cap = await service.refresh(dbReady)
|
||||
expect(cap.reasonCode).toBe(reason)
|
||||
expect(cap.canEnable).toBe(false)
|
||||
}
|
||||
})
|
||||
|
||||
it('有授权但缺组件:不能开,且原因不是授权问题', async () => {
|
||||
const { service } = makeService({ component: null })
|
||||
const cap = await service.refresh(dbReady)
|
||||
expect(cap.entitlementState).toBe('valid')
|
||||
expect(cap.nativeComponentState).toBe('missing')
|
||||
expect(cap.reasonCode).toBe('COMPONENT_MISSING')
|
||||
expect(cap.canEnable).toBe(false)
|
||||
})
|
||||
|
||||
it('组件损坏:不能开', async () => {
|
||||
const dir = makeTempDir()
|
||||
const junk = path.join(dir, 'broken.exe')
|
||||
fs.writeFileSync(junk, Buffer.alloc(4096, 0x00))
|
||||
const { service } = makeService({ component: junk })
|
||||
const cap = await service.refresh(dbReady)
|
||||
expect(cap.nativeComponentState).toBe('invalid')
|
||||
expect(cap.reasonCode).toBe('COMPONENT_INVALID')
|
||||
expect(cap.canEnable).toBe(false)
|
||||
})
|
||||
|
||||
it('数据库没连:不能开', async () => {
|
||||
const { service } = makeService({ component: writeFakePe() })
|
||||
const cap = await service.refresh(dbDown)
|
||||
expect(cap.reasonCode).toBe('DATABASE_UNAVAILABLE')
|
||||
expect(cap.canEnable).toBe(false)
|
||||
})
|
||||
|
||||
it('授权 + 组件 + 数据库齐了才允许开', async () => {
|
||||
const { service } = makeService({ component: writeFakePe() })
|
||||
const cap = await service.refresh(dbReady)
|
||||
expect(cap.canEnable).toBe(true)
|
||||
expect(cap.reasonCode).toBe('CAPABILITY_READY')
|
||||
})
|
||||
|
||||
it('关闭永远不受授权限制', async () => {
|
||||
const { service } = makeService({
|
||||
probe: async () => ({ ok: true, status: 'LICENSE_EXPIRED' }),
|
||||
component: null
|
||||
})
|
||||
const cap = await service.refresh(dbReady)
|
||||
expect(cap.canEnable).toBe(false)
|
||||
expect(cap.canDisable).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('macOS 双版本策略', () => {
|
||||
it('OSS 包:不支持,且不能说成「未授权」', async () => {
|
||||
const { service } = makeService({ platform: 'darwin', macBundle: false })
|
||||
const cap = await service.refresh(dbReady)
|
||||
expect(cap.entitlementState).toBe('unsupported')
|
||||
expect(cap.reasonCode).toBe('UNSUPPORTED_PLATFORM')
|
||||
expect(cap.canEnable).toBe(false)
|
||||
})
|
||||
|
||||
it('Native 包:组件与数据库就绪即可开,不看发送 ready', async () => {
|
||||
const { service } = makeService({ platform: 'darwin', macBundle: true, component: writeFakePe() })
|
||||
const cap = await service.refresh(dbReady)
|
||||
expect(cap.canEnable).toBe(true)
|
||||
expect(cap.entitlementState).toBe('valid')
|
||||
})
|
||||
|
||||
it('Native 包但缺组件:仍然不能开', async () => {
|
||||
const { service } = makeService({ platform: 'darwin', macBundle: true, component: null })
|
||||
const cap = await service.refresh(dbReady)
|
||||
expect(cap.canEnable).toBe(false)
|
||||
expect(cap.reasonCode).toBe('COMPONENT_MISSING')
|
||||
})
|
||||
})
|
||||
|
||||
describe('缓存与并发', () => {
|
||||
it('TTL 内复用结果,refresh 强制重查', async () => {
|
||||
let now = 1_000
|
||||
let status = 'LICENSE_REQUIRED'
|
||||
const { service, probeCalls } = makeService({
|
||||
component: writeFakePe(),
|
||||
now: () => now,
|
||||
probe: async () => ({ ok: true, status: status === 'LICENSE_REQUIRED' ? 'LICENSE_NOT_FOUND' : status })
|
||||
})
|
||||
|
||||
await service.getCapability(dbReady)
|
||||
const first = probeCalls()
|
||||
await service.getCapability(dbReady)
|
||||
expect(probeCalls()).toBe(first)
|
||||
|
||||
status = 'LICENSE_OK'
|
||||
const cached = await service.getCapability(dbReady)
|
||||
expect(cached.canEnable).toBe(false)
|
||||
|
||||
const fresh = await service.refresh(dbReady)
|
||||
expect(fresh.canEnable).toBe(true)
|
||||
expect(probeCalls()).toBeGreaterThan(first)
|
||||
|
||||
now += 31_000
|
||||
await service.getCapability(dbReady)
|
||||
expect(probeCalls()).toBeGreaterThan(first + 1)
|
||||
})
|
||||
|
||||
it('并发刷新只探一次', async () => {
|
||||
const { service, probeCalls } = makeService({ component: writeFakePe() })
|
||||
await Promise.all([service.refresh(dbReady), service.refresh(dbReady), service.refresh(dbReady)])
|
||||
expect(probeCalls()).toBe(1)
|
||||
})
|
||||
|
||||
it('数据库状态变化不受授权缓存影响', async () => {
|
||||
const { service } = makeService({ component: writeFakePe() })
|
||||
expect((await service.getCapability(dbReady)).canEnable).toBe(true)
|
||||
expect((await service.getCapability(dbDown)).canEnable).toBe(false)
|
||||
expect((await service.getCapability(dbReady)).canEnable).toBe(true)
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user