import test from "node:test" import assert from "node:assert/strict" // 直接加载真实的 store 模块;在 Node 里 import 到的就是下发到浏览器的那份配置。 const store = (await import("../src/store/index.js")).default const tasks = () => [ {gid: 1, name: "beta [tag]", status: "下载完成", pages: 10, proceeding: 10, thumb_link: "", createTime: 2, downloader: 7, isCollect: true}, {gid: 2, name: "alpha", status: "下载中", pages: 10, proceeding: 5, thumb_link: "", createTime: 1, downloader: 7, isCollect: false}, {gid: 3, name: "gamma", status: "已提交", pages: 10, proceeding: 0, thumb_link: "", createTime: 3, downloader: 8, isCollect: false}, ] function reset() { store.state.AuthCode = "test-auth" store.state.userId = 7 store.state.category = "total" store.state.sortType = "createTime" store.state.length = 2 store.state.page = 1 store.state.isAdmin = false store.state.downloaderFilter = null store.commit("_updateGalleryTasks", tasks()) } test("module exposes one normalized task list plus derived getters", () => { reset() assert.equal(store.state.totalGalleryTask.length, 3) assert.equal("collectGallery" in store.state, false) assert.equal("downloadGallery" in store.state, false) assert.equal(store.getters.downloadGallery.length, 2) assert.equal(store.getters.collectGallery.length, 1) }) test("json-string and object payloads decode to the same store result", () => { reset() const fromObjects = store.state.totalGalleryTask.map(task => task.gid) store.state.totalGalleryTask = [] store.commit("_updateGalleryTasks", JSON.parse(JSON.stringify(tasks()))) assert.deepEqual(store.state.totalGalleryTask.map(task => task.gid), fromObjects) }) test("local search keeps every match reachable through the pager", () => { reset() store.state.length = 2 store.commit("_searchLocalByKeyword", "a") assert.equal(store.state.isSearch, true) assert.equal(store.getters.filteredTasks.length, 3) assert.equal(store.getters.max, 2) assert.equal(store.getters.currentTasks.length, 2) store.commit("_changePage", 2) assert.equal(store.getters.currentTasks.length, 1) }) test("a search matching more than one page exposes every page", () => { store.state.AuthCode = "auth" store.state.userId = 7 store.state.category = "total" const many = Array.from({length: 25}, (_, index) => ({ gid: index + 1, name: "match " + index, status: "已提交", pages: 1, proceeding: 0, thumb_link: "", createTime: index, downloader: 7, })) store.state.length = 10 store.commit("_updateGalleryTasks", many) store.commit("_searchLocalByKeyword", "match") assert.equal(store.getters.filteredTasks.length, 25) assert.equal(store.getters.max, 3) assert.equal(store.getters.currentTasks.length, 10) store.commit("_changePage", 3) assert.equal(store.getters.currentTasks.length, 5) }) test("cancelling a collect for a task missing from the list never throws", () => { reset() // 旧实现会在 collectGallery 里找不到 gid 时抛 TypeError。 assert.doesNotThrow(() => store.commit("_setCollect", {gid: 999, collect: false})) store.commit("_setCollect", {gid: 1, collect: false}) assert.equal(store.getters.collectGallery.length, 0) store.commit("_setCollect", {gid: 1, collect: true}) assert.equal(store.getters.collectGallery.length, 1) }) test("sorting never reorders the single source array", () => { reset() const before = store.state.totalGalleryTask.map(task => task.gid) store.commit("_setSortType", "name") assert.deepEqual(store.state.totalGalleryTask.map(task => task.gid), before) assert.deepEqual(store.state.currentTasks.map(task => task.name), ["alpha", "beta [tag]", "gamma"]) }) test("admin downloader filter narrows the visible list only for admins", () => { reset() store.commit("_setDownloaderFilter", 8) assert.equal(store.getters.filteredTasks.length, 3) store.state.isAdmin = true store.commit("_setDownloaderFilter", 8) assert.equal(store.getters.filteredTasks.length, 1) assert.equal(store.getters.filteredTasks[0].gid, 3) }) test("thumbnail and online image urls carry the active auth code", () => { store.state.AuthCode = "code with spaces" store.commit("_updateGalleryTasks", [{ gid: 123, name: "sample", status: "已提交", thumb_link: "https://example.org/thumb?a=1&b=2", createTime: 1, downloader: 7, }]) const thumbnail = new URL(store.state.totalGalleryTask[0].thumb_link) assert.equal(thumbnail.searchParams.get("AuthCode"), "code with spaces") assert.equal(thumbnail.searchParams.get("path"), "https://example.org/thumb?a=1&b=2") store.commit("_setReadingGallery", {gid: 123, pages: 1}) const image = new URL(store.state.readingGallery.images[0]) assert.equal(image.searchParams.get("gid"), "123") assert.equal(image.searchParams.get("AuthCode"), "code with spaces") }) test("proceeding websocket updates refresh the progress text", () => { reset() store.commit("_updateGalleryTaskProceeding", [{gid: 3, status: 1, proceeding: 5}]) const task = store.state.totalGalleryTask.find(item => item.gid === 3) assert.equal(task.status, "下载中") assert.equal(task.progress, "50%") })