|
|
@@ -0,0 +1,74 @@
|
|
|
+import { expect, test } from "bun:test"
|
|
|
+import {
|
|
|
+ archiveProject,
|
|
|
+ bindProjectWorkspace,
|
|
|
+ newProject,
|
|
|
+ projectStorageKey,
|
|
|
+ readProjectState,
|
|
|
+ renameProject,
|
|
|
+ writeProjectState,
|
|
|
+} from "./projects"
|
|
|
+
|
|
|
+class MemoryStorage implements Storage {
|
|
|
+ private data = new Map<string, string>()
|
|
|
+ get length() {
|
|
|
+ return this.data.size
|
|
|
+ }
|
|
|
+ clear() {
|
|
|
+ this.data.clear()
|
|
|
+ }
|
|
|
+ getItem(key: string) {
|
|
|
+ return this.data.get(key) ?? null
|
|
|
+ }
|
|
|
+ key(index: number) {
|
|
|
+ return [...this.data.keys()][index] ?? null
|
|
|
+ }
|
|
|
+ removeItem(key: string) {
|
|
|
+ this.data.delete(key)
|
|
|
+ }
|
|
|
+ setItem(key: string, value: string) {
|
|
|
+ this.data.set(key, value)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+test("empty storage creates one stable default project", () => {
|
|
|
+ const storage = new MemoryStorage()
|
|
|
+ const first = readProjectState(storage, "scope-a", "main", 1000)
|
|
|
+ const second = readProjectState(storage, "scope-a", "main", 2000)
|
|
|
+ expect(first.activeID).toBe(second.activeID)
|
|
|
+ expect(first.projects).toHaveLength(1)
|
|
|
+ expect(first.projects[0].name).toBe("我的研究")
|
|
|
+})
|
|
|
+
|
|
|
+test("projects persist per scope and window", () => {
|
|
|
+ const storage = new MemoryStorage()
|
|
|
+ const state = readProjectState(storage, "scope-a", "main", 1000)
|
|
|
+ writeProjectState(storage, "scope-a", "main", state)
|
|
|
+ expect(storage.getItem(projectStorageKey("scope-a", "main"))).toContain("我的研究")
|
|
|
+ expect(storage.getItem(projectStorageKey("scope-b", "main"))).toBeNull()
|
|
|
+})
|
|
|
+
|
|
|
+test("rename, bind and archive preserve active project invariants", () => {
|
|
|
+ const storage = new MemoryStorage()
|
|
|
+ const state = readProjectState(storage, "scope", "main", 1000)
|
|
|
+ const project = newProject("光学综述", "D:/Research/optics", 1100, "p1")
|
|
|
+ const created = { ...state, projects: [...state.projects, project], activeID: project.id }
|
|
|
+ const renamed = renameProject(created, "p1", "超表面综述", 1200)
|
|
|
+ const bound = bindProjectWorkspace(renamed, "p1", "D:/Research/metasurface", 1300)
|
|
|
+ expect(bound.projects.find((item) => item.id === "p1")).toMatchObject({
|
|
|
+ name: "超表面综述",
|
|
|
+ workspace: "D:/Research/metasurface",
|
|
|
+ updatedAt: 1300,
|
|
|
+ })
|
|
|
+ const archived = archiveProject(bound, "p1", 1400)
|
|
|
+ expect(archived.projects.find((item) => item.id === "p1")?.archivedAt).toBe(1400)
|
|
|
+ expect(archived.activeID).not.toBe("p1")
|
|
|
+})
|
|
|
+
|
|
|
+test("corrupt project data falls back without throwing", () => {
|
|
|
+ const storage = new MemoryStorage()
|
|
|
+ storage.setItem(projectStorageKey("scope", "main"), "{broken")
|
|
|
+ const state = readProjectState(storage, "scope", "main", 1000)
|
|
|
+ expect(state.projects).toHaveLength(1)
|
|
|
+ expect(state.activeID).toBe(state.projects[0].id)
|
|
|
+})
|