fix: multiple bug fixes
isolated users, db backup, weird disconnect issues for long voice sessions,
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
import {
|
||||
defaultIfEmpty,
|
||||
firstValueFrom
|
||||
} from 'rxjs';
|
||||
|
||||
import { type Message } from '../../shared-kernel';
|
||||
import { dispatchIncomingMessage } from './messages-incoming.handlers';
|
||||
|
||||
function createMessage(overrides: Partial<Message> = {}): Message {
|
||||
return {
|
||||
id: 'message-1',
|
||||
roomId: 'room-1',
|
||||
senderId: 'user-1',
|
||||
senderName: 'User 1',
|
||||
content: 'hello',
|
||||
timestamp: 1,
|
||||
reactions: [],
|
||||
isDeleted: false,
|
||||
...overrides
|
||||
};
|
||||
}
|
||||
|
||||
function createContext(overrides: Record<string, unknown> = {}) {
|
||||
return {
|
||||
db: {
|
||||
getMessages: vi.fn()
|
||||
},
|
||||
webrtc: {
|
||||
sendToPeer: vi.fn()
|
||||
},
|
||||
attachments: {},
|
||||
debugging: {},
|
||||
currentUser: null,
|
||||
currentRoom: null,
|
||||
...overrides
|
||||
} as const;
|
||||
}
|
||||
|
||||
describe('dispatchIncomingMessage room-scoped sync', () => {
|
||||
it('requests sync for event room even when another room is viewed', async () => {
|
||||
const getMessages = vi.fn(async (roomId: string) => roomId === 'room-b'
|
||||
? [createMessage({ roomId: 'room-b', timestamp: 10, editedAt: 10 })]
|
||||
: [createMessage({ roomId: 'room-a', timestamp: 100, editedAt: 100 })]);
|
||||
const sendToPeer = vi.fn();
|
||||
const context = createContext({
|
||||
db: { getMessages },
|
||||
webrtc: { sendToPeer },
|
||||
currentRoom: { id: 'room-a' }
|
||||
});
|
||||
|
||||
await firstValueFrom(
|
||||
dispatchIncomingMessage(
|
||||
{
|
||||
type: 'chat-sync-summary',
|
||||
roomId: 'room-b',
|
||||
fromPeerId: 'peer-1',
|
||||
count: 2,
|
||||
lastUpdated: 20
|
||||
} as never,
|
||||
context as never
|
||||
).pipe(defaultIfEmpty(null))
|
||||
);
|
||||
|
||||
expect(getMessages).toHaveBeenCalledWith('room-b', expect.any(Number), 0);
|
||||
expect(sendToPeer).toHaveBeenCalledWith('peer-1', {
|
||||
type: 'chat-sync-request',
|
||||
roomId: 'room-b'
|
||||
});
|
||||
});
|
||||
|
||||
it('sends full sync for requested room even when another room is viewed', async () => {
|
||||
const roomBMessages = [
|
||||
createMessage({ id: 'message-b1', roomId: 'room-b', timestamp: 5 }),
|
||||
createMessage({ id: 'message-b2', roomId: 'room-b', timestamp: 15 })
|
||||
];
|
||||
const getMessages = vi.fn(async (roomId: string) => roomId === 'room-b'
|
||||
? roomBMessages
|
||||
: [createMessage({ id: 'message-a1', roomId: 'room-a', timestamp: 200 })]);
|
||||
const sendToPeer = vi.fn();
|
||||
const context = createContext({
|
||||
db: { getMessages },
|
||||
webrtc: { sendToPeer },
|
||||
currentRoom: { id: 'room-a' }
|
||||
});
|
||||
|
||||
await firstValueFrom(
|
||||
dispatchIncomingMessage(
|
||||
{
|
||||
type: 'chat-sync-request',
|
||||
roomId: 'room-b',
|
||||
fromPeerId: 'peer-2'
|
||||
} as never,
|
||||
context as never
|
||||
).pipe(defaultIfEmpty(null))
|
||||
);
|
||||
|
||||
expect(getMessages).toHaveBeenCalledWith('room-b', expect.any(Number), 0);
|
||||
expect(sendToPeer).toHaveBeenCalledWith('peer-2', {
|
||||
type: 'chat-sync-full',
|
||||
roomId: 'room-b',
|
||||
messages: roomBMessages
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -538,12 +538,14 @@ function handleSyncSummary(
|
||||
event: IncomingMessageEvent,
|
||||
{ db, webrtc, currentRoom }: IncomingMessageContext
|
||||
): Observable<Action> {
|
||||
if (!currentRoom)
|
||||
const targetRoomId = event.roomId || currentRoom?.id;
|
||||
|
||||
if (!targetRoomId)
|
||||
return EMPTY;
|
||||
|
||||
return from(
|
||||
(async () => {
|
||||
const local = await db.getMessages(currentRoom.id, FULL_SYNC_LIMIT, 0);
|
||||
const local = await db.getMessages(targetRoomId, FULL_SYNC_LIMIT, 0);
|
||||
const localCount = local.length;
|
||||
const localLastUpdated = local.reduce(
|
||||
(maxTimestamp, message) => Math.max(maxTimestamp, message.editedAt || message.timestamp || 0),
|
||||
@@ -561,7 +563,7 @@ function handleSyncSummary(
|
||||
if (!identical && needsSync && fromPeerId) {
|
||||
const syncRequestEvent: ChatEvent = {
|
||||
type: 'chat-sync-request',
|
||||
roomId: currentRoom.id
|
||||
roomId: targetRoomId
|
||||
};
|
||||
|
||||
webrtc.sendToPeer(fromPeerId, syncRequestEvent);
|
||||
@@ -575,17 +577,18 @@ function handleSyncRequest(
|
||||
event: IncomingMessageEvent,
|
||||
{ db, webrtc, currentRoom }: IncomingMessageContext
|
||||
): Observable<Action> {
|
||||
const targetRoomId = event.roomId || currentRoom?.id;
|
||||
const fromPeerId = event.fromPeerId;
|
||||
|
||||
if (!currentRoom || !fromPeerId)
|
||||
if (!targetRoomId || !fromPeerId)
|
||||
return EMPTY;
|
||||
|
||||
return from(
|
||||
(async () => {
|
||||
const all = await db.getMessages(currentRoom.id, FULL_SYNC_LIMIT, 0);
|
||||
const all = await db.getMessages(targetRoomId, FULL_SYNC_LIMIT, 0);
|
||||
const syncFullEvent: ChatEvent = {
|
||||
type: 'chat-sync-full',
|
||||
roomId: currentRoom.id,
|
||||
roomId: targetRoomId,
|
||||
messages: all
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user