Elffuss BraidHub

kikocis/continue public

19cfb6ec829a · 4.2 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 const sessionIds = Array.from({ length: limit }, (_, i) =>
97 (i + offset).toString(),
98 );
99 const isSessionIdInList = (sessionId: string) =>
100 sessions.some((session) => session.sessionId === sessionId);
101 expect(sessionIds.every(isSessionIdInList)).toBe(true);
102 });
103
104 test("List 25 sessions, with no offset", () => {
105 const limit = 25;
106
107 const sessions = historyManager.list({ limit: limit });
108 const sessionIds = Array.from({ length: limit }, (_, i) => i.toString());
109
110 const isSessionIdInList = (sessionId: string) =>
111 sessions.some((session) => session.sessionId === sessionId);
112 expect(sessionIds.every(isSessionIdInList)).toBe(true);
113 });
114
115 test("List sessions offset by 75", () => {
116 const offset = 75;
117
118 const sessions = historyManager.list({ offset: offset });
119 const sessionIds = Array.from(
120 { length: sessions.length - offset },
121 (_, i) => (i + offset).toString(),
122 );
123
124 const isSessionIdInList = (sessionId: string) =>
125 sessions.some((session) => session.sessionId === sessionId);
126 expect(sessionIds.every(isSessionIdInList)).toBe(true);
127 });
128
129 test("Delete all sessions", () => {
130 let sessions = historyManager.list({});
131
132 for (let session of sessions) {
133 historyManager.delete(session.sessionId);
134 }
135 sessions = historyManager.list({});
136 expect(sessions.length).toBe(0);
137 });
138});