移除示例项目初始化并修复前后端稳定性问题

This commit is contained in:
Cheng Zhou
2026-03-30 15:35:22 +08:00
parent a67991740e
commit 8c5d03f1cb
24 changed files with 581 additions and 102 deletions
+70
View File
@@ -0,0 +1,70 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { createPinia, setActivePinia } from "pinia";
const fetchStudies = vi.fn();
vi.mock("../api/studies", () => ({
fetchStudies,
}));
type StorageLike = {
getItem: (key: string) => string | null;
setItem: (key: string, value: string) => void;
removeItem: (key: string) => void;
clear: () => void;
};
const createStorage = (): StorageLike => {
const data = new Map<string, string>();
return {
getItem: (key) => data.get(key) ?? null,
setItem: (key, value) => {
data.set(key, String(value));
},
removeItem: (key) => {
data.delete(key);
},
clear: () => {
data.clear();
},
};
};
describe("study store startup rehydration", () => {
beforeEach(() => {
setActivePinia(createPinia());
fetchStudies.mockReset();
Object.defineProperty(window, "localStorage", {
value: createStorage(),
configurable: true,
});
});
it("clears a stale persisted study when the backend no longer has any studies", async () => {
window.localStorage.setItem("ctms_last_login_email", "admin@example.com");
window.localStorage.setItem(
"ctms_current_study",
JSON.stringify({
id: "deleted-study",
code: "DEMO-CTMS",
name: "示例临床试验项目",
})
);
fetchStudies.mockResolvedValue({
data: {
items: [],
},
});
const { useStudyStore } = await import("./study");
const study = useStudyStore();
study.loadCurrentStudy();
expect(study.currentStudy?.id).toBe("deleted-study");
await study.rehydrateStudyForLastUser();
expect(study.currentStudy).toBeNull();
expect(window.localStorage.getItem("ctms_current_study")).toBeNull();
});
});