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

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();
});
});
+52 -26
View File
@@ -77,6 +77,47 @@ export const useStudyStore = defineStore("study", () => {
}
};
const pickAvailableStudy = (
items: Study[],
userKey: string,
opts?: { preferActive?: boolean }
): Study | null => {
if (!items.length) {
clearCurrentStudy();
return null;
}
if (currentStudy.value?.id) {
const matchedCurrent = items.find((item) => item.id === currentStudy.value?.id);
if (matchedCurrent) {
setCurrentStudy(matchedCurrent);
return matchedCurrent;
}
}
const normalized = normalizeUserKey(userKey || "");
const saved = normalized ? readLastStudyMap()[normalized] : null;
if (saved?.id) {
const matchedSaved = items.find((item) => item.id === saved.id);
if (matchedSaved) {
setCurrentStudy(matchedSaved);
return matchedSaved;
}
}
const fallback = opts?.preferActive
? items.find((study) => study.status === "ACTIVE" && !study.is_locked) || items[0]
: items[0];
if (fallback) {
setCurrentStudy(fallback);
return fallback;
}
clearCurrentStudy();
return null;
};
const ensureDefaultStudy = async () => {
if (currentStudy.value) return currentStudy.value;
try {
@@ -124,41 +165,25 @@ export const useStudyStore = defineStore("study", () => {
};
const restoreStudyForUser = async (userKey: string, opts?: { preferActive?: boolean }) => {
const normalized = normalizeUserKey(userKey || "");
if (!normalized) return null;
if (!normalizeUserKey(userKey || "")) return null;
try {
const { data } = await fetchStudies();
const items = ((data as any).items || []) as Study[];
if (!items.length) {
clearCurrentStudy();
return null;
}
const saved = readLastStudyMap()[normalized];
if (saved?.id) {
const matched = items.find((item) => item.id === saved.id);
if (matched) {
setCurrentStudy(matched);
return matched;
}
}
const fallback = opts?.preferActive
? items.find((study) => study.status === "ACTIVE" && !study.is_locked) || items[0]
: items[0];
if (fallback) {
setCurrentStudy(fallback);
return fallback;
}
clearCurrentStudy();
return null;
return pickAvailableStudy(items, userKey, opts);
} catch {
clearCurrentStudy();
return null;
}
};
const rehydrateStudyForLastUser = async (opts?: { preferActive?: boolean }) => {
const userKey = localStorage.getItem("ctms_last_login_email") || "";
if (!normalizeUserKey(userKey)) {
return currentStudy.value;
}
return restoreStudyForUser(userKey, opts);
};
const setCurrentStudyRole = (role: string | null) => {
currentStudyRole.value = role;
if (role) {
@@ -195,6 +220,7 @@ export const useStudyStore = defineStore("study", () => {
ensureDefaultStudy,
ensureDefaultActiveStudy,
restoreStudyForUser,
rehydrateStudyForLastUser,
clearCurrentStudy,
};
});