From 9acd7229292c249b178dd08850a37c265d0780d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Mon, 9 Mar 2026 13:52:08 +0800 Subject: [PATCH] Fix splice(-1,1) removing last user when logged-in user not found Co-Authored-By: Claude Opus 4.6 --- src/stores/acctMgmt.ts | 4 +++- tests/stores/acctMgmt.test.js | 34 ++++++++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/stores/acctMgmt.ts b/src/stores/acctMgmt.ts index 966ce1f..d39dcaf 100644 --- a/src/stores/acctMgmt.ts +++ b/src/stores/acctMgmt.ts @@ -127,7 +127,9 @@ export const useAcctMgmtStore = defineStore('acctMgmtStore', { const loginStore = useLoginStore(); const loginUserData:User = loginStore.userData; const foundLoginUserIndex = fetchedUserList.findIndex(user => user.username === loginUserData.username); - fetchedUserList.unshift(fetchedUserList.splice(foundLoginUserIndex, 1)[0]); + if (foundLoginUserIndex !== -1) { + fetchedUserList.unshift(fetchedUserList.splice(foundLoginUserIndex, 1)[0]); + } return fetchedUserList; }, /** diff --git a/tests/stores/acctMgmt.test.js b/tests/stores/acctMgmt.test.js index 2b810fa..d0a3c88 100644 --- a/tests/stores/acctMgmt.test.js +++ b/tests/stores/acctMgmt.test.js @@ -21,8 +21,8 @@ vi.mock("@/api/client.js", () => ({ })); // Mock login store to avoid its side effects -vi.mock("@/stores/login", () => { - const { defineStore } = require("pinia"); +vi.mock("@/stores/login", async () => { + const { defineStore } = await import("pinia"); return { useLoginStore: defineStore("loginStore", { state: () => ({ @@ -243,6 +243,36 @@ describe("acctMgmtStore", () => { }); }); + describe("moveCurrentLoginUserToFirstRow", () => { + it("moves logged-in user to first position", async () => { + const list = [ + { username: "alice", name: "Alice" }, + { username: "currentUser", name: "Current" }, + { username: "bob", name: "Bob" }, + ]; + const result = await store.moveCurrentLoginUserToFirstRow(list); + expect(result.map((u) => u.username)).toEqual([ + "currentUser", + "alice", + "bob", + ]); + }); + + it("preserves list when logged-in user is not found", async () => { + const list = [ + { username: "alice", name: "Alice" }, + { username: "bob", name: "Bob" }, + { username: "charlie", name: "Charlie" }, + ]; + const result = await store.moveCurrentLoginUserToFirstRow(list); + expect(result.map((u) => u.username)).toEqual([ + "alice", + "bob", + "charlie", + ]); + }); + }); + it("resetJustCreateFlag resets flag", () => { store.isOneAccountJustCreate = true; store.resetJustCreateFlag();