Skip to content

Commit

Permalink
draft relatinship test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lalalune committed Feb 14, 2024
1 parent 5207722 commit 8ecef82
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
80 changes: 80 additions & 0 deletions src/lib/__tests__/relationships.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { type User } from '@supabase/supabase-js';
import { type UUID } from 'crypto';
import dotenv from 'dotenv';
import { createRuntime } from '../../test/createRuntime'; // Adjust the import path as needed
import {
createRelationship,
getRelationship,
getRelationships
} from '../relationships'; // Adjust the import path as needed
import { BgentRuntime } from '../runtime';

dotenv.config();

const zeroUuid = "00000000-0000-0000-0000-000000000000";

describe('Relationships Module', () => {
let runtime: BgentRuntime;
let user: User;

beforeAll(async () => {
const setup = await createRuntime(process.env as Record<string, string>);
runtime = setup.runtime;
user = setup.user as User;
});

test('createRelationship creates a new relationship', async () => {
const userA = user?.id as UUID;
const userB = zeroUuid;

const relationship = await createRelationship({ supabase: runtime.supabase, userA, userB });

expect(relationship).toBeDefined();

if (relationship?.length === 0) {
throw new Error('No relationship was created');
}

expect(relationship[0].user_a).toBe(userA);
expect(relationship[0].user_b).toBe(userB);
});

test('getRelationship retrieves an existing relationship', async () => {
const userA = user?.id as UUID;
const userB = zeroUuid;

await createRelationship({ supabase: runtime.supabase, userA, userB });

const relationship = await getRelationship({ supabase: runtime.supabase, userA, userB });
expect(relationship).toBeDefined();
expect(relationship.user_a).toBe(userA);
expect(relationship.user_b).toBe(userB);
});

test('getRelationships retrieves all relationships for a user', async () => {
const userA = user?.id as UUID;
const userB = zeroUuid;

await createRelationship({ supabase: runtime.supabase, userA, userB });

const relationships = await getRelationships({ supabase: runtime.supabase, userId: userA });
expect(relationships).toBeDefined();
expect(relationships.length).toBeGreaterThan(0);
expect(relationships.some(r => r.user_a === userA || r.user_b === userA)).toBeTruthy();
});

// test('formatRelationships formats relationships correctly', async () => {
// const userA = uuidv4();
// const userB = uuidv4();
// const userC = uuidv4();

// await createRelationship({ supabase, userA, userB });
// await createRelationship({ supabase, userA, userC });

// const formattedRelationships = await formatRelationships({ supabase, userId: userA });
// expect(formattedRelationships).toBeDefined();
// expect(formattedRelationships.length).toBeGreaterThan(0);
// expect(formattedRelationships).toContain(userB);
// expect(formattedRelationships).toContain(userC);
// });
});
4 changes: 2 additions & 2 deletions src/lib/relationships.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export async function createRelationship({
supabase: SupabaseClient;
userA: UUID;
userB: UUID;
}) {
}): Promise<Relationship[]> {
const { data, error } = await supabase.from("relationships").upsert({
user_a: userA,
user_b: userB,
Expand All @@ -20,7 +20,7 @@ export async function createRelationship({
throw new Error(error.message);
}

return data;
return data as unknown as Relationship[];
}

export async function getRelationship({
Expand Down

0 comments on commit 8ecef82

Please sign in to comment.