Repository navigation
Expand file tree
/
Copy pathDraftChangesPanel.agentSource-27176.test.tsx
More file actions
119 lines (105 loc) · 5.03 KB
/
Copy pathDraftChangesPanel.agentSource-27176.test.tsx
File metadata and controls
119 lines (105 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
/**
* objectstack-ai/objectstack#27176 — the pending-changes sheet names the AI
* tools that drafted part of an item: the AI copilot, and an agent building
* through the MCP build door by the OAuth client the person consented to.
*
* The drill-in already sends the strict draft read (`?state=draft`), which
* serves the draft's per-key provenance (`draftChanges`) beside `item`; the
* line reads it from there.
*
* Pinned:
* - an `mcp` entry with a named client reads "drafted by the MCP client
* “Claude”"; the copilot reads its own line; each source once;
* - an `mcp` client with no registered name is unnamed, never its id;
* - CONTROL: `studio` / `api` entries, and a read with no `draftChanges`,
* add no line.
*/
import '@testing-library/jest-dom/vitest';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { cleanup, render, screen, waitFor } from '@testing-library/react';
vi.mock('@object-ui/i18n', async (importOriginal) => ({
...(await importOriginal<Record<string, unknown>>()),
useObjectTranslation: () => ({
t: (_k: string, o?: Record<string, unknown> & { defaultValue?: string }) =>
(o?.defaultValue ?? _k).replace(/\{\{(\w+)\}\}/g, (_m, name: string) => String(o?.[name] ?? '')),
language: 'en',
}),
}));
// The source loads `@objectstack/lint` lazily; paid here, at collection, not
// inside a case's clock (check:test-source-alias).
import '@objectstack/lint';
import { DraftChangesPanel, draftAgentSources } from '../DraftChangesPanel';
const PKG = 'com.x';
const AT = '2026-10-28T10:00:00.000Z';
const ok = (body: unknown) => ({ ok: true, status: 200, json: async () => body });
let draftChanges: unknown = undefined;
const fetchMock = vi.fn(async (input: RequestInfo | URL) => {
const url = String(input);
if (url.includes('/_drafts')) return ok({ drafts: [{ type: 'object', name: 'st_ticket', packageId: PKG, label: 'Ticket' }] });
if (/\/meta\/object(\?|$)/.test(url)) return ok([{ name: 'st_ticket' }]);
if (url.includes('state=draft')) {
return ok({
type: 'object',
name: 'st_ticket',
item: { name: 'st_ticket', label: 'Ticket', fields: { a: { type: 'text' } } },
...(draftChanges !== undefined ? { draftChanges } : {}),
});
}
return ok({ type: 'object', name: 'st_ticket', item: { name: 'st_ticket', label: 'Ticket', fields: {} } });
});
afterEach(() => {
cleanup();
vi.unstubAllGlobals();
fetchMock.mockClear();
draftChanges = undefined;
});
async function renderConfirm() {
vi.stubGlobal('fetch', fetchMock as unknown as typeof fetch);
render(<DraftChangesPanel open onOpenChange={() => {}} packageId={PKG} onPublish={() => {}} posture="confirm" />);
await waitFor(() => expect(screen.getByTestId('draft-entry-detail')).toBeInTheDocument());
}
const agentLines = () => screen.queryAllByTestId('draft-entry-agent').map((el) => el.textContent);
describe('objectstack#27176 — the drill-in names the AI tools that drafted the item', () => {
it('an MCP client by its registered name, and the copilot, each once', async () => {
draftChanges = [
{ path: ['fields', 'a'], by: 'mcp', actor: 'u_1', at: AT, client: { id: 'cli_1', name: 'Claude' } },
{ path: ['label'], by: 'copilot', actor: 'u_1', at: AT },
{ path: ['fields', 'b'], by: 'mcp', actor: 'u_1', at: AT, client: { id: 'cli_1', name: 'Claude' } },
];
await renderConfirm();
expect(agentLines()).toEqual([
'Includes changes drafted by the MCP client “Claude”',
'Includes changes drafted by the AI assistant',
]);
});
it('a client with no registered name is unnamed, never its id', async () => {
draftChanges = [{ path: ['fields', 'a'], by: 'mcp', actor: 'u_1', at: AT, client: { id: 'cli_opaque' } }];
await renderConfirm();
expect(agentLines()).toEqual(['Includes changes drafted by an MCP client']);
expect(screen.queryByText(/cli_opaque/)).toBeNull();
});
it('CONTROL: studio and api writes, or no recorded provenance, add no line', async () => {
draftChanges = [{ path: ['fields', 'a'], by: 'studio', actor: 'u_1', at: AT }];
await renderConfirm();
expect(agentLines()).toEqual([]);
cleanup();
draftChanges = undefined;
await renderConfirm();
expect(agentLines()).toEqual([]);
});
});
describe('draftAgentSources', () => {
it('folds an unnamed client and an mcp write with none into one unnamed source; two named clients stay two', () => {
expect(
draftAgentSources([
{ path: ['a'], by: 'mcp', actor: null, at: AT, client: { id: 'cli_1' } },
{ path: ['b'], by: 'mcp', actor: null, at: AT },
{ path: ['c'], by: 'mcp', actor: null, at: AT, client: { id: 'cli_2', name: 'Cursor' } },
{ path: ['d'], by: 'mcp', actor: null, at: AT, client: { id: 'cli_3', name: 'Claude' } },
{ path: ['e'], by: 'api', actor: null, at: AT },
]),
).toEqual([{ by: 'mcp' }, { by: 'mcp', client: 'Cursor' }, { by: 'mcp', client: 'Claude' }]);
expect(draftAgentSources(null)).toEqual([]);
});
});