Skip to content

Commit bf102e3

Browse files
committed
test(dogfood): the timeline count against the separate reads, as two members
Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QsR2jSrQVdYaC3GSLbQCFd
1 parent 7b8d1e0 commit bf102e3

3 files changed

Lines changed: 171 additions & 0 deletions

File tree

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
//
3+
// [#28737] The record page payload's `timeline` part — the size of the
4+
// record's 动态 stream — through the real HTTP + security stack, on an
5+
// org-bound boot with plugin-audit's real writers: the comments, the activity
6+
// mirror and the ledger rows are the ones the member's own writes produce.
7+
//
8+
// The count is of rows the caller could read, and of no other: for each
9+
// member it equals the sum of the separate list reads it stands for, asked as
10+
// that member (the spec's timeline table). A member who may read the ledger
11+
// counts its field changes, and the activity's `updated` rows step aside; a
12+
// member refused the ledger counts no field change, and the updates stay.
13+
14+
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
15+
import { bootStack, type VerifyStack } from '@objectstack/verify';
16+
import { AuditPlugin } from '@objectstack/plugin-audit';
17+
import { SecurityPlugin, securityDefaultPermissionSets } from '@objectstack/plugin-security';
18+
import { PermissionSetSchema, type PermissionSet } from '@objectstack/spec/security';
19+
import { RECORD_TIMELINE_UNDRAWN_ACTIVITY_TYPES, RecordPageResponseSchema } from '@objectstack/spec/api';
20+
import { commentsFixtureStack, cmtFixtureBaselineSet } from './fixtures/comments-fixture.js';
21+
import { armedWhen, assertArmed } from './armed.js';
22+
import { catalogPermissionSet } from './catalog-permission-set.js';
23+
24+
const SYS = { isSystem: true } as const;
25+
const AUTHOR_EMAIL = 'timeline-author@verify.test';
26+
const PEER_EMAIL = 'timeline-peer@verify.test';
27+
28+
/** Object-level reads of the activity stream and the ledger — the author holds both. */
29+
const streamReaderSet: PermissionSet = PermissionSetSchema.parse({
30+
name: 'tl_stream_reader',
31+
label: 'Timeline fixture — object-level sys_activity and sys_audit_log read',
32+
objects: { sys_activity: { allowRead: true }, sys_audit_log: { allowRead: true } },
33+
});
34+
/** The activity stream alone — the peer holds this, and no ledger read. */
35+
const activityReaderSet: PermissionSet = PermissionSetSchema.parse({
36+
name: 'tl_activity_reader',
37+
label: 'Timeline fixture — object-level sys_activity read',
38+
objects: { sys_activity: { allowRead: true } },
39+
});
40+
41+
const ALL_SOURCES = { discussion: true, activity: true, fieldChanges: true } as const;
42+
43+
describe('[#28737] the timeline part counts the 动态 stream the caller could read', () => {
44+
let stack: VerifyStack;
45+
let ql: any;
46+
let authorTok: string;
47+
let peerTok: string;
48+
let recordId: string;
49+
50+
const grant = async (email: string, set: PermissionSet) => {
51+
const userId = (await ql.findOne('sys_user', { where: { email }, context: SYS }))?.id;
52+
const served = await catalogPermissionSet(ql, set.name);
53+
expect(served, `fixture permission set ${set.name} served by the security catalog`).toBeTruthy();
54+
await ql.insert('sys_user_permission_set', { user_id: userId, permission_set: served.name }, { context: { ...SYS } });
55+
};
56+
57+
beforeAll(async () => {
58+
stack = await bootStack(commentsFixtureStack as never, {
59+
security: new SecurityPlugin({
60+
defaultPermissionSets: [...securityDefaultPermissionSets, cmtFixtureBaselineSet, streamReaderSet, activityReaderSet],
61+
fallbackPermissionSet: cmtFixtureBaselineSet.name,
62+
}),
63+
extraPlugins: [new AuditPlugin()],
64+
orgContext: true,
65+
});
66+
await stack.signIn();
67+
authorTok = await stack.signUp(AUTHOR_EMAIL);
68+
peerTok = await stack.signUp(PEER_EMAIL);
69+
ql = await stack.kernel.getServiceAsync('objectql');
70+
await grant(AUTHOR_EMAIL, streamReaderSet);
71+
await grant(PEER_EMAIL, activityReaderSet);
72+
73+
// The record's stream, from its real producers: a create and two renames
74+
// (ledger rows and their activity mirror), two comments and a reply.
75+
const created = await stack.apiAs(authorTok, 'POST', '/data/cmt_open', { name: 'shared board' });
76+
expect(created.status, await created.clone().text()).toBeLessThan(300);
77+
const body: any = await created.json();
78+
recordId = String(body.id ?? body.record?.id ?? body.data?.id);
79+
for (const name of ['shared board, renamed', 'shared board, renamed again']) {
80+
expect((await stack.apiAs(authorTok, 'PATCH', `/data/cmt_open/${recordId}`, { name })).status).toBeLessThan(300);
81+
}
82+
const thread = `cmt_open:${recordId}`;
83+
const first = await stack.apiAs(authorTok, 'POST', '/data/sys_comment', { thread_id: thread, body: 'first' });
84+
expect(first.status, await first.clone().text()).toBe(201);
85+
const firstId = String(((await first.json()) as any).id ?? '');
86+
expect((await stack.apiAs(authorTok, 'POST', '/data/sys_comment', { thread_id: thread, body: 'second' })).status).toBe(201);
87+
expect((await stack.apiAs(authorTok, 'POST', '/data/sys_comment', { thread_id: thread, body: 'a reply', parent_id: firstId })).status).toBe(201);
88+
89+
await assertArmed([
90+
armedWhen({
91+
control: 'the stores at rest hold the stream this file counts, from their real producers',
92+
disarmedBy: 'a writer that stopped writing would let every count below agree on zero',
93+
observe: async () => ({
94+
comments: (await ql.find('sys_comment', { where: { thread_id: thread }, context: SYS })).length,
95+
ledger: (await ql.find('sys_audit_log', { where: { object_name: 'cmt_open', record_id: recordId }, context: SYS })).length,
96+
updates: (await ql.find('sys_activity', { where: { object_name: 'cmt_open', record_id: recordId, type: 'updated' }, context: SYS })).length,
97+
}),
98+
armed: (o) => o.comments === 3 && o.ledger >= 3 && o.updates === 2,
99+
}),
100+
armedWhen({
101+
control: 'the author reads the ledger and the peer is refused it',
102+
disarmedBy: 'a peer who could read the ledger would make the refused-ledger case a statement about nothing',
103+
observe: async () => ({
104+
author: (await stack.apiAs(authorTok, 'GET', `/data/sys_audit_log?$top=1`)).status,
105+
peer: (await stack.apiAs(peerTok, 'GET', `/data/sys_audit_log?$top=1`)).status,
106+
}),
107+
armed: (o) => o.author === 200 && o.peer >= 400,
108+
}),
109+
]);
110+
}, 180_000);
111+
112+
afterAll(async () => {
113+
await stack?.stop?.();
114+
});
115+
116+
/** A list read's total as `token` — or `null` when the read is refused. */
117+
const totalAs = async (token: string, object: string, filter: Record<string, unknown>): Promise<number | null> => {
118+
const res = await stack.apiAs(token, 'GET', `/data/${object}?$filter=${encodeURIComponent(JSON.stringify(filter))}&$top=1&$count=true`);
119+
if (res.status >= 400) return null;
120+
const json: any = await res.json();
121+
return Number(json?.data?.total ?? json?.total);
122+
};
123+
/** The sum the spec's timeline table spells, from the separate list reads as `token`. */
124+
const expectedAs = async (token: string) => {
125+
const ofRecord = { object_name: 'cmt_open', record_id: recordId };
126+
const comments = await totalAs(token, 'sys_comment', { thread_id: `cmt_open:${recordId}` });
127+
const changes = (await totalAs(token, 'sys_audit_log', { ...ofRecord, action: { $in: ['create', 'update', 'delete'] } })) ?? 0;
128+
const drawn = await totalAs(token, 'sys_activity', { ...ofRecord, type: { $nin: [...RECORD_TIMELINE_UNDRAWN_ACTIVITY_TYPES] } });
129+
const updates = await totalAs(token, 'sys_activity', { ...ofRecord, type: 'updated' });
130+
expect(comments, 'the comment read answers').not.toBeNull();
131+
expect(drawn, 'the activity read answers').not.toBeNull();
132+
return { comments: comments!, changes, drawn: drawn!, updates: updates!, total: comments! + changes + drawn! - (changes > 0 ? updates! : 0) };
133+
};
134+
const timelineAs = async (token: string) => {
135+
const res = await stack.apiAs(token, 'POST', `/data/cmt_open/${recordId}/page`, { timeline: ALL_SOURCES });
136+
const json: any = await res.json();
137+
expect(res.status, JSON.stringify(json)).toBe(200);
138+
return RecordPageResponseSchema.parse(json.data).timeline;
139+
};
140+
141+
it('a member who reads the ledger counts its field changes, and the updates step aside', async () => {
142+
const expected = await expectedAs(authorTok);
143+
expect(expected.comments).toBe(3);
144+
expect(expected.changes).toBeGreaterThanOrEqual(3);
145+
expect(expected.updates).toBe(2);
146+
expect(await timelineAs(authorTok)).toEqual({ total: expected.total });
147+
}, 60_000);
148+
149+
it('a member refused the ledger counts no field change, and the updates stay', async () => {
150+
const expected = await expectedAs(peerTok);
151+
expect(expected.changes).toBe(0);
152+
expect(await timelineAs(peerTok)).toEqual({ total: expected.total });
153+
expect(expected.total).toBe(expected.comments + expected.drawn);
154+
}, 60_000);
155+
156+
it('counts only the sources named', async () => {
157+
const res = await stack.apiAs(authorTok, 'POST', `/data/cmt_open/${recordId}/page`, {
158+
timeline: { discussion: true, activity: false, fieldChanges: false },
159+
});
160+
expect(res.status).toBe(200);
161+
expect(RecordPageResponseSchema.parse(((await res.json()) as any).data).timeline).toEqual({ total: 3 });
162+
}, 60_000);
163+
});

‎packages/spec/authorable-surface/api.json‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,6 +1501,7 @@
15011501
"api/RecordPageRequest:explain",
15021502
"api/RecordPageRequest:fieldAccess",
15031503
"api/RecordPageRequest:related",
1504+
"api/RecordPageRequest:timeline",
15041505
"api/RecordPageResponse:activity",
15051506
"api/RecordPageResponse:approvals",
15061507
"api/RecordPageResponse:attachments",
@@ -1511,9 +1512,14 @@
15111512
"api/RecordPageResponse:object",
15121513
"api/RecordPageResponse:record",
15131514
"api/RecordPageResponse:related",
1515+
"api/RecordPageResponse:timeline",
15141516
"api/RecordPageStreamRefusal:error",
15151517
"api/RecordPageStreamRows:records",
15161518
"api/RecordPageStreamRows:total",
1519+
"api/RecordPageTimelineCount:total",
1520+
"api/RecordPageTimelineRequest:activity",
1521+
"api/RecordPageTimelineRequest:discussion",
1522+
"api/RecordPageTimelineRequest:fieldChanges",
15171523
"api/RefreshTokenRequest:refreshToken",
15181524
"api/RegisterDeviceRequest:deviceId",
15191525
"api/RegisterDeviceRequest:name",

‎packages/spec/json-schema.manifest/api.json‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,8 @@
370370
"api/RecordPageResponse",
371371
"api/RecordPageStreamRefusal",
372372
"api/RecordPageStreamRows",
373+
"api/RecordPageTimelineCount",
374+
"api/RecordPageTimelineRequest",
373375
"api/RefreshTokenRequest",
374376
"api/RegisterDeviceRequest",
375377
"api/RegisterDeviceResponse",

0 commit comments

Comments
 (0)