Skip to content

Commit 9a91f47

Browse files
committed
refactor: clarify response data transform naming
1 parent 37b9ead commit 9a91f47

12 files changed

Lines changed: 224 additions & 223 deletions
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import { describe, expect, test } from 'vitest';
2+
import { resolveCommandResponseDataTransform } from '../command-descriptor/registry.ts';
3+
import { transformInteractionResponseData } from '../interaction-response-data-transform.ts';
4+
5+
describe('interaction response data transform', () => {
6+
test('reads command-owned response data transform from descriptors', () => {
7+
expect(resolveCommandResponseDataTransform('press')?.fields).toEqual({
8+
count: { defaultValue: 1, omitDefault: true },
9+
intervalMs: { defaultValue: 0, omitDefault: true },
10+
holdMs: { defaultValue: 0, omitDefault: true },
11+
jitterPx: { defaultValue: 0, omitDefault: true },
12+
doubleTap: { defaultValue: false, omitDefault: true },
13+
});
14+
expect(resolveCommandResponseDataTransform('fill')?.fields.delayMs).toEqual({
15+
defaultValue: 0,
16+
});
17+
expect(resolveCommandResponseDataTransform('longpress')).toBeUndefined();
18+
});
19+
20+
test('omits default press repeat values', () => {
21+
expect(
22+
transformInteractionResponseData({
23+
command: 'press',
24+
input: {
25+
count: 1,
26+
intervalMs: 0,
27+
holdMs: 0,
28+
jitterPx: 0,
29+
doubleTap: false,
30+
},
31+
data: undefined,
32+
}),
33+
).toEqual({});
34+
});
35+
36+
test('keeps non-default press repeat values', () => {
37+
expect(
38+
transformInteractionResponseData({
39+
command: 'press',
40+
input: {
41+
count: 2,
42+
intervalMs: 25,
43+
holdMs: 10,
44+
jitterPx: 1,
45+
doubleTap: true,
46+
},
47+
data: undefined,
48+
}),
49+
).toEqual({
50+
count: 2,
51+
intervalMs: 25,
52+
holdMs: 10,
53+
jitterPx: 1,
54+
doubleTap: true,
55+
});
56+
});
57+
58+
test('keeps the normalized fill delay default', () => {
59+
expect(
60+
transformInteractionResponseData({ command: 'fill', input: {}, data: undefined }),
61+
).toEqual({
62+
delayMs: 0,
63+
});
64+
});
65+
66+
test('preserves backend fields while applying command defaults', () => {
67+
expect(
68+
transformInteractionResponseData({
69+
command: 'press',
70+
input: {},
71+
data: {
72+
count: 1,
73+
videoPath: '/tmp/demo.mp4',
74+
},
75+
}),
76+
).toEqual({ videoPath: '/tmp/demo.mp4' });
77+
});
78+
79+
test('removes default touch repeat fields from fill backend data', () => {
80+
expect(
81+
transformInteractionResponseData({
82+
command: 'fill',
83+
input: {},
84+
data: {
85+
count: 1,
86+
delayMs: 0,
87+
doubleTap: false,
88+
holdMs: 0,
89+
intervalMs: 0,
90+
jitterPx: 0,
91+
text: 'Hello',
92+
},
93+
}),
94+
).toEqual({ delayMs: 0, text: 'Hello' });
95+
});
96+
});

‎src/core/__tests__/interaction-wire-projection.test.ts‎

Lines changed: 0 additions & 85 deletions
This file was deleted.

‎src/core/command-descriptor/registry.ts‎

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,18 @@ import {
77
import type { CommandCapability } from '../capabilities.ts';
88
import type { DaemonRequest } from '../../daemon/types.ts';
99
import { resolveWaitBudgetMs } from '../wait-positionals.ts';
10-
import {
11-
FILL_DELAY_WIRE_ECHO,
12-
INTERACTION_REPEAT_WIRE_ECHO_FIELDS,
13-
type CommandWireProjection,
14-
} from '../interaction-wire-echo.ts';
1510
import {
1611
DEFAULT_TIMEOUT_POLICY,
1712
INSTALL_REQUEST_TIMEOUT_MS,
1813
PREPARE_REQUEST_TIMEOUT_MS,
1914
} from './timeout-policy.ts';
2015
import { resolvePostActionObservationSupport } from './post-action-observation.ts';
2116
import type { PostActionObservationSupport } from './post-action-observation.ts';
22-
import type { CommandDescriptor, CommandTimeoutPolicy } from './types.ts';
17+
import type {
18+
CommandDescriptor,
19+
CommandResponseDataTransform,
20+
CommandTimeoutPolicy,
21+
} from './types.ts';
2322

2423
type RawCommandDescriptor = Omit<CommandDescriptor, 'mcpExposed'> & {
2524
mcpExposed?: boolean;
@@ -130,18 +129,22 @@ const SETTLE_FLAG_PRESERVE_DAEMON_TIMEOUT_POLICY: CommandTimeoutPolicy = {
130129
onTimeout: 'preserve-daemon',
131130
};
132131

133-
const TOUCH_INTERACTION_WIRE_PROJECTION = {
134-
wireEcho: {
135-
...INTERACTION_REPEAT_WIRE_ECHO_FIELDS,
132+
const TOUCH_INTERACTION_RESPONSE_DATA_TRANSFORM = {
133+
fields: {
134+
count: { defaultValue: 1, omitDefault: true },
135+
intervalMs: { defaultValue: 0, omitDefault: true },
136+
holdMs: { defaultValue: 0, omitDefault: true },
137+
jitterPx: { defaultValue: 0, omitDefault: true },
138+
doubleTap: { defaultValue: false, omitDefault: true },
136139
},
137-
} as const satisfies CommandWireProjection;
140+
} as const satisfies CommandResponseDataTransform;
138141

139-
const FILL_INTERACTION_WIRE_PROJECTION = {
140-
wireEcho: {
141-
...INTERACTION_REPEAT_WIRE_ECHO_FIELDS,
142-
delayMs: FILL_DELAY_WIRE_ECHO,
142+
const FILL_INTERACTION_RESPONSE_DATA_TRANSFORM = {
143+
fields: {
144+
...TOUCH_INTERACTION_RESPONSE_DATA_TRANSFORM.fields,
145+
delayMs: { defaultValue: 0 },
143146
},
144-
} as const satisfies CommandWireProjection;
147+
} as const satisfies CommandResponseDataTransform;
145148

146149
function interactionTimeoutPolicy(command: string): CommandTimeoutPolicy {
147150
return resolvePostActionObservationSupport(command) !== undefined
@@ -534,7 +537,7 @@ const RAW_COMMAND_DESCRIPTORS = [
534537
capability: { apple: APPLE_SIM_AND_DEVICE, android: ANDROID_ALL, linux: LINUX_DEVICE },
535538
timeoutPolicy: interactionTimeoutPolicy(PUBLIC_COMMANDS.click),
536539
postActionObservation: postActionObservation(PUBLIC_COMMANDS.click),
537-
wireProjection: TOUCH_INTERACTION_WIRE_PROJECTION,
540+
responseDataTransform: TOUCH_INTERACTION_RESPONSE_DATA_TRANSFORM,
538541
batchable: true,
539542
},
540543
{
@@ -543,7 +546,7 @@ const RAW_COMMAND_DESCRIPTORS = [
543546
capability: { apple: APPLE_SIM_AND_DEVICE, android: ANDROID_ALL, linux: LINUX_DEVICE },
544547
timeoutPolicy: interactionTimeoutPolicy(PUBLIC_COMMANDS.fill),
545548
postActionObservation: postActionObservation(PUBLIC_COMMANDS.fill),
546-
wireProjection: FILL_INTERACTION_WIRE_PROJECTION,
549+
responseDataTransform: FILL_INTERACTION_RESPONSE_DATA_TRANSFORM,
547550
batchable: true,
548551
},
549552
{
@@ -560,7 +563,7 @@ const RAW_COMMAND_DESCRIPTORS = [
560563
capability: { apple: APPLE_SIM_AND_DEVICE, android: ANDROID_ALL, linux: LINUX_DEVICE },
561564
timeoutPolicy: interactionTimeoutPolicy(PUBLIC_COMMANDS.press),
562565
postActionObservation: postActionObservation(PUBLIC_COMMANDS.press),
563-
wireProjection: TOUCH_INTERACTION_WIRE_PROJECTION,
566+
responseDataTransform: TOUCH_INTERACTION_RESPONSE_DATA_TRANSFORM,
564567
batchable: true,
565568
},
566569
{
@@ -796,11 +799,14 @@ const TIMEOUT_POLICY_BY_COMMAND: ReadonlyMap<string, CommandTimeoutPolicy> = new
796799
commandDescriptors.map((descriptor) => [descriptor.name, descriptor.timeoutPolicy]),
797800
);
798801

799-
const WIRE_PROJECTION_BY_COMMAND: ReadonlyMap<string, CommandWireProjection> = new Map(
800-
Array.from(COMMAND_DESCRIPTOR_BY_NAME.values()).flatMap((descriptor) =>
801-
descriptor.wireProjection ? [[descriptor.name, descriptor.wireProjection] as const] : [],
802-
),
803-
);
802+
const RESPONSE_DATA_TRANSFORM_BY_COMMAND: ReadonlyMap<string, CommandResponseDataTransform> =
803+
new Map(
804+
Array.from(COMMAND_DESCRIPTOR_BY_NAME.values()).flatMap((descriptor) =>
805+
descriptor.responseDataTransform
806+
? [[descriptor.name, descriptor.responseDataTransform] as const]
807+
: [],
808+
),
809+
);
804810

805811
export function resolveCommandPostActionObservationSupport(
806812
command: string | undefined,
@@ -828,9 +834,9 @@ export function resolveCommandTimeoutPolicy(command: string | undefined): Comman
828834
return TIMEOUT_POLICY_BY_COMMAND.get(command) ?? DEFAULT_TIMEOUT_POLICY;
829835
}
830836

831-
export function resolveCommandWireProjection(
837+
export function resolveCommandResponseDataTransform(
832838
command: string | undefined,
833-
): CommandWireProjection | undefined {
839+
): CommandResponseDataTransform | undefined {
834840
if (command === undefined) return undefined;
835-
return WIRE_PROJECTION_BY_COMMAND.get(command);
841+
return RESPONSE_DATA_TRANSFORM_BY_COMMAND.get(command);
836842
}

‎src/core/command-descriptor/types.ts‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
import type { CommandCapability } from '../capabilities.ts';
2-
import type { CommandWireProjection } from '../interaction-wire-echo.ts';
32
import type { DaemonCommandDescriptor } from '../../daemon/daemon-command-registry.ts';
43
import type { PostActionObservationSupport } from './post-action-observation.ts';
54

5+
export type ResponseDataFieldTransform = {
6+
defaultValue?: unknown;
7+
omitDefault?: boolean;
8+
};
9+
10+
export type CommandResponseDataTransform = {
11+
fields: Record<string, ResponseDataFieldTransform>;
12+
};
13+
614
/**
715
* The daemon route + request-policy traits for a command, minus the `command`
816
* key (which is carried at the descriptor top level as `name`). This reuses the
@@ -82,9 +90,10 @@ export type CommandTimeoutPolicy = {
8290
* commands that support `--settle`/`--verify`; consumed by
8391
* command surfaces and timeout policy instead of repeated
8492
* command-name lists.
85-
* - `wireProjection` — optional response projection metadata for command-owned
86-
* echoes in daemon responses. This keeps response shaping on
87-
* the same descriptor surface as other command traits.
93+
* - `responseDataTransform` — optional public response data shaping rules for
94+
* command-owned fields in daemon responses. This keeps
95+
* response shaping on the same descriptor surface as other
96+
* command traits.
8897
*
8998
* The registry started dormant (proven byte-equal to the hand tables by the
9099
* parity tests) and is now the live source: the daemon registry, capability
@@ -99,5 +108,5 @@ export type CommandDescriptor = {
99108
mcpExposed: boolean;
100109
timeoutPolicy: CommandTimeoutPolicy;
101110
postActionObservation?: PostActionObservationSupport;
102-
wireProjection?: CommandWireProjection;
111+
responseDataTransform?: CommandResponseDataTransform;
103112
};
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { resolveCommandResponseDataTransform } from './command-descriptor/registry.ts';
2+
import type { ResponseDataFieldTransform } from './command-descriptor/types.ts';
3+
4+
export type InteractionResponseDataTransformCommand = 'click' | 'press' | 'fill';
5+
6+
export function transformInteractionResponseData(params: {
7+
command: InteractionResponseDataTransformCommand;
8+
input: Record<string, unknown> | undefined;
9+
data: Record<string, unknown> | undefined;
10+
}): Record<string, unknown> {
11+
return applyResponseDataFieldTransforms({
12+
fields: readCommandResponseDataTransformFields(params.command),
13+
input: params.input ?? {},
14+
data: params.data,
15+
});
16+
}
17+
18+
function readCommandResponseDataTransformFields(
19+
command: InteractionResponseDataTransformCommand,
20+
): Record<string, ResponseDataFieldTransform> {
21+
const transform = resolveCommandResponseDataTransform(command);
22+
if (!transform) {
23+
throw new Error(`Missing response data transform descriptor for ${command}`);
24+
}
25+
return transform.fields;
26+
}
27+
28+
function applyResponseDataFieldTransforms(params: {
29+
fields: Record<string, ResponseDataFieldTransform>;
30+
input: Record<string, unknown>;
31+
data: Record<string, unknown> | undefined;
32+
}): Record<string, unknown> {
33+
const transformed = { ...(params.data ?? {}) };
34+
for (const [key, field] of Object.entries(params.fields)) {
35+
const value = params.input[key] === undefined ? field.defaultValue : params.input[key];
36+
if (value === undefined || (field.omitDefault === true && value === field.defaultValue)) {
37+
delete transformed[key];
38+
continue;
39+
}
40+
transformed[key] = value;
41+
}
42+
return Object.fromEntries(Object.entries(transformed).filter(([, value]) => value !== undefined));
43+
}

0 commit comments

Comments
 (0)