Fix chat visibility and conversation sync
This commit is contained in:
1 parent
f4e8fdc42f
commit
34ae532cdd
10 files changed
+416
-23
No files matched your search
@@ -6,12 +6,16 @@ import { createConversationsRouter } from '../../src/web/routes/conversations.js
|
||||
|
||||
const servers: Array<ReturnType<express.Express['listen']>> = [];
|
||||
|
||||
async function request(app: express.Express, path: string): Promise<Response> {
|
||||
async function request(
|
||||
app: express.Express,
|
||||
path: string,
|
||||
init?: RequestInit,
|
||||
): Promise<Response> {
|
||||
const server = app.listen(0);
|
||||
servers.push(server);
|
||||
await new Promise<void>((resolve) => server.once('listening', resolve));
|
||||
const { port } = server.address() as AddressInfo;
|
||||
return fetch(`http://127.0.0.1:${port}${path}`);
|
||||
return fetch(`http://127.0.0.1:${port}${path}`, init);
|
||||
}
|
||||
|
||||
afterEach(async () => {
|
||||
@@ -177,4 +181,48 @@ describe('conversations route', () => {
|
||||
});
|
||||
expect(context.repos.messages.getLatestByRecipient).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('waits for the conversation delta sync after sending a reply', async () => {
|
||||
const conversation = {
|
||||
username: 'alice', roomName: 'alice', roomId: 'room-alice',
|
||||
url: 'https://chat.reddit.com/room/room-alice',
|
||||
lastMessageTime: 10, lastMessageSnippet: 'Hello', isFromUs: false, unread: false,
|
||||
messages: [{ sender: 'alice', text: 'Hello', timestamp: 10, isSelf: false }],
|
||||
};
|
||||
let completeSync: (() => void) | undefined;
|
||||
const refreshTopChats = vi.fn(() => new Promise<void>((resolve) => {
|
||||
completeSync = resolve;
|
||||
}));
|
||||
const context = {
|
||||
logger: { error: vi.fn() },
|
||||
activity: { push: vi.fn() },
|
||||
broadcaster: { push: vi.fn() },
|
||||
services: { chat: {
|
||||
getCachedConversations: vi.fn().mockResolvedValue([conversation]),
|
||||
sendChatMessage: vi.fn().mockResolvedValue(true),
|
||||
refreshTopChats,
|
||||
} },
|
||||
} as unknown as WebContext;
|
||||
const app = express()
|
||||
.use(express.json())
|
||||
.use('/api', createConversationsRouter(context));
|
||||
|
||||
let responseSettled = false;
|
||||
const responsePromise = request(app, '/api/conversations/room-alice/send-reply', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ message: 'AI-assisted reply' }),
|
||||
}).then((response) => {
|
||||
responseSettled = true;
|
||||
return response;
|
||||
});
|
||||
|
||||
await vi.waitFor(() => expect(refreshTopChats).toHaveBeenCalledWith(5));
|
||||
expect(responseSettled).toBe(false);
|
||||
completeSync?.();
|
||||
|
||||
const response = await responsePromise;
|
||||
expect(response.status).toBe(200);
|
||||
expect(await response.json()).toEqual({ success: true });
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user