From d0eb2f05be076eb90a4bff252d882e8db39bcdff Mon Sep 17 00:00:00 2001 From: root Date: Tue, 8 Sep 2026 09:35:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=8C=89=E9=93=BE=E6=8E=A5?= =?UTF-8?q?=E6=9F=A5=E6=89=BE=E6=95=B0=E5=AD=97=E7=94=BB=E5=BB=8AID?= =?UTF-8?q?=E5=B9=B6=E8=A1=A5=E5=85=85=E7=8A=B6=E6=80=81=E5=9B=9E=E5=BD=92?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/store/index.js | 2 +- tests/store.test.mjs | 50 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 tests/store.test.mjs diff --git a/src/store/index.js b/src/store/index.js index 378fbd1..73dfb8d 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -335,7 +335,7 @@ const mutations = { } } else for (let i = 0; i < tasks.length; i++) - if (tasks[i].gid === gid) { + if (String(tasks[i].gid) === gid) { state.page = Math.floor(i / state.length) + 1 name = state.sortType === "shortName" ? tasks[i].shortName: tasks[i].name break diff --git a/tests/store.test.mjs b/tests/store.test.mjs new file mode 100644 index 0000000..3f020b2 --- /dev/null +++ b/tests/store.test.mjs @@ -0,0 +1,50 @@ +import test from 'node:test' +import assert from 'node:assert/strict' +import fs from 'node:fs' +import vm from 'node:vm' + +function fixture() { + let code = fs.readFileSync(new URL('../src/store/index.js', import.meta.url), 'utf8') + .replace(/^import .*$/gm, '') + .replace('export default new vuex.Store(', 'globalThis.store = new vuex.Store(') + const messages = [] + const axios = {interceptors: {response: {use() {}}}} + const context = vm.createContext({axios, vuex: {Store: function (config) {Object.assign(this, config)}}, + ElMessage: message => messages.push(message), URLSearchParams, console, + qs: {stringify: () => ''}}) + vm.runInContext(code, context) + const store = context.store + const api = {state: store.state, + commit: (name, data) => store.mutations[name](store.state, data), + dispatch: (name, data) => Promise.resolve(store.actions[name](api, data))} + return {store, messages, axios, api} +} + +test('link search matches a numeric gid and jumps to the right page', () => { + const {store, messages} = fixture() + store.state.currentTasks = [{gid: 1}, {gid: 2}, {gid: 12345, name: 'sample', shortName: 'sample'}] + store.state.length = 2 + store.mutations._searchLocalByLink(store.state, 'https://example.org/g/12345/key/') + assert.equal(store.state.page, 2) + assert.match(messages[0], /已跳转/) +}) + +test('missing gid still reports no match', () => { + const {store, messages} = fixture() + store.state.currentTasks = [{gid: 12345}] + store.mutations._searchLocalByLink(store.state, 'https://example.org/g/999/key/') + assert.equal(messages[0], '未找到此任务') +}) + +test('submission reloads persisted status without duplicating a completed task', async () => { + const {store, axios, api} = fixture() + const gallery = {gid: 123, name: 'sample', status: '下载完成', thumb_link: '', createTime: 1, downloader: 7} + store.state.chosenGallery = {...gallery, status: '已提交'} + store.state.totalGalleryTask.push(gallery) + axios.post = async () => ({data: {result: 'success'}}) + axios.get = async url => ({data: {result: 'success', data: JSON.stringify(url.endsWith('weekUsedAmount') ? {} : [gallery])}}) + await store.actions.postGalleryTask(api, {targetResolution: 'original'}) + assert.equal(store.state.totalGalleryTask.length, 1) + assert.equal(store.state.totalGalleryTask[0].status, '下载完成') + assert.equal(store.state.chosenGallery, false) +})