51 lines
2.4 KiB
JavaScript
51 lines
2.4 KiB
JavaScript
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)
|
|
})
|