Skip to content

Commit ba54f83

Browse files
committed
test(app-shell): the reaction records pin removes through the unreact door
Claude-Session: https://claude.ai/code/session_01ADHkmPysaCwFvT4Pqb88z8 Co-authored-by: Claude <noreply@anthropic.com>
1 parent 9f62e06 commit ba54f83

1 file changed

Lines changed: 44 additions & 12 deletions

File tree

‎ui/packages/app-shell/src/views/RecordDetailView.reactionRecords-12078.test.tsx‎

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
* left only the later write stored. Now, where the deployment has the object,
1111
* the chatter reads every comment's reaction rows in one batched `comment_id`
1212
* `$in` read, groups them into the shape the panel already renders, and a click
13-
* creates or deletes the clicker's own row. Where the deployment has no such
13+
* creates the clicker's own row, or removes it through the unreact door
14+
* (`DELETE /api/v1/comments/reactions/:id`, objectstack#26346). Where the deployment has no such
1415
* object (a framework that predates it), the column path stays, unchanged: the
1516
* `objectui#11019` / `objectui#10899` / `objectui#11035` pins beside this file
1617
* run on that path.
@@ -20,7 +21,8 @@
2021
* source is a fake SERVER shared by every mounted view: each view's adapter is
2122
* bound to its signed-in member, and the server keeps the object's declared
2223
* rules: `user_id` stamped from that member, one row per (comment, emoji,
23-
* user), and a member deletes only their own row. Which store the page uses is
24+
* user), and a member removes only their own row, through the unreact door
25+
* the page's authenticated fetch reaches. Which store the page uses is
2426
* read from the object registry the page's metadata context serves.
2527
*/
2628

@@ -36,6 +38,9 @@ import { toast } from 'sonner';
3638
/** The signed-in member is read from a context, so two views can be two members at once. */
3739
const signedIn = vi.hoisted(() => ({ ctx: null as unknown as React.Context<string> }));
3840

41+
/** The unreact door of the member a view is being mounted as: the page's authenticated fetch reaches it. */
42+
const mounting = vi.hoisted(() => ({ door: null as unknown as (url: string, init?: RequestInit) => Promise<Response> }));
43+
3944
vi.mock('@object-ui/auth', async (importOriginal) => {
4045
const ReactModule = await import('react');
4146
signedIn.ctx = ReactModule.createContext('u1');
@@ -45,7 +50,12 @@ vi.mock('@object-ui/auth', async (importOriginal) => {
4550
const id = ReactModule.useContext(signedIn.ctx);
4651
return { user: { id, name: id, image: null }, activeOrganization: null };
4752
},
48-
createAuthenticatedFetch: () => vi.fn(),
53+
// Only the unreact door is answered; every other call answers nothing, as before.
54+
createAuthenticatedFetch: () => {
55+
const door = mounting.door;
56+
return (url: string, init?: RequestInit) =>
57+
String(url).includes('/api/v1/comments/reactions/') ? door(url, init) : undefined;
58+
},
4959
};
5060
});
5161

@@ -136,7 +146,8 @@ const refusal = (status: number, code: string) =>
136146
/**
137147
* One server shared by every view a case mounts. `as(member)` is that member's
138148
* adapter: what they create is stamped as theirs, and they delete only rows
139-
* that are theirs.
149+
* that are theirs. Its `unreact` is that member's unreact door: it removes the
150+
* member's own row and refuses anyone else's, as the server's door does.
140151
*/
141152
function makeServer(init: { comments?: CommentRow[]; reactions?: Omit<ReactionRow, 'id' | 'created_at'>[] } = {}) {
142153
const comments: CommentRow[] = init.comments ?? [
@@ -192,6 +203,18 @@ function makeServer(init: { comments?: CommentRow[]; reactions?: Omit<ReactionRo
192203
reactions.splice(at, 1);
193204
return true;
194205
}),
206+
unreact: vi.fn(async (url: string, init?: RequestInit) => {
207+
const answer = (status: number, body: unknown) =>
208+
new Response(JSON.stringify(body), { status, headers: { 'content-type': 'application/json' } });
209+
const id = decodeURIComponent(String(url).split('/api/v1/comments/reactions/')[1]);
210+
if (init?.method !== 'DELETE') return answer(404, { success: false });
211+
const at = reactions.findIndex((r) => r.id === id);
212+
if (at < 0) return answer(404, { success: false, error: { code: 'RECORD_NOT_FOUND' } });
213+
// The door's own rule: the row's reactor is the caller.
214+
if (reactions[at].user_id !== member) return answer(403, { success: false, error: { code: 'PERMISSION_DENIED' } });
215+
reactions.splice(at, 1);
216+
return answer(200, { success: true, data: { id, deleted: true } });
217+
}),
195218
});
196219

197220
return { comments, reactions, as };
@@ -234,6 +257,7 @@ function tree(member: string, dataSource: any, metadata: any) {
234257

235258
/** Mounts the record page as `member` and waits for the comment to show. */
236259
async function mountAs(member: string, dataSource: any, objects: unknown[] = REGISTRY.present) {
260+
mounting.door = dataSource.unreact;
237261
const view = render(tree(member, dataSource, metadataFor(objects)));
238262
await within(view.container).findByText(COMMENT_BODY);
239263
// Let the reaction read that follows the comment read land.
@@ -283,6 +307,11 @@ async function click(run: () => void) {
283307
await act(async () => {});
284308
}
285309

310+
/** The row ids a member's take-backs removed through the unreact door, in order. */
311+
function unreacted(dataSource: any): string[] {
312+
return dataSource.unreact.mock.calls.map(([url]: [string]) => decodeURIComponent(url.split('/').pop() as string));
313+
}
314+
286315
/** Every `sys_comment_reaction` read a data source was asked, as its `$in` id lists. */
287316
function reactionReads(dataSource: any): string[][] {
288317
return dataSource.find.mock.calls
@@ -332,7 +361,7 @@ describe("a comment's reactions are each member's own sys_comment_reaction recor
332361
expect(isOwn(again, '👍')).toBe(true);
333362
});
334363

335-
it('a second click removes it: the clicker deletes their own row, by the id the create returned and by the id the read returned', async () => {
364+
it('a second click removes it: the clicker removes their own row through the unreact door, by the id the create returned and by the id the read returned', async () => {
336365
const server = makeServer({ reactions: [{ comment_id: COMMENT_ID, emoji: '👍', user_id: 'u2' }] });
337366
const u1 = server.as('u1');
338367
const view = await mountAs('u1', u1);
@@ -342,11 +371,12 @@ describe("a comment's reactions are each member's own sys_comment_reaction recor
342371
expect(chips(view)).toEqual(['👍 1', '🎉 1']);
343372

344373
await click(() => fireEvent.click(chip(view, '🎉')));
345-
expect(u1.delete.mock.calls).toEqual([[REACTION, created.id]]);
374+
expect(unreacted(u1)).toEqual([created.id]);
375+
expect(u1.delete).not.toHaveBeenCalled();
346376
expect(server.reactions.map((r) => [r.emoji, r.user_id])).toEqual([['👍', 'u2']]);
347377
expect(chips(view)).toEqual(['👍 1']);
348378

349-
// A row the read found: its id is the one the take-back deletes.
379+
// A row the read found: its id is the one the take-back removes.
350380
cleanup();
351381
const before = makeServer({
352382
reactions: [
@@ -359,7 +389,8 @@ describe("a comment's reactions are each member's own sys_comment_reaction recor
359389
const read = await mountAs('u1', reader);
360390
expect(chips(read)).toEqual(['👍 2']);
361391
await click(() => fireEvent.click(chip(read, '👍')));
362-
expect(reader.delete.mock.calls).toEqual([[REACTION, mine.id]]);
392+
expect(unreacted(reader)).toEqual([mine.id]);
393+
expect(reader.delete).not.toHaveBeenCalled();
363394
expect(before.reactions.map((r) => r.user_id)).toEqual(['u2']);
364395
expect(chips(read)).toEqual(['👍 1']);
365396
expect(toast.error).not.toHaveBeenCalled();
@@ -452,7 +483,7 @@ describe("a comment's reactions are each member's own sys_comment_reaction recor
452483
expect(server.reactions.map((r) => r.user_id)).toEqual(['u2']);
453484
});
454485

455-
it('a take-back clicked before the create answers deletes the row that create made', async () => {
486+
it('a take-back clicked before the create answers removes the row that create made', async () => {
456487
const server = makeServer();
457488
const u1 = server.as('u1');
458489
let answer!: () => void;
@@ -471,11 +502,12 @@ describe("a comment's reactions are each member's own sys_comment_reaction recor
471502
expect(chips(view)).toEqual([]);
472503
await act(async () => {});
473504
// The take-back waits for the row it takes back.
474-
expect(u1.delete).not.toHaveBeenCalled();
505+
expect(u1.unreact).not.toHaveBeenCalled();
475506

476507
await act(async () => answer());
477-
await waitFor(() => expect(u1.delete).toHaveBeenCalledTimes(1));
478-
expect(u1.delete.mock.calls[0]).toEqual([REACTION, 'r1']);
508+
await waitFor(() => expect(u1.unreact).toHaveBeenCalledTimes(1));
509+
expect(unreacted(u1)).toEqual(['r1']);
510+
expect(u1.delete).not.toHaveBeenCalled();
479511
expect(server.reactions).toEqual([]);
480512
expect(chips(view)).toEqual([]);
481513
expect(toast.error).not.toHaveBeenCalled();

0 commit comments

Comments
 (0)