3dom4wf7p: Poll the feed before reporting a publish failure
A freshly published post can take a moment to surface on the home feed, so the single immediate confirm reload reported a false failure even though the post went through. Retry the reload a few times; once the server has accepted the publish, treat a not-yet-visible post as success instead of an error.
This commit is contained in:
+33
-4
@@ -6,6 +6,7 @@ import { POST } from "@/app/api/posts/route";
|
||||
import { Composer } from "@/components/Composer";
|
||||
import { FeedView } from "@/components/FeedView";
|
||||
import { PostError, publishAndConfirm, sendPost } from "@/lib/post-client";
|
||||
import { FeedLoadError } from "@/lib/feed-client";
|
||||
import { createSession } from "@/lib/session-store";
|
||||
|
||||
function session() {
|
||||
@@ -84,14 +85,42 @@ describe("publishAndConfirm", () => {
|
||||
expect(state.id).toBeNull();
|
||||
});
|
||||
|
||||
it("throws and keeps the id when the post is not on the page, then a retry does not create again", async () => {
|
||||
const nap = { delayMs: 0, sleep: async () => {} };
|
||||
|
||||
it("retries the reload and succeeds once a slow post surfaces", async () => {
|
||||
const state = { id: null as string | null };
|
||||
const send = vi.fn(async () => "p9");
|
||||
const miss = async () => ({ items: [item("p1")], nextCursor: null });
|
||||
await expect(publishAndConfirm("hi", null, state, { send, fetchPage: miss })).rejects.toBeInstanceOf(PostError);
|
||||
let n = 0;
|
||||
const fetchPage = vi.fn(async () =>
|
||||
n++ < 2 ? { items: [item("p1")], nextCursor: null } : { items: [item("p9"), item("p1")], nextCursor: null },
|
||||
);
|
||||
const got = await publishAndConfirm("hi", null, state, { send, fetchPage, confirm: nap });
|
||||
expect(got.items.map((i) => (i as { post: { id: string } }).post.id)).toContain("p9");
|
||||
expect(state.id).toBeNull();
|
||||
expect(send).toHaveBeenCalledTimes(1);
|
||||
expect(n).toBe(3);
|
||||
});
|
||||
|
||||
it("does not throw or duplicate when a published post never surfaces (feed lag)", async () => {
|
||||
const state = { id: null as string | null };
|
||||
const send = vi.fn(async () => "p9");
|
||||
const miss = vi.fn(async () => ({ items: [item("p1")], nextCursor: null }));
|
||||
const got = await publishAndConfirm("hi", null, state, { send, fetchPage: miss, confirm: { ...nap, attempts: 3 } });
|
||||
expect(got).toBeTruthy();
|
||||
expect(state.id).toBeNull();
|
||||
expect(send).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("keeps the id when the reload itself fails, so a retry re-checks without creating again", async () => {
|
||||
const state = { id: null as string | null };
|
||||
const send = vi.fn(async () => "p9");
|
||||
const boom = async () => {
|
||||
throw new FeedLoadError(false);
|
||||
};
|
||||
await expect(publishAndConfirm("hi", null, state, { send, fetchPage: boom, confirm: nap })).rejects.toBeInstanceOf(PostError);
|
||||
expect(state.id).toBe("p9");
|
||||
const hit = async () => ({ items: [item("p9")], nextCursor: null });
|
||||
await publishAndConfirm("hi", null, state, { send, fetchPage: hit });
|
||||
await publishAndConfirm("hi", null, state, { send, fetchPage: hit, confirm: nap });
|
||||
expect(send).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user