File size: 9,134 Bytes
ac6acf2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// @ts-check
/// <reference path="../node_modules/@types/jest/index.d.ts" />
const { start } = require("../utils");
const lg = require("../utils/litegraph");

describe("users", () => {
	beforeEach(() => {
		lg.setup(global);
	});

	afterEach(() => {
		lg.teardown(global);
	});

	function expectNoUserScreen() {
		// Ensure login isnt visible
		const selection = document.querySelectorAll("#comfy-user-selection")?.[0];
		expect(selection["style"].display).toBe("none");
		const menu = document.querySelectorAll(".comfy-menu")?.[0];
		expect(window.getComputedStyle(menu)?.display).not.toBe("none");
	}

	describe("multi-user", () => {
		function mockAddStylesheet() {
			const utils = require("../../web/scripts/utils");
			utils.addStylesheet = jest.fn().mockReturnValue(Promise.resolve());
		}

		async function waitForUserScreenShow() {
			mockAddStylesheet();

			// Wait for "show" to be called
			const { UserSelectionScreen } = require("../../web/scripts/ui/userSelection");
			let resolve, reject;
			const fn = UserSelectionScreen.prototype.show;
			const p = new Promise((res, rej) => {
				resolve = res;
				reject = rej;
			});
			jest.spyOn(UserSelectionScreen.prototype, "show").mockImplementation(async (...args) => {
				const res = fn(...args);
				await new Promise(process.nextTick); // wait for promises to resolve
				resolve();
				return res;
			});
			// @ts-ignore
			setTimeout(() => reject("timeout waiting for UserSelectionScreen to be shown."), 500);
			await p;
			await new Promise(process.nextTick); // wait for promises to resolve
		}

		async function testUserScreen(onShown, users) {
			if (!users) {
				users = {};
			}
			const starting = start({
				resetEnv: true,
				userConfig: { storage: "server", users },
			});

			// Ensure no current user
			expect(localStorage["Comfy.userId"]).toBeFalsy();
			expect(localStorage["Comfy.userName"]).toBeFalsy();

			await waitForUserScreenShow();

			const selection = document.querySelectorAll("#comfy-user-selection")?.[0];
			expect(selection).toBeTruthy();

			// Ensure login is visible
			expect(window.getComputedStyle(selection)?.display).not.toBe("none");
			// Ensure menu is hidden
			const menu = document.querySelectorAll(".comfy-menu")?.[0];
			expect(window.getComputedStyle(menu)?.display).toBe("none");

			const isCreate = await onShown(selection);

			// Submit form
			selection.querySelectorAll("form")[0].submit();
			await new Promise(process.nextTick); // wait for promises to resolve

			// Wait for start
			const s = await starting;

			// Ensure login is removed
			expect(document.querySelectorAll("#comfy-user-selection")).toHaveLength(0);
			expect(window.getComputedStyle(menu)?.display).not.toBe("none");

			// Ensure settings + templates are saved
			const { api } = require("../../web/scripts/api");
			expect(api.createUser).toHaveBeenCalledTimes(+isCreate);
			expect(api.storeSettings).toHaveBeenCalledTimes(+isCreate);
			expect(api.storeUserData).toHaveBeenCalledTimes(+isCreate);
			if (isCreate) {
				expect(api.storeUserData).toHaveBeenCalledWith("comfy.templates.json", null, { stringify: false });
				expect(s.app.isNewUserSession).toBeTruthy();
			} else {
				expect(s.app.isNewUserSession).toBeFalsy();
			}

			return { users, selection, ...s };
		}

		it("allows user creation if no users", async () => {
			const { users } = await testUserScreen((selection) => {
				// Ensure we have no users flag added
				expect(selection.classList.contains("no-users")).toBeTruthy();

				// Enter a username
				const input = selection.getElementsByTagName("input")[0];
				input.focus();
				input.value = "Test User";

				return true;
			});

			expect(users).toStrictEqual({
				"Test User!": "Test User",
			});

			expect(localStorage["Comfy.userId"]).toBe("Test User!");
			expect(localStorage["Comfy.userName"]).toBe("Test User");
		});
		it("allows user creation if no current user but other users", async () => {
			const users = {
				"Test User 2!": "Test User 2",
			};

			await testUserScreen((selection) => {
				expect(selection.classList.contains("no-users")).toBeFalsy();

				// Enter a username
				const input = selection.getElementsByTagName("input")[0];
				input.focus();
				input.value = "Test User 3";
				return true;
			}, users);

			expect(users).toStrictEqual({
				"Test User 2!": "Test User 2",
				"Test User 3!": "Test User 3",
			});

			expect(localStorage["Comfy.userId"]).toBe("Test User 3!");
			expect(localStorage["Comfy.userName"]).toBe("Test User 3");
		});
		it("allows user selection if no current user but other users", async () => {
			const users = {
				"A!": "A",
				"B!": "B",
				"C!": "C",
			};

			await testUserScreen((selection) => {
				expect(selection.classList.contains("no-users")).toBeFalsy();

				// Check user list
				const select = selection.getElementsByTagName("select")[0];
				const options = select.getElementsByTagName("option");
				expect(
					[...options]
						.filter((o) => !o.disabled)
						.reduce((p, n) => {
							p[n.getAttribute("value")] = n.textContent;
							return p;
						}, {})
				).toStrictEqual(users);

				// Select an option
				select.focus();
				select.value = options[2].value;

				return false;
			}, users);

			expect(users).toStrictEqual(users);

			expect(localStorage["Comfy.userId"]).toBe("B!");
			expect(localStorage["Comfy.userName"]).toBe("B");
		});
		it("doesnt show user screen if current user", async () => {
			const starting = start({
				resetEnv: true,
				userConfig: {
					storage: "server",
					users: {
						"User!": "User",
					},
				},
				localStorage: {
					"Comfy.userId": "User!",
					"Comfy.userName": "User",
				},
			});
			await new Promise(process.nextTick); // wait for promises to resolve

			expectNoUserScreen();

			await starting;
		});
		it("allows user switching", async () => {
			const { app } = await start({
				resetEnv: true,
				userConfig: {
					storage: "server",
					users: {
						"User!": "User",
					},
				},
				localStorage: {
					"Comfy.userId": "User!",
					"Comfy.userName": "User",
				},
			});

			// cant actually test switching user easily but can check the setting is present
			expect(app.ui.settings.settingsLookup["Comfy.SwitchUser"]).toBeTruthy();
		});
	});
	describe("single-user", () => {
		it("doesnt show user creation if no default user", async () => {
			const { app } = await start({
				resetEnv: true,
				userConfig: { migrated: false, storage: "server" },
			});
			expectNoUserScreen();

			// It should store the settings
			const { api } = require("../../web/scripts/api");
			expect(api.storeSettings).toHaveBeenCalledTimes(1);
			expect(api.storeUserData).toHaveBeenCalledTimes(1);
			expect(api.storeUserData).toHaveBeenCalledWith("comfy.templates.json", null, { stringify: false });
			expect(app.isNewUserSession).toBeTruthy();
		});
		it("doesnt show user creation if default user", async () => {
			const { app } = await start({
				resetEnv: true,
				userConfig: { migrated: true, storage: "server" },
			});
			expectNoUserScreen();

			// It should store the settings
			const { api } = require("../../web/scripts/api");
			expect(api.storeSettings).toHaveBeenCalledTimes(0);
			expect(api.storeUserData).toHaveBeenCalledTimes(0);
			expect(app.isNewUserSession).toBeFalsy();
		});
		it("doesnt allow user switching", async () => {
			const { app } = await start({
				resetEnv: true,
				userConfig: { migrated: true, storage: "server" },
			});
			expectNoUserScreen();

			expect(app.ui.settings.settingsLookup["Comfy.SwitchUser"]).toBeFalsy();
		});
	});
	describe("browser-user", () => {
		it("doesnt show user creation if no default user", async () => {
			const { app } = await start({
				resetEnv: true,
				userConfig: { migrated: false, storage: "browser" },
			});
			expectNoUserScreen();

			// It should store the settings
			const { api } = require("../../web/scripts/api");
			expect(api.storeSettings).toHaveBeenCalledTimes(0);
			expect(api.storeUserData).toHaveBeenCalledTimes(0);
			expect(app.isNewUserSession).toBeFalsy();
		});
		it("doesnt show user creation if default user", async () => {
			const { app } = await start({
				resetEnv: true,
				userConfig: { migrated: true, storage: "server" },
			});
			expectNoUserScreen();

			// It should store the settings
			const { api } = require("../../web/scripts/api");
			expect(api.storeSettings).toHaveBeenCalledTimes(0);
			expect(api.storeUserData).toHaveBeenCalledTimes(0);
			expect(app.isNewUserSession).toBeFalsy();
		});
		it("doesnt allow user switching", async () => {
			const { app } = await start({
				resetEnv: true,
				userConfig: { migrated: true, storage: "browser" },
			});
			expectNoUserScreen();

			expect(app.ui.settings.settingsLookup["Comfy.SwitchUser"]).toBeFalsy();
		});
	});
});