fix: refresh recent connections once connections are loaded

The Recent strip was computed from get().connections which is still empty
while loadAll() is in flight on app startup, so cached recent IDs got
filtered out and the strip could appear empty/flaky until a remount.

loadAll now refreshes recents after connections populate.
This commit is contained in:
2026-09-01 05:13:31 +08:00
parent 7a66b70c26
commit f8321d926f
2 changed files with 30 additions and 0 deletions
@@ -225,6 +225,33 @@ describe("favorites / recents / move-selection", () => {
expect(commands.recordRecentConnection).toHaveBeenCalledWith("c1"); expect(commands.recordRecentConnection).toHaveBeenCalledWith("c1");
}); });
it("loadAll refreshes recents once connections are known (startup race fix)", async () => {
const conn = makeConn({ id: "c1", name: "Prod" });
vi.spyOn(commands, "getConnections").mockResolvedValue([conn]);
vi.spyOn(commands, "getFolders").mockResolvedValue([]);
vi.spyOn(commands, "getTags").mockResolvedValue([]);
vi.spyOn(commands, "getRecentConnections").mockResolvedValue([
{ connection_id: "c1", opened_at: "2026-08-02T00:00:00Z" },
]);
// Simulate the app-shell mount: loadAll fires and populates connections.
await useConnectionStore.getState().loadAll();
// The recent strip must now show the connection that was opened previously.
expect(commands.getRecentConnections).toHaveBeenCalledWith(8);
expect(useConnectionStore.getState().recent).toEqual([conn]);
});
it("loadRecent maps cached recent ids against the current connection list", async () => {
const conn = makeConn({ id: "c1", name: "Prod" });
useConnectionStore.setState({ connections: [conn] });
vi.spyOn(commands, "getRecentConnections").mockResolvedValue([
{ connection_id: "c1", opened_at: "2026-08-02T00:00:00Z" },
]);
await useConnectionStore.getState().loadRecent();
expect(useConnectionStore.getState().recent).toEqual([conn]);
});
describe("duplicateConnection", () => { describe("duplicateConnection", () => {
it("copies fields with a '(copy)' name and favorite false", async () => { it("copies fields with a '(copy)' name and favorite false", async () => {
const src = makeConn({ id: "c1", name: "Prod", folder_id: "f1", tag_ids: ["t1"], favorite: true }); const src = makeConn({ id: "c1", name: "Prod", folder_id: "f1", tag_ids: ["t1"], favorite: true });
+3
View File
@@ -46,6 +46,9 @@ export const useConnectionStore = create<ConnectionState>((set, get) => ({
set((s) => ({ connections: [...s.connections].sort((a, b) => Number(b.favorite) - Number(a.favorite)) })); set((s) => ({ connections: [...s.connections].sort((a, b) => Number(b.favorite) - Number(a.favorite)) }));
// Also load tag order // Also load tag order
get().loadTagOrder(); get().loadTagOrder();
// Refresh recents once connections are known so the recent strip is not
// computed against an empty list (avoids a startup race with loadRecent).
get().loadRecent().catch(() => {});
} catch (e) { } catch (e) {
set({ loading: false, error: e instanceof Error ? e.message : String(e) }); set({ loading: false, error: e instanceof Error ? e.message : String(e) });
} }