Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Create python infra #51

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .env.development.sample
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ VITE_FIREBASE_STORAGE_BUCKET="sample_storage_bucket"
VITE_FIREBASE_MESSAGING_SENDER_ID="sample_messaging_sender_id"
VITE_FIREBASE_APP_ID="sample_app_id"
VITE_FIREBASE_MEASUREMENT_ID="sample_measurement_id"
VITE_SIGNALING_BACKEND="firestore"
2 changes: 1 addition & 1 deletion jest-setup.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "@testing-library/jest-dom";

jest.mock("./src/services/firestore-connection.ts", () => ({
jest.mock("./src/services/firestore-infra/firestore-connection.ts", () => ({
db: {},
}));

Expand Down
12 changes: 6 additions & 6 deletions src/components/templates/CallTemplate.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import CallTemplate from "./CallTemplate";
import { UserState, userInitialState } from "../../state/user";
import { CallState, callInitialState } from "../../state/call";
import { createCall, createUser } from "../../testing-helpers/call-fixtures";
import firestoreSignaling from "../../services/firestore-signaling";
import signalingBackend from "../../services/signaling-backend";

jest.mock("../../hooks/useRedirectionRule", () =>
jest.fn().mockReturnValue("")
);

jest.mock("../../hooks/useCallUsersListener", () => jest.fn());

jest.mock("../../services/firestore-signaling", () => ({
jest.mock("../../services/signaling-backend", () => ({
leaveCall: jest.fn(),
}));

Expand All @@ -35,8 +35,8 @@ describe("CallTemplate", () => {
};

beforeEach(() => {
(firestoreSignaling.acceptPendingUser as jest.Mock).mockClear();
(firestoreSignaling.rejectPendingUser as jest.Mock).mockClear();
(signalingBackend.acceptPendingUser as jest.Mock).mockClear();
(signalingBackend.rejectPendingUser as jest.Mock).mockClear();
});

it("renders", async () => {
Expand Down Expand Up @@ -102,7 +102,7 @@ describe("CallTemplate", () => {
});

it("leaves call", async () => {
firestoreSignaling.leaveCall = jest.fn().mockResolvedValueOnce(null);
signalingBackend.leaveCall = jest.fn().mockResolvedValueOnce(null);
const { store } = await act(() =>
fullRender(<CallTemplate>Call.</CallTemplate>, {
preloadedState: { user, call },
Expand All @@ -115,7 +115,7 @@ describe("CallTemplate", () => {
});

it("leaves call even if signaling throws error", async () => {
firestoreSignaling.leaveCall = jest.fn().mockImplementationOnce(() => {
signalingBackend.leaveCall = jest.fn().mockImplementationOnce(() => {
throw new Error();
});
const { store } = await act(() =>
Expand Down
12 changes: 6 additions & 6 deletions src/features/call-selection/CallCreationMain.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import CallCreationMain from "./CallCreationMain";
import { act, fireEvent, screen } from "@testing-library/react";
import webrtc from "../../webrtc";
import fullRender from "../../testing-helpers/fullRender";
import firestoreSignaling from "../../services/firestore-signaling";
import signalingBackend from "../../services/signaling-backend";
import { userInitialState } from "../../state/user";
import { devicesInitialState } from "../../state/devices";

Expand All @@ -19,9 +19,9 @@ jest.mock("../../webrtc", () => ({

describe("CallCreationMain", () => {
beforeEach(() => {
(firestoreSignaling.createCall as jest.Mock).mockClear();
(firestoreSignaling.askToJoinCall as jest.Mock).mockClear();
(firestoreSignaling.getIceServersConfig as jest.Mock).mockClear();
(signalingBackend.createCall as jest.Mock).mockClear();
(signalingBackend.askToJoinCall as jest.Mock).mockClear();
(signalingBackend.getIceServersConfig as jest.Mock).mockClear();

(webrtc.retrieveMediaInputs as jest.Mock).mockResolvedValue([
{
Expand Down Expand Up @@ -63,7 +63,7 @@ describe("CallCreationMain", () => {
fireEvent.change(callNameInputElement, { target: { value: "Daily" } });
await act(() => fireEvent.click(callCreationButtonElement));

expect(firestoreSignaling.createCall).toBeCalledTimes(1);
expect(signalingBackend.createCall).toBeCalledTimes(1);
});

it("call creation is integrated with firebase to also gather ice config", async () => {
Expand Down Expand Up @@ -93,7 +93,7 @@ describe("CallCreationMain", () => {
fireEvent.change(callNameInputElement, { target: { value: "Daily" } });
await act(() => fireEvent.click(callCreationButtonElement));

expect(firestoreSignaling.getIceServersConfig).toBeCalledTimes(1);
expect(signalingBackend.getIceServersConfig).toBeCalledTimes(1);
});

it("can not create call if has no device", async () => {
Expand Down
10 changes: 5 additions & 5 deletions src/features/call-selection/CallJoinMain.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import CallJoinMain from "./CallJoinMain";
import { act, fireEvent, screen } from "@testing-library/react";
import webrtc from "../../webrtc";
import fullRender from "../../testing-helpers/fullRender";
import firestoreSignaling from "../../services/firestore-signaling";
import signalingBackend from "../../services/signaling-backend";
import { userInitialState } from "../../state/user";
import { devicesInitialState } from "../../state/devices";

Expand All @@ -19,8 +19,8 @@ jest.mock("../../webrtc", () => ({

describe("CallJoinMain", () => {
beforeEach(() => {
(firestoreSignaling.createCall as jest.Mock).mockClear();
(firestoreSignaling.askToJoinCall as jest.Mock).mockClear();
(signalingBackend.createCall as jest.Mock).mockClear();
(signalingBackend.askToJoinCall as jest.Mock).mockClear();

(webrtc.retrieveMediaInputs as jest.Mock).mockResolvedValue([
{
Expand Down Expand Up @@ -63,8 +63,8 @@ describe("CallJoinMain", () => {
});
await act(() => fireEvent.click(joinCallButtonElement));

expect(firestoreSignaling.askToJoinCall).toHaveBeenCalledTimes(1);
expect(firestoreSignaling.askToJoinCall).toHaveBeenCalledWith({
expect(signalingBackend.askToJoinCall).toHaveBeenCalledTimes(1);
expect(signalingBackend.askToJoinCall).toHaveBeenCalledWith({
callUid: "b385c5fe-5da5-476d-b66f-a4580581be61",
userUid: "1m2kkn3",
userDisplayName: "John Doe",
Expand Down
2 changes: 1 addition & 1 deletion src/features/p2p-call/P2PCallMain.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jest.mock("../../webrtc", () => ({
},
}));

jest.mock("../../services/firestore-signaling", () => ({
jest.mock("../../services/signaling-backend", () => ({
create: jest.fn(),
createCall: jest.fn(),
askToJoinCall: jest.fn(),
Expand Down
6 changes: 3 additions & 3 deletions src/hooks/useCallUsersListener.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect } from "react";
import firestoreSignaling from "../services/firestore-signaling";
import signalingBackend from "../services/signaling-backend";
import { useAppDispatch, useAppSelector } from "../state";
import {
selectCallUid,
Expand All @@ -25,7 +25,7 @@ export default function useCallUsersListener() {
return () => null;
}

return firestoreSignaling.listenCallUsers(callUid, (callUsersResult) =>
return signalingBackend.listenCallUsers(callUid, (callUsersResult) =>
dispatch(setCallUsers(callUsersResult))
);
}, [dispatch, callUid]);
Expand All @@ -35,7 +35,7 @@ export default function useCallUsersListener() {
return () => null;
}

return firestoreSignaling.listenP22Descriptions(
return signalingBackend.listenP22Descriptions(
{ callUid, userUid, participantsUids },
(p2pDescriptions) => dispatch(setP2PDescriptions(p2pDescriptions))
);
Expand Down
2 changes: 1 addition & 1 deletion src/services/firestore-auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jest.mock("uuid", () => ({
v4: jest.fn(),
}));

jest.mock("./firestore-connection", () => ({
jest.mock("./firestore-infra/firestore-connection", () => ({
db: jest.fn(),
}));

Expand Down
2 changes: 1 addition & 1 deletion src/services/firestore-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
signOut,
} from "firebase/auth";
import { v4 as uuidv4 } from "uuid";
import { db, googleAuthProvider } from "./firestore-connection";
import { db, googleAuthProvider } from "./firestore-infra/firestore-connection";
import type { User } from "../webrtc";
import {
DocumentData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,16 @@ import type {
CallParticipant,
CallPendingUser,
CallUser,
} from "../webrtc";

const firestoreSignaling = {
} from "../../webrtc";
import {
CallUserExitIntent,
CallUserJoinIntent,
CallUsersResult,
ParticipantIntent,
SignalingBackend,
} from "../signaling-backend";

const firestoreSignaling: SignalingBackend = {
create,
createCall,
askToJoinCall,
Expand Down Expand Up @@ -78,12 +85,6 @@ export async function createCall(callData: Omit<Call, "uid">): Promise<Call> {
};
}

export interface CallUserJoinIntent {
userUid: string;
userDisplayName: string;
callUid: string;
}

export async function askToJoinCall({
callUid,
userDisplayName,
Expand All @@ -109,12 +110,6 @@ export async function askToJoinCall({
await setDoc(ref, data);
}

export interface ParticipantIntent {
userUid: string;
callUid: string;
participantsUids: string[];
}

export async function joinAsNewerParticipation({
userUid,
callUid,
Expand Down Expand Up @@ -201,11 +196,6 @@ export function listenP22Descriptions(
);
}

export interface CallUsersResult {
participants: CallParticipant[];
pendingUsers: CallPendingUser[];
}

export function listenCallUsers(
callUid: string,
callback: (result: CallUsersResult) => void
Expand Down Expand Up @@ -238,11 +228,6 @@ export async function acceptPendingUser(userUid: string, callUid: string) {
});
}

export interface CallUserExitIntent {
userUid: string;
callUid: string;
}

export async function rejectPendingUser({
callUid,
userUid,
Expand Down
120 changes: 120 additions & 0 deletions src/services/python-infra/python-signaling.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import type { Call, CallP2PDescription } from "../../webrtc";
import {
CallUserExitIntent,
CallUserJoinIntent,
CallUsersResult,
ParticipantIntent,
SignalingBackend,
listenerUnsubscribe,
} from "../signaling-backend";

const pythonSignaling: SignalingBackend = {
create,
createCall,
askToJoinCall,
listenCallUsers,
updateParticipation,
listenP22Descriptions,
joinAsNewerParticipation,
acceptPendingUser,
rejectPendingUser,
leaveCall,
getIceServersConfig,
};

export default pythonSignaling;

export async function create<T extends Record<string, unknown>>(
collectionName: string,
rawCreatingData: T
) {
console.log("create", collectionName, rawCreatingData);
return {
...rawCreatingData,
uid: "e223-4e25-aaca-b7c0ffecd647",
};
}

export async function createCall(callData: Omit<Call, "uid">): Promise<Call> {
return {
...callData,
uid: "e223-4e25-aaca-b7c0ffecd647",
};
}

export async function askToJoinCall({
callUid,
userDisplayName,
userUid,
}: CallUserJoinIntent) {
console.log("askToJoinCall", callUid, userDisplayName, userUid);
return;
}

export async function joinAsNewerParticipation({
userUid,
callUid,
participantsUids,
}: ParticipantIntent): Promise<void> {
console.log("joinAsNewerParticipation", userUid, callUid, participantsUids);
return;
}

export async function updateParticipation({
callUid,
p2pDescription,
}: {
callUid: string;
p2pDescription: Partial<CallP2PDescription>;
}) {
console.log("updateParticipation", callUid, p2pDescription);
return;
}

export function listenP22Descriptions(
{ userUid, callUid, participantsUids }: ParticipantIntent,
listener: (p2pDescriptions: CallP2PDescription[]) => void
): listenerUnsubscribe {
console.log(
"listenP22Descriptions",
userUid,
callUid,
participantsUids,
listener
);
return () => null;
}

export function listenCallUsers(
callUid: string,
callback: (result: CallUsersResult) => void
): listenerUnsubscribe {
console.log("listenCallUsers", callUid, callback);
return () => null;
}

export async function acceptPendingUser(userUid: string, callUid: string) {
console.log("acceptPendingUser", userUid, callUid);
return;
}

export async function rejectPendingUser({
callUid,
userUid,
}: CallUserExitIntent) {
console.log("rejectPendingUser", callUid, userUid);
return;
}

export async function leaveCall({ callUid, userUid }: CallUserExitIntent) {
console.log("leaveCall", callUid, userUid);
return;
}

export async function getIceServersConfig(): Promise<RTCIceServer> {
return DEFAULT_ICE_SERVERS_CONFIG;
}

const DEFAULT_ICE_SERVERS_CONFIG: RTCIceServer = {
urls: ["stun:stun.l.google.com:19302", "stun:stun2.l.google.com:19302"],
};
Loading