Skip to content
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
e62ea91
setting up test structure
O-Bots Nov 7, 2025
0add06b
.
O-Bots Nov 7, 2025
ae75bdb
added playwright config file, deleted original playwright folder and …
O-Bots Nov 7, 2025
b3a6b31
continued test structure setup
O-Bots Nov 7, 2025
d91d2af
Updating test folder structure
O-Bots Nov 7, 2025
68011f8
Merge branch 'main' into main
O-Bots Nov 7, 2025
8671300
Merge branch 'CompassConnections:main' into main
O-Bots Nov 8, 2025
b13b8d4
Merge branch 'CompassConnections:main' into main
O-Bots Nov 11, 2025
266a2b4
Added database seeding script and backend testing folder structure
O-Bots Nov 11, 2025
062b6f2
Merge branch 'main' of https://github.com/O-Bots/Obots_Compass
O-Bots Nov 11, 2025
18f24e2
removed the database test
O-Bots Nov 11, 2025
c949891
Replaced db seeding script
O-Bots Nov 11, 2025
dfd5b6f
Updated userInformation.ts to use values from choices.tsx
O-Bots Nov 11, 2025
8bd9f45
merge prep
O-Bots Nov 15, 2025
834c433
Merge branch 'main' of https://github.com/O-Bots/Obots_Compass
O-Bots Nov 15, 2025
7115c22
removing extra unit test, moving api test to correct folder
O-Bots Nov 15, 2025
c039a10
Merge branch 'CompassConnections:main' into main
O-Bots Nov 15, 2025
750d7c9
Pushing to get help with sql Unit test
O-Bots Nov 17, 2025
a7f36c5
Merge branch 'main' of https://github.com/O-Bots/Obots_Compass
O-Bots Nov 17, 2025
e30eac9
Merge branch 'main' into main
O-Bots Nov 17, 2025
f3f2ebf
Updating get-profiles unit tests
O-Bots Nov 19, 2025
48ef836
Added more unit tests
O-Bots Nov 19, 2025
ea7ef9c
.
O-Bots Nov 20, 2025
443996a
Added more unit tests
O-Bots Nov 21, 2025
10f17af
Added getSupabaseToken unit test
O-Bots Nov 21, 2025
a0e48aa
.
O-Bots Nov 21, 2025
f96c122
excluding supabase token test so ci can pass
O-Bots Nov 22, 2025
2a4b002
.
O-Bots Nov 22, 2025
f9bebe3
Seperated the seedDatabase func into its own file so it can be access…
O-Bots Nov 29, 2025
4cd3327
Fixed failing test
O-Bots Nov 29, 2025
25e4d91
.
O-Bots Nov 29, 2025
6f014bb
.
O-Bots Nov 29, 2025
da0a911
Merge branch 'refs/heads/main' into fork/O-Bots/main
MartinBraquet Nov 29, 2025
d658211
Fix tests
MartinBraquet Nov 29, 2025
d76fd2a
Fix lint
MartinBraquet Nov 29, 2025
0359742
Clean
MartinBraquet Nov 29, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions backend/api/tests/unit/get-profiles.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import * as profilesModule from "api/get-profiles";
import { Profile } from "common/profiles/profile";
import { createSupabaseDirectClient } from "shared/supabase/init";
import { renderSql } from "shared/supabase/sql-builder";

jest.mock("shared/supabase/init")

// describe.skip('getProfiles', () => {
// beforeEach(() => {
// jest.clearAllMocks();
// });

// describe('should fetch the user profiles', () => {
// it('successfully', async ()=> {
// const mockProfiles = [
// {
// diet: ['Jonathon Hammon'],
// has_kids: 0
// },
// {
// diet: ['Joseph Hammon'],
// has_kids: 1
// },
// {
// diet: ['Jolene Hammon'],
// has_kids: 2,
// }
// ] as Profile [];

// jest.spyOn(profilesModule, 'loadProfiles').mockResolvedValue(mockProfiles);

// const props = {
// limit: 2,
// orderBy: "last_online_time" as const,
// };
// const mockReq = {} as any;
// const results = await profilesModule.getProfiles(props, mockReq, mockReq);

// if('continue' in results) {
// throw new Error('Expected direct response')
// };

// expect(results.status).toEqual('success');
// expect(results.profiles).toEqual(mockProfiles);
// expect(results.profiles[0]).toEqual(mockProfiles[0]);
// expect(profilesModule.loadProfiles).toHaveBeenCalledWith(props);
// expect(profilesModule.loadProfiles).toHaveBeenCalledTimes(1);
// });

// it('unsucessfully', async () => {
// jest.spyOn(profilesModule, 'loadProfiles').mockRejectedValue(null);

// const props = {
// limit: 2,
// orderBy: "last_online_time" as const,
// };
// const mockReq = {} as any;
// const results = await profilesModule.getProfiles(props, mockReq, mockReq);

// if('continue' in results) {
// throw new Error('Expected direct response')
// };

// expect(results.status).toEqual('fail');
// expect(results.profiles).toEqual([]);
// expect(profilesModule.loadProfiles).toHaveBeenCalledWith(props);
// expect(profilesModule.loadProfiles).toHaveBeenCalledTimes(1);
// });

// });
// });

describe('loadProfiles', () => {
let mockPg: any;

describe('should', () => {
beforeEach(() => {
mockPg = {
map: jest.fn().mockResolvedValue([]),
};

(createSupabaseDirectClient as jest.Mock).mockReturnValue(mockPg)
jest.clearAllMocks();
});
it('call pg.map with an SQL query', async () => {
await profilesModule.loadProfiles({
limit: 10,
name: 'John',
is_smoker: true,
});

const sqlQuery = mockPg.map.mock.calls
console.log(sqlQuery);

});
});

// describe.skip('should', () => {
// beforeEach(() => {
// mockPg = {
// map: jest.fn(),
// };

// (createSupabaseDirectClient as jest.Mock).mockReturnValue(mockPg)

// jest.clearAllMocks();
// });
// it('return profiles from the database', async () => {
// const mockProfiles = [
// {
// diet: ['Jonathon Hammon'],
// is_smoker: true,
// has_kids: 0
// },
// {
// diet: ['Joseph Hammon'],
// is_smoker: false,
// has_kids: 1
// },
// {
// diet: ['Jolene Hammon'],
// is_smoker: true,
// has_kids: 2,
// }
// ] as Profile [];

// mockPg.map.mockResolvedValue(mockProfiles);
// const props = {} as any;
// const results = await profilesModule.loadProfiles(props);

// expect(results).toEqual(mockProfiles);
// });
// })
})

// const test = profilesModule.loadProfiles({
// limit: 10,
// name: 'Noah Boyer',
// // is_smoker: true,
// // orderBy: 'created_time'
// });
// test.then(res => {console.log(res);
// })
171 changes: 137 additions & 34 deletions backend/api/tests/unit/get-users.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { getUser } from "api/get-user";
import { createSupabaseDirectClient } from "shared/supabase/init";
import { toUserAPIResponse } from "common/api/user-types";
import { convertUser } from "common/supabase/users";
import { APIError } from "common/src/api/utils";
import { APIError } from "common/api/utils";

jest.mock("shared/supabase/init");
jest.mock("common/supabase/users");
jest.mock("common/api/utils");

jest.spyOn(require("common/supabase/users"), 'convertUser')
jest.spyOn(require("common/api/user-types"), 'toUserAPIResponse')

describe('getUser', () =>{
let mockPg: any;

Expand All @@ -19,41 +21,142 @@ describe('getUser', () =>{
jest.clearAllMocks();
});

it('should fetch user successfully by id', async () => {
const mockDbUser = {
created_time: '2025-11-11T16:42:05.188Z',
data: { link: {}, avatarUrl: "", isBannedFromPosting: false },
id: 'feUaIfcxVmJZHJOVVfawLTTPgZiP',
name: 'Franklin Buckridge',
name_username_vector: "'buckridg':2,4 'franklin':1,3",
username: 'Franky_Buck'
};
const mockConvertedUser = {
created_time: new Date(),
id: 'feUaIfcxVmJZHJOVVfawLTTPgZiP',
name: 'Franklin Buckridge',
name_username_vector: "'buckridg':2,4 'franklin':1,3",
username: 'Franky_Buck'
describe('when fetching by id', () => {
it('should fetch user successfully by id', async () => {
const mockDbUser = {
created_time: '2025-11-11T16:42:05.188Z',
data: { link: {}, avatarUrl: "", isBannedFromPosting: false },
id: 'feUaIfcxVmJZHJOVVfawLTTPgZiP',
name: 'Franklin Buckridge',
name_username_vector: "'buckridg':2,4 'franklin':1,3",
username: 'Franky_Buck'
};
const mockConvertedUser = {
created_time: new Date(),
id: 'feUaIfcxVmJZHJOVVfawLTTPgZiP',
name: 'Franklin Buckridge',
name_username_vector: "'buckridg':2,4 'franklin':1,3",
username: 'Franky_Buck'

};
const mockApiResponse = {
created_time: '2025-11-11T16:42:05.188Z',
data: { link: {}, avatarUrl: "", isBannedFromPosting: false },
id: 'feUaIfcxVmJZHJOVVfawLTTPgZiP',
name: 'Franklin Buckridge',
username: 'Franky_Buck'
};

mockPg.oneOrNone.mockImplementation((query: string, values: any[], cb: (value: any) => any) => {
const result = cb(mockDbUser);
return Promise.resolve(result);
});

(convertUser as jest.Mock).mockReturnValue(mockConvertedUser);
( toUserAPIResponse as jest.Mock).mockReturnValue(mockApiResponse);

const result = await getUser({id: 'feUaIfcxVmJZHJOVVfawLTTPgZiP'})

expect(mockPg.oneOrNone).toHaveBeenCalledWith(
expect.stringContaining('where id = $1'),
['feUaIfcxVmJZHJOVVfawLTTPgZiP'],
expect.any(Function)
);

expect(convertUser).toHaveBeenCalledWith(mockDbUser);
expect(toUserAPIResponse).toHaveBeenCalledWith(mockConvertedUser);

expect(result).toEqual(mockApiResponse);

});

};
const mockApiResponse = {
created_time: '2025-11-11T16:42:05.188Z',
data: { link: {}, avatarUrl: "", isBannedFromPosting: false },
id: 'feUaIfcxVmJZHJOVVfawLTTPgZiP',
name: 'Franklin Buckridge',
username: 'Franky_Buck'
};
it('should throw 404 when user is not found by id', async () => {
mockPg.oneOrNone.mockImplementation((query: string, values: any[], cb: (value: any) => any) => {
return Promise.resolve(null);
});

// mockPg.oneOrNone.mockImplementation((query: any, params: any, callback: any) => {
// return Promise.resolve(callback(mockDbUser))
// })
(convertUser as jest.Mock).mockReturnValue(null)

try {
await getUser({id: '3333'});
fail('Should have thrown');
} catch (error) {
const apiError = error as APIError;
expect(apiError.code).toBe(404)
expect(apiError.message).toBe('User not found')
expect(apiError.details).toBeUndefined()
expect(apiError.name).toBe('APIError')
}
})

})

// (convertUser as jest.Mock).mockReturnValue(mockConvertedUser);
// ( toUserAPIResponse as jest.Mock).mockReturnValue(mockApiResponse);
describe('when fetching by username', () => {
it('should fetch user successfully by username', async () => {
const mockDbUser = {
created_time: '2025-11-11T16:42:05.188Z',
data: { link: {}, avatarUrl: "", isBannedFromPosting: false },
id: 'feUaIfcxVmJZHJOVVfawLTTPgZiP',
name: 'Franklin Buckridge',
name_username_vector: "'buckridg':2,4 'franklin':1,3",
username: 'Franky_Buck'
};
const mockConvertedUser = {
created_time: new Date(),
id: 'feUaIfcxVmJZHJOVVfawLTTPgZiP',
name: 'Franklin Buckridge',
name_username_vector: "'buckridg':2,4 'franklin':1,3",
username: 'Franky_Buck'

};
const mockApiResponse = {
created_time: '2025-11-11T16:42:05.188Z',
data: { link: {}, avatarUrl: "", isBannedFromPosting: false },
id: 'feUaIfcxVmJZHJOVVfawLTTPgZiP',
name: 'Franklin Buckridge',
username: 'Franky_Buck'
};

mockPg.oneOrNone.mockImplementation((query: string, values: any[], cb: (value: any) => any) => {
const result = cb(mockDbUser);
return Promise.resolve(result);
});

(convertUser as jest.Mock).mockReturnValue(mockConvertedUser);
(toUserAPIResponse as jest.Mock).mockReturnValue(mockApiResponse);

const result = await getUser({username: 'Franky_Buck'})

expect(mockPg.oneOrNone).toHaveBeenCalledWith(
expect.stringContaining('where username = $1'),
['Franky_Buck'],
expect.any(Function)
);

expect(convertUser).toHaveBeenCalledWith(mockDbUser);
expect(toUserAPIResponse).toHaveBeenCalledWith(mockConvertedUser);

expect(result).toEqual(mockApiResponse);

});

// const result = await getUser({id: 'feUaIfcxVmJZHJOVVfawLTTPgZiP'})
it('should throw 404 when user is not found by id', async () => {
mockPg.oneOrNone.mockImplementation((query: string, values: any[], cb: (value: any) => any) => {
return Promise.resolve(null);
});

// console.log(result);

(convertUser as jest.Mock).mockReturnValue(null)

try {
await getUser({username: '3333'});
fail('Should have thrown');
} catch (error) {
const apiError = error as APIError;
expect(apiError.code).toBe(404)
expect(apiError.message).toBe('User not found')
expect(apiError.details).toBeUndefined()
expect(apiError.name).toBe('APIError')
}
})
})
})