-
-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
51 changed files
with
430 additions
and
383 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,7 @@ vi.mock('@/database/server/core/db', async () => ({ | |
|
||
const userId = 'user-db'; | ||
const userEmail = '[email protected]'; | ||
const userModel = new UserModel(); | ||
const userModel = new UserModel(serverDB, userId); | ||
|
||
beforeEach(async () => { | ||
await serverDB.delete(users); | ||
|
@@ -44,14 +44,14 @@ describe('UserModel', () => { | |
email: '[email protected]', | ||
}; | ||
|
||
await UserModel.createUser(params); | ||
await UserModel.createUser(serverDB, params); | ||
|
||
const user = await serverDB.query.users.findFirst({ where: eq(users.id, userId) }); | ||
expect(user).not.toBeNull(); | ||
expect(user?.username).toBe('testuser'); | ||
expect(user?.email).toBe('[email protected]'); | ||
|
||
const sessionModel = new SessionModel(userId); | ||
const sessionModel = new SessionModel(serverDB, userId); | ||
const inbox = await sessionModel.findByIdOrSlug(INBOX_SESSION_ID); | ||
expect(inbox).not.toBeNull(); | ||
}); | ||
|
@@ -61,7 +61,7 @@ describe('UserModel', () => { | |
it('should delete a user', async () => { | ||
await serverDB.insert(users).values({ id: userId }); | ||
|
||
await UserModel.deleteUser(userId); | ||
await UserModel.deleteUser(serverDB, userId); | ||
|
||
const user = await serverDB.query.users.findFirst({ where: eq(users.id, userId) }); | ||
expect(user).toBeUndefined(); | ||
|
@@ -72,7 +72,7 @@ describe('UserModel', () => { | |
it('should find a user by ID', async () => { | ||
await serverDB.insert(users).values({ id: userId, username: 'testuser' }); | ||
|
||
const user = await UserModel.findById(userId); | ||
const user = await UserModel.findById(serverDB, userId); | ||
|
||
expect(user).not.toBeNull(); | ||
expect(user?.id).toBe(userId); | ||
|
@@ -84,7 +84,7 @@ describe('UserModel', () => { | |
it('should find a user by email', async () => { | ||
await serverDB.insert(users).values({ id: userId, email: userEmail }); | ||
|
||
const user = await UserModel.findByEmail(userEmail); | ||
const user = await UserModel.findByEmail(serverDB, userEmail); | ||
|
||
expect(user).not.toBeNull(); | ||
expect(user?.id).toBe(userId); | ||
|
@@ -107,23 +107,25 @@ describe('UserModel', () => { | |
keyVaults: encryptedKeyVaults, | ||
}); | ||
|
||
const state = await userModel.getUserState(userId); | ||
const state = await userModel.getUserState(); | ||
|
||
expect(state.userId).toBe(userId); | ||
expect(state.preference).toEqual(preference); | ||
expect(state.settings.keyVaults).toEqual(keyVaults); | ||
}); | ||
|
||
it('should throw an error if user not found', async () => { | ||
await expect(userModel.getUserState('invalid-user-id')).rejects.toThrow('user not found'); | ||
const userModel = new UserModel(serverDB, 'invalid-user-id'); | ||
|
||
await expect(userModel.getUserState()).rejects.toThrow('user not found'); | ||
}); | ||
}); | ||
|
||
describe('updateUser', () => { | ||
it('should update user fields', async () => { | ||
await serverDB.insert(users).values({ id: userId, username: 'oldname' }); | ||
|
||
await userModel.updateUser(userId, { username: 'newname' }); | ||
await userModel.updateUser({ username: 'newname' }); | ||
|
||
const updatedUser = await serverDB.query.users.findFirst({ | ||
where: eq(users.id, userId), | ||
|
@@ -137,7 +139,7 @@ describe('UserModel', () => { | |
await serverDB.insert(users).values({ id: userId }); | ||
await serverDB.insert(userSettings).values({ id: userId }); | ||
|
||
await userModel.deleteSetting(userId); | ||
await userModel.deleteSetting(); | ||
|
||
const settings = await serverDB.query.userSettings.findFirst({ | ||
where: eq(users.id, userId), | ||
|
@@ -155,7 +157,7 @@ describe('UserModel', () => { | |
} as UserSettings; | ||
await serverDB.insert(users).values({ id: userId }); | ||
|
||
await userModel.updateSetting(userId, settings); | ||
await userModel.updateSetting(settings); | ||
|
||
const updatedSettings = await serverDB.query.userSettings.findFirst({ | ||
where: eq(users.id, userId), | ||
|
@@ -178,7 +180,7 @@ describe('UserModel', () => { | |
const newSettings = { | ||
general: { fontSize: 16, language: 'zh-CN', themeMode: 'dark' }, | ||
} as UserSettings; | ||
await userModel.updateSetting(userId, newSettings); | ||
await userModel.updateSetting(newSettings); | ||
|
||
const updatedSettings = await serverDB.query.userSettings.findFirst({ | ||
where: eq(users.id, userId), | ||
|
@@ -195,7 +197,7 @@ describe('UserModel', () => { | |
const newPreference: Partial<UserPreference> = { | ||
guide: { topic: true, moveSettingsToAvatar: true }, | ||
}; | ||
await userModel.updatePreference(userId, newPreference); | ||
await userModel.updatePreference(newPreference); | ||
|
||
const updatedUser = await serverDB.query.users.findFirst({ where: eq(users.id, userId) }); | ||
expect(updatedUser?.preference).toEqual({ ...preference, ...newPreference }); | ||
|
@@ -212,7 +214,7 @@ describe('UserModel', () => { | |
moveSettingsToAvatar: true, | ||
uploadFileInKnowledgeBase: true, | ||
}; | ||
await userModel.updateGuide(userId, newGuide); | ||
await userModel.updateGuide(newGuide); | ||
|
||
const updatedUser = await serverDB.query.users.findFirst({ where: eq(users.id, userId) }); | ||
expect(updatedUser?.preference).toEqual({ ...preference, guide: newGuide }); | ||
|
Oops, something went wrong.