diff --git a/main.js b/main.js index 2af0b58..82a20f0 100644 --- a/main.js +++ b/main.js @@ -556,6 +556,8 @@ sources.getSource('zlib').setLoginTransport(browserZlibLogin); ipcMain.handle('zlib:hasCreds', () => wrap(() => zlibAuth.hasCreds())); ipcMain.handle('zlib:login', (_e, email, password) => wrap(() => sources.getSource('zlib').login(email, password))); ipcMain.handle('zlib:logout', () => wrap(() => sources.getSource('zlib').logout())); +ipcMain.handle('zlib:getMirrors', () => wrap(() => sources.getSource('zlib').getMirrorConfig())); +ipcMain.handle('zlib:setMirrors', (_e, list) => wrap(() => sources.getSource('zlib').setMirrors(list))); // Semantic Scholar API Key ipcMain.handle('semanticScholar:keyStatus', () => wrap(() => semanticKey.status())); diff --git a/package-lock.json b/package-lock.json index dc5e2c8..58f093a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "peoplelib", - "version": "2.1.4", + "version": "2.1.5", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "peoplelib", - "version": "2.1.4", + "version": "2.1.5", "license": "MIT", "dependencies": { "foliate-js": "1.0.1", diff --git a/package.json b/package.json index 6ee26ee..f311d91 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "peoplelib", - "version": "2.1.4", + "version": "2.1.5", "description": "多源开放文献、电子书与本地书库客户端", "main": "main.js", "author": "peoplelib", diff --git a/preload.js b/preload.js index 70b07a7..321614d 100644 --- a/preload.js +++ b/preload.js @@ -129,7 +129,9 @@ contextBridge.exposeInMainWorld('api', { zlib: { hasCreds: () => ipcRenderer.invoke('zlib:hasCreds'), login: (email, password) => ipcRenderer.invoke('zlib:login', email, password), - logout: () => ipcRenderer.invoke('zlib:logout') + logout: () => ipcRenderer.invoke('zlib:logout'), + getMirrors: () => ipcRenderer.invoke('zlib:getMirrors'), + setMirrors: (list) => ipcRenderer.invoke('zlib:setMirrors', list) }, semanticScholar: { keyStatus: () => ipcRenderer.invoke('semanticScholar:keyStatus'), diff --git a/src/_test/auth.test.js b/src/_test/auth.test.js index 27ce338..618c306 100644 --- a/src/_test/auth.test.js +++ b/src/_test/auth.test.js @@ -135,6 +135,36 @@ test('customMirrors 往返不丢失', () => { assert.deepStrictEqual(auth.read().customMirrors, ['https://m1', 'https://m2']); }); +test('setCustomMirrors 不重写密文,凭据与会话都保留', () => { + const d = tmp(); + const auth = freshAuth(); + auth.init(d, fakeStorage()); + auth.write({ email: 'a@b.c', password: 'Keep', userId: '1', userKey: 'k', mirror: 'https://m1' }); + + auth.setCustomMirrors(['https://m1', 'https://m2']); + assert.deepStrictEqual(auth.getCustomMirrors(), ['https://m1', 'https://m2']); + assert.strictEqual(auth.read().password, 'Keep', '改镜像不该动凭据'); + assert.deepStrictEqual(auth.getSession(), { userId: '1', userKey: 'k', mirror: 'https://m1' }); + + auth.setCustomMirrors([]); + assert.deepStrictEqual(auth.getCustomMirrors(), []); + assert.strictEqual(auth.hasCreds(), true); +}); + +test('退出登录清掉凭据但保留自定义镜像', () => { + const d = tmp(); + const auth = freshAuth(); + auth.init(d, fakeStorage()); + auth.write({ email: 'a@b.c', password: 'p', userId: '1', userKey: 'k' }); + auth.setCustomMirrors(['https://mine.example']); + + auth.clear(); + assert.strictEqual(auth.hasCreds(), false, '凭据未清除'); + assert.strictEqual(auth.getSession(), null, '会话未清除'); + // 默认域名可能已全部失效,退出登录顺带清掉镜像会让用户无从登录 + assert.deepStrictEqual(auth.getCustomMirrors(), ['https://mine.example']); +}); + test('损坏的密文不影响会话字段读取', () => { const d = tmp(); const auth = freshAuth(); diff --git a/src/_test/electron/startup.integration.js b/src/_test/electron/startup.integration.js index fed7afc..d71aa3b 100644 --- a/src/_test/electron/startup.integration.js +++ b/src/_test/electron/startup.integration.js @@ -95,6 +95,29 @@ app.whenReady().then(async () => { proxyRoute); await win.webContents.executeJavaScript(`window.api.proxy.set('')`); + await win.webContents.executeJavaScript(`document.getElementById('zlibMirrorBtn').click()`); + await waitUntil(() => win.webContents.executeJavaScript( + `!!document.getElementById('zlibMirrorList')` + )); + await win.webContents.executeJavaScript(`(() => { + document.getElementById('zlibMirrorList').value = + 'https://mirror.example/path\\nhttps://second.example/'; + document.getElementById('modalOk').click(); + })()`); + const mirrorConfig = await waitUntil(() => win.webContents.executeJavaScript(`(async () => { + const result = await window.api.zlib.getMirrors(); + const status = document.getElementById('zlibMirrorStatus').textContent; + return result.ok && result.data.custom.length === 2 && /已自定义 2 个镜像/.test(status) + ? { custom: result.data.custom, status } + : null; + })()`)); + check('Z-Library 账户设置可保存并优先使用自定义镜像', + JSON.stringify(mirrorConfig.custom) === JSON.stringify([ + 'https://mirror.example', + 'https://second.example' + ]), + JSON.stringify(mirrorConfig)); + const sourceRows = await waitUntil(() => win.webContents.executeJavaScript(`(() => { const rows = Array.from(document.querySelectorAll('#sourceList input[data-id]')).map((input) => ({ id: input.dataset.id, diff --git a/src/_test/sources.test.js b/src/_test/sources.test.js index df6839a..747dd50 100644 --- a/src/_test/sources.test.js +++ b/src/_test/sources.test.js @@ -560,6 +560,70 @@ test('zlib: 完全无效的 id 仍然拒绝', async () => { await assert.rejects(zlib.detail('not-an-id'), /无效的 Z-Library ID/); }); +test('zlib: 自定义镜像规范化、去重并优先于内置列表', () => { + const zlib = h.freshRequire('sources/zlib.js'); + const auth = require('../sources/zlib-auth'); + const orig = { + getCustomMirrors: auth.getCustomMirrors, + setCustomMirrors: auth.setCustomMirrors, + getSession: auth.getSession, + clearSession: auth.clearSession + }; + let custom = []; + auth.getCustomMirrors = () => custom.slice(); + auth.setCustomMirrors = (list) => { custom = list.slice(); }; + auth.getSession = () => null; + auth.clearSession = () => {}; + try { + const result = zlib.setMirrors([ + 'https://mirror.example/path?ignored=1', + 'https://mirror.example/', + 'https://second.example' + ]); + assert.deepStrictEqual(result.custom, [ + 'https://mirror.example', + 'https://second.example' + ]); + assert.deepStrictEqual(zlib.getMirrorConfig().custom, result.custom); + assert.ok(result.defaults.length >= 1, '内置镜像列表丢失'); + } finally { + Object.assign(auth, orig); + } +}); + +test('zlib: 自定义镜像拒绝非 HTTPS、凭据与超量输入', () => { + const zlib = h.freshRequire('sources/zlib.js'); + assert.throws(() => zlib.setMirrors(['http://mirror.example']), /必须使用 HTTPS/); + assert.throws(() => zlib.setMirrors(['https://user:pass@mirror.example']), /不能包含账号信息/); + assert.throws( + () => zlib.setMirrors(Array.from({ length: 21 }, (_, i) => `https://m${i}.example`)), + /最多只能保存 20 个镜像/ + ); +}); + +test('zlib: 移除当前会话的非内置镜像会清除旧会话', () => { + const zlib = h.freshRequire('sources/zlib.js'); + const auth = require('../sources/zlib-auth'); + const orig = { + getCustomMirrors: auth.getCustomMirrors, + setCustomMirrors: auth.setCustomMirrors, + getSession: auth.getSession, + clearSession: auth.clearSession + }; + let custom = ['https://old.example']; + let cleared = 0; + auth.getCustomMirrors = () => custom.slice(); + auth.setCustomMirrors = (list) => { custom = list.slice(); }; + auth.getSession = () => ({ userId: '1', userKey: 'k', mirror: 'https://old.example' }); + auth.clearSession = () => { cleared += 1; }; + try { + zlib.setMirrors(['https://new.example']); + assert.strictEqual(cleared, 1, '被移除镜像上的会话仍被保留'); + } finally { + Object.assign(auth, orig); + } +}); + // 实测:会话过期时 /file 返回 400 + {"success":0,"error":"Please login"}。 // 若按 HTTP 状态码短路,真实原因会被吞掉,自动重登也不会触发。 test('zlib: 4xx+JSON 的会话过期能被识别并自动重新登录', async () => { diff --git a/src/_test/ui.test.js b/src/_test/ui.test.js index 9c641c3..0e322c2 100644 --- a/src/_test/ui.test.js +++ b/src/_test/ui.test.js @@ -240,6 +240,28 @@ test('Z-Library 进入详情不消耗下载额度,点击下载后才解析并 assert.match(onDemand, /每日免费下载额度有限,仅在点击后获取下载地址/); }); +test('Z-Library 账户设置提供可编辑的镜像站点列表', () => { + const html = fs.readFileSync(path.join(__dirname, '..', 'ui', 'index.html'), 'utf8'); + const app = fs.readFileSync(path.join(__dirname, '..', 'ui', 'app.js'), 'utf8'); + const preload = fs.readFileSync(path.join(__dirname, '..', '..', 'preload.js'), 'utf8'); + const main = fs.readFileSync(path.join(__dirname, '..', '..', 'main.js'), 'utf8'); + const css = fs.readFileSync(path.join(__dirname, '..', 'ui', 'style.css'), 'utf8'); + + const accountAt = html.indexOf('Z-Library 账号'); + const mirrorsAt = html.indexOf('id="zlibMirrorBtn"'); + const nextGroupAt = html.indexOf('
一行一个地址,必须是 HTTPS。自定义镜像会排在内置列表前面优先尝试;留空则只用内置列表。
+