import { test, expect } from "./fixtures"; import { freshAccount, loginViaUi, pickBottle } from "./helpers"; test.describe("bottle social loop", () => { test("throws, picks, replies and chats in real time with two users", async ({ browser, }) => { const aliceAccount = freshAccount("131"); const bobAccount = freshAccount("132"); const alice = await browser.newContext(); const bob = await browser.newContext(); const aPage = await alice.newPage(); const bPage = await bob.newPage(); await loginViaUi(aPage, aliceAccount); await loginViaUi(bPage, bobAccount); // Alice 投瓶 await aPage.goto("/throw"); await aPage .getByPlaceholder("写下你想让陌生人看到的话…") .fill("你好,海上的陌生人,愿你被温柔接住。"); await aPage.getByRole("button", { name: /扔进海里/ }).click(); await expect(aPage.getByText(/瓶子已进入审核/)).toBeVisible(); // Bob 捞瓶(过滤掉 Alice 已投的,应捞到池中瓶子) await bPage.goto("/pick"); await pickBottle(bPage); const content = (await bPage.locator("blockquote").innerText()).trim(); expect(content.length).toBeGreaterThan(0); // Bob 首次回复建立会话 await bPage .locator(".bottle-card textarea") .fill("很高兴遇见你,来自另一片海域。"); await bPage.getByRole("button", { name: /回复并建立会话/ }).click(); await expect(bPage).toHaveURL(/\/conversations\//, { timeout: 15_000 }); await expect( bPage.getByText("很高兴遇见你,来自另一片海域。"), ).toBeVisible(); // Alice 会话列表出现新会话 await aPage.goto("/conversations"); await expect(aPage.locator(".conversation")).toHaveCount(1, { timeout: 15_000, }); // Bob 发送实时消息,两端都能看到 await bPage.locator(".composer textarea").fill("今天过得怎么样?"); await bPage.getByRole("button", { name: /发送消息/ }).click(); await expect(bPage.getByText("今天过得怎么样?")).toBeVisible(); await aPage.goto("/conversations"); await aPage.locator(".conversation").first().click(); await expect(aPage.getByText("今天过得怎么样?")).toBeVisible({ timeout: 15_000, }); await alice.close(); await bob.close(); }); test("replies once and deduplicates concurrent history sync", async ({ browser, }) => { const aAccount = freshAccount("133"); const bAccount = freshAccount("134"); const ctxA = await browser.newContext(); const ctxB = await browser.newContext(); const pageA = await ctxA.newPage(); const pageB = await ctxB.newPage(); await loginViaUi(pageA, aAccount); await loginViaUi(pageB, bAccount); // 两个瓶子避免同瓶并发 await pageA.goto("/throw"); await pageA .getByPlaceholder("写下你想让陌生人看到的话…") .fill(`sync 瓶子 A ${Date.now()}`); await pageA.getByRole("button", { name: /扔进海里/ }).click(); await expect(pageA.getByText(/瓶子已进入审核/)).toBeVisible(); await pageA.goto("/throw"); await pageA .getByPlaceholder("写下你想让陌生人看到的话…") .fill(`sync 瓶子 B ${Date.now()}`); await pageA.getByRole("button", { name: /扔进海里/ }).click(); await expect(pageA.getByText(/瓶子已进入审核/)).toBeVisible(); // B 连续捞两只瓶子并各回复一次 for (let i = 0; i < 2; i++) { await pageB.goto("/pick"); await pickBottle(pageB); await pageB.locator(".bottle-card textarea").fill(`回复-${i}:海上的你好`); await pageB.getByRole("button", { name: /回复并建立会话/ }).click(); await expect(pageB).toHaveURL(/\/conversations\//, { timeout: 15_000, }); } // A 端应看到两个会话 await pageA.goto("/conversations"); await expect(pageA.locator(".conversation")).toHaveCount(2, { timeout: 15_000, }); await ctxA.close(); await ctxB.close(); }); });