Elffuss BraidHub

kikocis/continue public

main · 4.3 KiBRawPermalink
1import { v4 as uuidv4 } from "uuid";
2
3import { Session } from "..";
4import { NEW_SESSION_TITLE } from "./constants";
5import historyManager from "./history";
6import { getSessionFilePath } from "./paths";
7
8const sessionId = uuidv4();
9const testSession: Session = {
10 history: [],
11 title: `${sessionId} title`,
12 workspaceDirectory: "workspaceDir",
13 sessionId: sessionId,
14};
15
16describe("No sessions have been created", () => {
17 const testSessionId = "invalid";
18 const testSessionPath = getSessionFilePath(testSessionId);
19
20 test("Listing all sessions returns empty list", () => {
21 const sessions = historyManager.list({});
22 expect(sessions).toEqual([]);
23 });
24
25 test("Deleting session throws error", () => {
26 expect(() => {
27 historyManager.delete(testSessionId);
28 }).toThrow(`Session file ${testSessionPath} does not exist`);
29 });
30
31 test("Loading session returns default session", () => {
32 const session = historyManager.load(testSessionId);
33 expect(session).toEqual({
34 history: [],
35 title: NEW_SESSION_TITLE,
36 workspaceDirectory: "",
37 sessionId: testSessionId,
38 });
39 });
40});
41
42describe("Full session lifecycle", () => {
43 test("Creating and listing a session", () => {
44 // save and list
45 historyManager.save(testSession);
46 const sessions = historyManager.list({});
47 const sessionExists = sessions.some(
48 (session) => session?.sessionId === testSession.sessionId,
49 );
50 expect(sessionExists).toBe(true);
51 });
52
53 test("Loading session by ID returns correct object", () => {
54 const retrievedSession = historyManager.load(testSession.sessionId);
55 expect(retrievedSession).toEqual(testSession);
56 });
57
58 test("Saving session with new title updates session", () => {
59 const modifiedSession = { ...testSession };
60 modifiedSession.title = `Edited: ${testSession.title}`;
61 historyManager.save(modifiedSession);
62 const session = historyManager.load(testSession.sessionId);
63
64 expect(session.title).toBe(modifiedSession.title);
65 });
66
67 test("Deleting session", () => {
68 historyManager.delete(testSession.sessionId);
69 const sessions = historyManager.list({});
70 const sessionWasDeleted = sessions.every(
71 (session) => session?.sessionId !== testSession.sessionId,
72 );
73 expect(sessionWasDeleted).toEqual(true);
74 });
75});
76
77describe("Many sessions created", () => {
78 test("Create 100 sessions and list all", () => {
79 for (let i = 0; i < 100; i++) {
80 historyManager.save({
81 history: [],
82 title: `${i}`,
83 workspaceDirectory: "workspaceDir",
84 sessionId: `${i}`,
85 });
86 }
87 const sessions = historyManager.list({});
88 expect(sessions.length).toBe(100);
89 });
90
91 test("List 10 sessions, offest by 10", () => {
92 const limit = 10;
93 const offset = 10;
94
95 const sessions = historyManager.list({ offset: offset, limit: limit });
96 // Sessions are now reversed, so newest (99) comes first
97 const sessionIds = Array.from({ length: limit }, (_, i) =>
98 (99 - offset - i).toString(),
99 );
100 const isSessionIdInList = (sessionId: string) =>
101 sessions.some((session) => session.sessionId === sessionId);
102 expect(sessionIds.every(isSessionIdInList)).toBe(true);
103 });
104
105 test("List 25 sessions, with no offset", () => {
106 const limit = 25;
107
108 const sessions = historyManager.list({ limit: limit });
109 // Sessions are now reversed, so newest (99) comes first
110 const sessionIds = Array.from({ length: limit }, (_, i) =>
111 (99 - i).toString(),
112 );
113
114 const isSessionIdInList = (sessionId: string) =>
115 sessions.some((session) => session.sessionId === sessionId);
116 expect(sessionIds.every(isSessionIdInList)).toBe(true);
117 });
118
119 test("List sessions offset by 75", () => {
120 const offset = 75;
121
122 const sessions = historyManager.list({ offset: offset });
123 const sessionIds = Array.from(
124 { length: sessions.length - offset },
125 (_, i) => (i + offset).toString(),
126 );
127
128 const isSessionIdInList = (sessionId: string) =>
129 sessions.some((session) => session.sessionId === sessionId);
130 expect(sessionIds.every(isSessionIdInList)).toBe(true);
131 });
132
133 test("Delete all sessions", () => {
134 let sessions = historyManager.list({});
135
136 for (let session of sessions) {
137 historyManager.delete(session.sessionId);
138 }
139 sessions = historyManager.list({});
140 expect(sessions.length).toBe(0);
141 });
142});