Skip to content

Commit ecdc398

Browse files
committed
test(maestro): move repeat and condition execution cases to a sibling module
Keeps engine.test.ts under the 1,000-line test file limit; makePort moves to the shared runtime-port fixtures.
1 parent e3f1ecf commit ecdc398

3 files changed

Lines changed: 226 additions & 219 deletions

File tree

‎packages/maestro/src/internal/__tests__/engine.test.ts‎

Lines changed: 1 addition & 219 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
import { describe, expect, test, vi } from 'vitest';
22
import { AppError } from '@agent-device/kernel/errors';
33
import { maestroTestFailure } from '../compatibility-errors.ts';
4-
import { MAESTRO_COMPATIBILITY_PRESETS } from '../compatibility-policy.ts';
54
import { parseMaestroProgram } from '../program-ir-parser.ts';
65
import type {
76
MaestroObservation,
87
MaestroRuntimePort,
98
MaestroRuntimeRequest,
10-
MaestroRuntimeResult,
119
} from '../engine-types.ts';
12-
import { executeMaestroProgram } from './runtime-port-fixtures.ts';
10+
import { executeMaestroProgram, makePort } from './runtime-port-fixtures.ts';
1311

1412
describe('executeMaestroProgram', () => {
1513
test('preserves authored percentage swipe intent without observing', async () => {
@@ -680,204 +678,6 @@ describe('executeMaestroProgram', () => {
680678
).toHaveLength(3);
681679
});
682680

683-
test('repeat while evaluates JavaScript expressions and retains output updates', async () => {
684-
const texts: string[] = [];
685-
const port = makePort({
686-
execute: vi.fn(async (request) => {
687-
if (request.command.kind === 'inputText') texts.push(request.command.text);
688-
request.invalidateObservation();
689-
return {};
690-
}),
691-
});
692-
const program = parseMaestroProgram(
693-
[
694-
'---',
695-
'- evalScript: ${output.counter = 0}',
696-
'- repeat:',
697-
' while:',
698-
' true: "${output.counter < 3}"',
699-
' commands:',
700-
' - inputText: loop',
701-
' - evalScript: ${output.counter++}',
702-
].join('\n'),
703-
);
704-
705-
await executeMaestroProgram(program, port);
706-
707-
expect(texts).toEqual(['loop', 'loop', 'loop']);
708-
});
709-
710-
test('repeat while observes selectors once per iteration and stops when false', async () => {
711-
let checks = 0;
712-
const observe = vi.fn(async ({ generation }: Parameters<MaestroRuntimePort['observe']>[0]) => ({
713-
generation,
714-
matched: ++checks < 3,
715-
}));
716-
const port = makePort({ observe });
717-
const program = parseMaestroProgram(
718-
[
719-
'---',
720-
'- repeat:',
721-
' times: 10',
722-
' while:',
723-
' platform: Android',
724-
' notVisible: ValueX',
725-
' commands:',
726-
' - inputText: loop',
727-
].join('\n'),
728-
);
729-
730-
await executeMaestroProgram(program, port, { platform: 'android' });
731-
732-
expect(port.execute).toHaveBeenCalledTimes(2);
733-
expect(observe).toHaveBeenCalledTimes(3);
734-
expect(observe).toHaveBeenCalledWith(
735-
expect.objectContaining({
736-
condition: { kind: 'notVisible', selector: { text: 'ValueX' } },
737-
timeoutMs: MAESTRO_COMPATIBILITY_PRESETS.command.optionalTargetLookupTimeoutMs,
738-
}),
739-
);
740-
});
741-
742-
test('repeat while stops at the times limit even while its condition remains true', async () => {
743-
const port = makePort();
744-
const program = parseMaestroProgram(
745-
[
746-
'---',
747-
'- repeat:',
748-
' times: 2',
749-
' while:',
750-
' true: true',
751-
' commands:',
752-
' - inputText: loop',
753-
].join('\n'),
754-
);
755-
756-
await executeMaestroProgram(program, port);
757-
758-
expect(port.execute).toHaveBeenCalledTimes(2);
759-
});
760-
761-
test('repeat while expressions are refused for remote untrusted flows', async () => {
762-
const program = parseMaestroProgram(
763-
[
764-
'---',
765-
'- repeat:',
766-
' while:',
767-
' true: "${output.counter < 3}"',
768-
' commands:',
769-
' - inputText: loop',
770-
].join('\n'),
771-
);
772-
773-
await expect(
774-
executeMaestroProgram(program, makePort(), { trustedScripts: false }),
775-
).rejects.toMatchObject({ code: 'UNAUTHORIZED' });
776-
});
777-
778-
test('repeat while true binds maestro.platform in JavaScript expressions', async () => {
779-
const port = makePort();
780-
const program = parseMaestroProgram(
781-
[
782-
'---',
783-
'- evalScript: ${output.n = 0}',
784-
'- repeat:',
785-
' while:',
786-
` true: "\${maestro.platform === 'ios' && output.n < 2}"`,
787-
' commands:',
788-
' - inputText: loop',
789-
' - evalScript: ${output.n++}',
790-
].join('\n'),
791-
);
792-
793-
await executeMaestroProgram(program, port, { platform: 'ios' });
794-
795-
expect(port.execute).toHaveBeenCalledTimes(2);
796-
});
797-
798-
test('repeat while true accepts plain literals and platform comparisons without scripts', async () => {
799-
const port = makePort();
800-
const program = parseMaestroProgram(
801-
[
802-
'---',
803-
'- repeat:',
804-
' times: 2',
805-
' while:',
806-
' true: yes',
807-
' commands:',
808-
' - inputText: literal',
809-
'- repeat:',
810-
' times: 2',
811-
' while:',
812-
` true: "\${maestro.platform == 'android'}"`,
813-
' commands:',
814-
' - inputText: platform',
815-
].join('\n'),
816-
);
817-
818-
await executeMaestroProgram(program, port, { platform: 'ios', trustedScripts: false });
819-
820-
expect(port.execute).toHaveBeenCalledTimes(2);
821-
});
822-
823-
test('runFlow when true shares the repeat while JavaScript evaluator', async () => {
824-
const port = makePort();
825-
const program = parseMaestroProgram(
826-
[
827-
'---',
828-
'- evalScript: ${output.n = 2}',
829-
'- runFlow:',
830-
' when:',
831-
' true: "${output.n > 1}"',
832-
' commands:',
833-
' - inputText: ran',
834-
].join('\n'),
835-
);
836-
837-
await executeMaestroProgram(program, port);
838-
839-
expect(port.execute).toHaveBeenCalledTimes(1);
840-
});
841-
842-
test('repeat while re-resolves condition selectors before every check', async () => {
843-
const observe = vi.fn(async ({ generation }: Parameters<MaestroRuntimePort['observe']>[0]) => ({
844-
generation,
845-
matched: true,
846-
}));
847-
const port = makePort({ observe });
848-
const program = parseMaestroProgram(
849-
[
850-
'---',
851-
'- evalScript: ${output.target = "first"}',
852-
'- repeat:',
853-
' times: 2',
854-
' while:',
855-
' visible: ${output.target}',
856-
' commands:',
857-
' - evalScript: ${output.target = "second"}',
858-
].join('\n'),
859-
);
860-
861-
await executeMaestroProgram(program, port);
862-
863-
expect(observe.mock.calls.map(([request]) => request.condition)).toEqual([
864-
{ kind: 'visible', selector: { text: 'first' } },
865-
{ kind: 'visible', selector: { text: 'second' } },
866-
]);
867-
});
868-
869-
test('an unbounded synchronous repeat still yields to cancellation timers', async () => {
870-
const controller = new AbortController();
871-
const program = parseMaestroProgram(
872-
['---', '- repeat:', ' while:', ' true: true', ' commands: []'].join('\n'),
873-
);
874-
setTimeout(() => controller.abort(), 10);
875-
876-
await expect(
877-
executeMaestroProgram(program, makePort(), { signal: controller.signal }),
878-
).rejects.toMatchObject({ details: { reason: 'request_canceled' } });
879-
});
880-
881681
test('evalScript replaces output namespace so shrunken arrays drop stale leaves', async () => {
882682
const texts: string[] = [];
883683
const port = makePort({
@@ -1056,21 +856,3 @@ describe('executeMaestroProgram', () => {
1056856
expect(execute.mock.calls[0]?.[0].signal).toBe(controller.signal);
1057857
});
1058858
});
1059-
1060-
function makePort(overrides: Partial<MaestroRuntimePort> = {}): MaestroRuntimePort {
1061-
return {
1062-
execute: vi.fn(async (request): Promise<MaestroRuntimeResult> => {
1063-
const { command } = request;
1064-
if (
1065-
command.kind !== 'takeScreenshot' &&
1066-
command.kind !== 'runScript' &&
1067-
command.kind !== 'waitForAnimationToEnd'
1068-
) {
1069-
request.invalidateObservation();
1070-
}
1071-
return command.kind === 'takeScreenshot' ? { artifactPaths: [command.path] } : {};
1072-
}),
1073-
observe: vi.fn(async ({ generation }) => ({ generation, matched: true })),
1074-
...overrides,
1075-
};
1076-
}

0 commit comments

Comments
 (0)