Skip to content

Commit 2baa859

Browse files
authored
feat(forms): inline edit, WebMCP try, Playwright export, Signal Forms schema and form lint (#479)
Refs #367
1 parent 5033639 commit 2baa859

33 files changed

Lines changed: 6011 additions & 2133 deletions

‎app/src/__tests__/forms-panels.test.ts‎

Lines changed: 302 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { TestBed, type ComponentFixture } from '@angular/core/testing';
22
import type { DevframeRpcClient } from 'devframe/client';
33
import { afterEach, describe, expect, it } from 'vitest';
44
import { scopeToPage } from '../page-id';
5+
import { FormsExportTest, testFileName } from '../pages/forms-export-test';
56
import { FormsFieldDetail } from '../pages/forms-field-detail';
67
import { FormsInspector } from '../pages/forms-inspector';
78
import { FormsLint, FormsSubmit } from '../pages/forms-report';
@@ -1055,3 +1056,304 @@ describe('FormsTimeline', () => {
10551056
expect(box.checked).toBe(true);
10561057
});
10571058
});
1059+
1060+
describe('FormsFields inline editing', () => {
1061+
function client(call: Call, meta: Record<string, unknown> = {}): DevframeRpcClient {
1062+
const rpc = {
1063+
call,
1064+
callEvent: () => Promise.resolve(),
1065+
sharedState: () => Promise.resolve({ value: () => ({ forms: [form] }), on: () => () => {} }),
1066+
};
1067+
return { connectionMeta: meta, scope: () => ({ rpc }) } as unknown as DevframeRpcClient;
1068+
}
1069+
1070+
it('edits the value, touched and dirty of a control in its row, then hands focus back', async () => {
1071+
const calls: Record<string, unknown>[] = [];
1072+
const fixture = TestBed.createComponent(FormsInspector);
1073+
fixture.componentRef.setInput(
1074+
'rpc',
1075+
client((name, arg) => {
1076+
if (name === 'request-form-action') calls.push(arg);
1077+
return Promise.resolve({ ok: true, message: 'Done.' });
1078+
}),
1079+
);
1080+
document.body.append(fixture.nativeElement);
1081+
await settle(fixture);
1082+
const host = fixture.nativeElement as HTMLElement;
1083+
const edit = button(fixture, 'Edit email');
1084+
expect(edit.getAttribute('aria-expanded')).toBe('false');
1085+
1086+
edit.click();
1087+
await settle(fixture);
1088+
expect(edit.getAttribute('aria-expanded')).toBe('true');
1089+
const input = host.querySelector<HTMLInputElement>('#forms-inline-value')!;
1090+
expect(input.value).toBe('x');
1091+
expect(document.activeElement).toBe(input);
1092+
1093+
input.value = '[email protected]';
1094+
input.dispatchEvent(new Event('input'));
1095+
input.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter' }));
1096+
await settle(fixture);
1097+
expect(calls.at(-1)).toEqual({
1098+
action: 'set-value',
1099+
formId: form.id,
1100+
path: 'email',
1101+
1102+
coerce: true,
1103+
mode: 'user',
1104+
});
1105+
expect(host.querySelector('.edit-row [role="status"]')?.textContent).toContain('Done.');
1106+
1107+
const [touched, dirty] = Array.from(
1108+
host.querySelectorAll<HTMLInputElement>('.edit-row input[type="checkbox"]'),
1109+
);
1110+
touched.click();
1111+
await settle(fixture);
1112+
expect(calls.at(-1)).toEqual({ action: 'mark-touched', formId: form.id, path: 'email' });
1113+
dirty.click();
1114+
await settle(fixture);
1115+
expect(calls.at(-1)).toEqual({ action: 'mark-dirty', formId: form.id, path: 'email' });
1116+
1117+
button(fixture, 'Done').click();
1118+
await settle(fixture);
1119+
expect(host.querySelector('#forms-inline-editor')).toBeNull();
1120+
expect(document.activeElement).toBe(edit);
1121+
});
1122+
1123+
it('puts a checkbox back when the page refuses the write', async () => {
1124+
const fixture = TestBed.createComponent(FormsInspector);
1125+
fixture.componentRef.setInput(
1126+
'rpc',
1127+
client(() => Promise.resolve({ ok: false, error: 'No field at email.' })),
1128+
);
1129+
document.body.append(fixture.nativeElement);
1130+
await settle(fixture);
1131+
button(fixture, 'Edit email').click();
1132+
await settle(fixture);
1133+
const host = fixture.nativeElement as HTMLElement;
1134+
const touched = host.querySelector<HTMLInputElement>('.edit-row input[type="checkbox"]')!;
1135+
touched.click();
1136+
await settle(fixture);
1137+
expect(touched.checked).toBe(false);
1138+
expect(text(fixture)).toContain('No field at email.');
1139+
});
1140+
1141+
it('turns Edit off when form writes are off', async () => {
1142+
const fixture = TestBed.createComponent(FormsInspector);
1143+
fixture.componentRef.setInput(
1144+
'rpc',
1145+
client(fail, { configs: { pangular: { actions: { forms: false } } } }),
1146+
);
1147+
document.body.append(fixture.nativeElement);
1148+
await settle(fixture);
1149+
const edit = button(fixture, 'Edit email');
1150+
expect(edit.disabled).toBe(true);
1151+
expect(edit.getAttribute('aria-describedby')).toBe('forms-writes-off');
1152+
});
1153+
});
1154+
1155+
describe('FormsWebMcp Try this tool', () => {
1156+
const tool = {
1157+
name: 'sign_up',
1158+
description: 'Create an account',
1159+
status: 'registered' as const,
1160+
seen: 'register' as const,
1161+
inputs: ['name: string', 'age: number'],
1162+
schema: {
1163+
type: 'object',
1164+
properties: { name: { type: 'string' }, age: { type: 'number' } },
1165+
required: ['name'],
1166+
},
1167+
calls: [{ at: 0, outcome: 'submitted', tried: true }],
1168+
};
1169+
1170+
it('shows the input schema, starts from a sample input and asks before it runs the tool', async () => {
1171+
const calls: Record<string, unknown>[] = [];
1172+
const fixture = TestBed.createComponent(FormsWebMcp);
1173+
fixture.componentRef.setInput('tool', tool);
1174+
fixture.componentRef.setInput('formId', 'signup@p1');
1175+
fixture.componentRef.setInput(
1176+
'rpc',
1177+
fakeClient((_name, arg) => {
1178+
calls.push(arg);
1179+
return Promise.resolve({
1180+
ok: true,
1181+
message: 'The tool answered: Form submitted successfully',
1182+
});
1183+
}),
1184+
);
1185+
await settle(fixture);
1186+
const host = fixture.nativeElement as HTMLElement;
1187+
expect(host.querySelector('.schema pre')?.textContent).toContain('"required"');
1188+
expect(text(fixture)).toContain('tried from the panel');
1189+
const input = host.querySelector<HTMLTextAreaElement>('textarea')!;
1190+
expect(JSON.parse(input.value)).toEqual({ name: '', age: 0 });
1191+
expect(host.querySelector(`label[for="${input.id}"]`)?.textContent).toContain('Tool input');
1192+
1193+
input.value = '{"name": "Ada", "age": 3}';
1194+
input.dispatchEvent(new Event('input'));
1195+
button(fixture, 'Try this tool').click();
1196+
await settle(fixture);
1197+
expect(calls).toEqual([]);
1198+
button(fixture, 'Confirm try').click();
1199+
await settle(fixture);
1200+
expect(calls).toEqual([
1201+
{ action: 'try-webmcp', formId: 'signup@p1', value: { name: 'Ada', age: 3 }, confirm: true },
1202+
]);
1203+
expect(text(fixture)).toContain('Form submitted successfully');
1204+
});
1205+
1206+
it('runs a confirmed try once and keeps the button off until it answers', async () => {
1207+
const calls: Record<string, unknown>[] = [];
1208+
let answer!: (value: unknown) => void;
1209+
const fixture = TestBed.createComponent(FormsWebMcp);
1210+
fixture.componentRef.setInput('tool', tool);
1211+
fixture.componentRef.setInput('formId', 'signup@p1');
1212+
fixture.componentRef.setInput(
1213+
'rpc',
1214+
fakeClient((_name, arg) => {
1215+
calls.push(arg);
1216+
return new Promise((resolve) => (answer = resolve));
1217+
}),
1218+
);
1219+
await settle(fixture);
1220+
button(fixture, 'Try this tool').click();
1221+
await settle(fixture);
1222+
button(fixture, 'Confirm try').click();
1223+
await settle(fixture);
1224+
expect(calls).toHaveLength(1);
1225+
expect(button(fixture, 'Try this tool').disabled).toBe(true);
1226+
// A second try while the first runs, armed again, sends nothing.
1227+
fixture.componentInstance.armed.set(true);
1228+
await fixture.componentInstance.tryTool();
1229+
expect(calls).toHaveLength(1);
1230+
answer({ ok: true, message: 'The tool answered: Form submitted successfully' });
1231+
await settle(fixture);
1232+
expect(text(fixture)).toContain('Form submitted successfully');
1233+
expect(button(fixture, 'Confirm try').disabled).toBe(false);
1234+
});
1235+
1236+
it('refuses input that is not a JSON object', async () => {
1237+
const fixture = TestBed.createComponent(FormsWebMcp);
1238+
fixture.componentRef.setInput('tool', tool);
1239+
fixture.componentRef.setInput('formId', 'signup@p1');
1240+
fixture.componentRef.setInput('rpc', fakeClient(fail));
1241+
await settle(fixture);
1242+
const input = (fixture.nativeElement as HTMLElement).querySelector('textarea')!;
1243+
input.value = '[1';
1244+
input.dispatchEvent(new Event('input'));
1245+
button(fixture, 'Try this tool').click();
1246+
await settle(fixture);
1247+
expect(text(fixture)).toContain('not valid JSON');
1248+
});
1249+
1250+
it('offers no Try for a tool without a form, and turns it off when form writes are off', async () => {
1251+
const fixture = TestBed.createComponent(FormsWebMcp);
1252+
fixture.componentRef.setInput('tool', tool);
1253+
await settle(fixture);
1254+
expect(text(fixture)).not.toContain('Try this tool');
1255+
fixture.componentRef.setInput('formId', 'signup@p1');
1256+
fixture.componentRef.setInput('rpc', {
1257+
connectionMeta: { configs: { pangular: { actions: { forms: false } } } },
1258+
scope: () => ({ rpc: {} }),
1259+
} as unknown as DevframeRpcClient);
1260+
await settle(fixture);
1261+
expect(button(fixture, 'Try this tool').disabled).toBe(true);
1262+
});
1263+
});
1264+
1265+
describe('FormsExportTest', () => {
1266+
it('copies the Playwright test code and says when there is no history', async () => {
1267+
const answers = [
1268+
'_Labels, paths and values come from the page._\n\n```ts\ntest("x", async () => {});\n```',
1269+
'No changes from a person, an agent or the panel are recorded for `Signup.form`.',
1270+
];
1271+
const asked: Record<string, unknown>[] = [];
1272+
let copied = '';
1273+
Object.defineProperty(navigator, 'clipboard', {
1274+
configurable: true,
1275+
value: { writeText: (value: string) => ((copied = value), Promise.resolve()) },
1276+
});
1277+
const fixture = TestBed.createComponent(FormsExportTest);
1278+
fixture.componentRef.setInput('formId', 'Signup.form@p1');
1279+
fixture.componentRef.setInput(
1280+
'rpc',
1281+
fakeClient((_name, arg) => {
1282+
asked.push(arg);
1283+
return Promise.resolve(answers.shift());
1284+
}),
1285+
);
1286+
await settle(fixture);
1287+
button(fixture, 'Copy Playwright test').click();
1288+
await settle(fixture);
1289+
expect(asked[0]).toEqual({ kind: 'playwright', form: 'Signup.form@p1' });
1290+
expect(copied).toBe('test("x", async () => {});\n');
1291+
expect(text(fixture)).toContain('Copied.');
1292+
button(fixture, 'Copy Playwright test').click();
1293+
await settle(fixture);
1294+
expect(text(fixture)).toContain('No changes from a person');
1295+
});
1296+
1297+
it('names the download after the form', () => {
1298+
expect(testFileName('SignupForm.form@p1')).toBe('signup-form-form.spec.ts');
1299+
expect(testFileName('@p1')).toBe('form.spec.ts');
1300+
});
1301+
});
1302+
1303+
describe('FormsInspector schema view', () => {
1304+
const signalForm: CollectedForm = {
1305+
id: 'profile@p1',
1306+
kind: 'signal',
1307+
owner: 'Profile',
1308+
label: 'Profile.form',
1309+
root: {
1310+
...field(''),
1311+
type: 'group',
1312+
rules: { tree: 1 },
1313+
children: [
1314+
field('name', { required: true, rules: { sync: 2 }, constraints: { minLength: 2 } }),
1315+
field('promo', { hidden: true, hiddenBy: 'self', rules: { hidden: 1 } }),
1316+
field('plain'),
1317+
],
1318+
},
1319+
};
1320+
1321+
it('offers a Schema tab for a Signal Form with the rules of each field', async () => {
1322+
const fixture = TestBed.createComponent(FormsInspector);
1323+
fixture.componentRef.setInput('rpc', fakeClient(fail, [signalForm, form]));
1324+
document.body.append(fixture.nativeElement);
1325+
await settle(fixture);
1326+
const host = fixture.nativeElement as HTMLElement;
1327+
const tab = host.querySelector<HTMLButtonElement>('#forms-tab-schema')!;
1328+
expect(tab.textContent?.trim()).toBe('Schema');
1329+
tab.click();
1330+
await settle(fixture);
1331+
const rows = Array.from(host.querySelectorAll('.schema tbody tr')).map((row) =>
1332+
Array.from(row.querySelectorAll('th, td'))
1333+
.map((cell) =>
1334+
Array.from(cell.children)
1335+
.map((part) => part.textContent?.trim())
1336+
.join(', '),
1337+
)
1338+
.join(' | '),
1339+
);
1340+
expect(rows).toEqual([
1341+
'(form), group | 1 cross-field rule | | ',
1342+
'name, control | required, 2 validators | minLength 2 | ',
1343+
'promo, control | 1 hidden rule | | hidden',
1344+
]);
1345+
});
1346+
1347+
it('hides the Schema tab for other forms and falls back to Fields', async () => {
1348+
const fixture = TestBed.createComponent(FormsInspector);
1349+
fixture.componentRef.setInput('rpc', fakeClient(fail, [signalForm, form]));
1350+
document.body.append(fixture.nativeElement);
1351+
await settle(fixture);
1352+
fixture.componentInstance.tab_.set('schema');
1353+
fixture.componentInstance.selectForm(form.id);
1354+
await settle(fixture);
1355+
const host = fixture.nativeElement as HTMLElement;
1356+
expect(host.querySelector('#forms-tab-schema')).toBeNull();
1357+
expect(fixture.componentInstance.tab_()).toBe('fields');
1358+
});
1359+
});

0 commit comments

Comments
 (0)