|
| 1 | +# @midscene/playground Tests |
| 2 | + |
| 3 | +This directory contains the test suite for the `@midscene/playground` package. |
| 4 | + |
| 5 | +## Test Structure |
| 6 | + |
| 7 | +``` |
| 8 | +tests/ |
| 9 | +├── unit/ # Unit tests for individual components |
| 10 | +│ ├── common.test.ts # Tests for common utilities |
| 11 | +│ ├── playground-sdk.test.ts # Tests for PlaygroundSDK |
| 12 | +│ ├── base-adapter.test.ts # Tests for BasePlaygroundAdapter |
| 13 | +│ ├── local-execution-adapter.test.ts # Tests for LocalExecutionAdapter |
| 14 | +│ ├── remote-execution-adapter.test.ts # Tests for RemoteExecutionAdapter |
| 15 | +│ └── types.test.ts # Type definition tests |
| 16 | +├── integration/ # Integration tests |
| 17 | +│ └── playground-integration.test.ts # End-to-end workflow tests |
| 18 | +├── setup.ts # Test setup and global mocks |
| 19 | +└── README.md # This file |
| 20 | +``` |
| 21 | + |
| 22 | +## Running Tests |
| 23 | + |
| 24 | +### All Tests |
| 25 | +```bash |
| 26 | +pnpm test |
| 27 | +``` |
| 28 | + |
| 29 | +### Watch Mode |
| 30 | +```bash |
| 31 | +pnpm test:watch |
| 32 | +``` |
| 33 | + |
| 34 | +### Coverage Report |
| 35 | +```bash |
| 36 | +pnpm test -- --coverage |
| 37 | +``` |
| 38 | + |
| 39 | +### Specific Test Files |
| 40 | +```bash |
| 41 | +# Run only unit tests |
| 42 | +pnpm test tests/unit |
| 43 | + |
| 44 | +# Run only integration tests |
| 45 | +pnpm test tests/integration |
| 46 | + |
| 47 | +# Run specific test file |
| 48 | +pnpm test tests/unit/common.test.ts |
| 49 | +``` |
| 50 | + |
| 51 | +## Test Categories |
| 52 | + |
| 53 | +### Unit Tests |
| 54 | + |
| 55 | +**common.test.ts** |
| 56 | +- Tests core utility functions like `formatErrorMessage`, `validateStructuredParams`, and `executeAction` |
| 57 | +- Validates API constants and their relationships |
| 58 | +- Tests error handling and parameter validation |
| 59 | + |
| 60 | +**playground-sdk.test.ts** |
| 61 | +- Tests the main PlaygroundSDK class |
| 62 | +- Validates adapter selection logic |
| 63 | +- Tests delegation to appropriate adapters |
| 64 | +- Mocks underlying adapters for isolation |
| 65 | + |
| 66 | +**base-adapter.test.ts** |
| 67 | +- Tests the abstract BasePlaygroundAdapter class |
| 68 | +- Validates common validation logic |
| 69 | +- Tests parameter filtering and display content creation |
| 70 | +- Tests helper methods and error handling |
| 71 | + |
| 72 | +**local-execution-adapter.test.ts** |
| 73 | +- Tests LocalExecutionAdapter specific functionality |
| 74 | +- Validates local agent interaction |
| 75 | +- Tests progress tracking and task cancellation |
| 76 | +- Tests parameter parsing for local execution |
| 77 | + |
| 78 | +**remote-execution-adapter.test.ts** |
| 79 | +- Tests RemoteExecutionAdapter specific functionality |
| 80 | +- Validates server communication methods |
| 81 | +- Tests Android-specific error formatting |
| 82 | +- Mocks fetch calls for server interactions |
| 83 | + |
| 84 | +**types.test.ts** |
| 85 | +- Validates TypeScript type definitions |
| 86 | +- Ensures interfaces accept valid objects |
| 87 | +- Tests type compatibility and extensibility |
| 88 | + |
| 89 | +### Integration Tests |
| 90 | + |
| 91 | +**playground-integration.test.ts** |
| 92 | +- End-to-end workflow testing |
| 93 | +- Tests complete scenarios from SDK creation to action execution |
| 94 | +- Validates cross-adapter compatibility |
| 95 | +- Tests real-world usage patterns |
| 96 | + |
| 97 | +## Test Setup |
| 98 | + |
| 99 | +The `setup.ts` file provides: |
| 100 | +- Global mock configuration |
| 101 | +- Console method mocking to reduce test noise |
| 102 | +- Browser global mocking for server-side tests |
| 103 | +- Automatic cleanup between tests |
| 104 | + |
| 105 | +## Mocking Strategy |
| 106 | + |
| 107 | +- **External Dependencies**: Mocked using Vitest's `vi.mock()` |
| 108 | +- **Network Calls**: Mocked using global `fetch` mock |
| 109 | +- **Console Output**: Suppressed during tests to reduce noise |
| 110 | +- **Browser APIs**: Mocked for Node.js environment compatibility |
| 111 | + |
| 112 | +## Coverage |
| 113 | + |
| 114 | +The test suite aims for high coverage of: |
| 115 | +- All exported functions and classes |
| 116 | +- Error handling paths |
| 117 | +- Edge cases and boundary conditions |
| 118 | +- Type safety and interface compliance |
| 119 | + |
| 120 | +Coverage reports are generated in `coverage/` directory when running with `--coverage` flag. |
| 121 | + |
| 122 | +## Writing New Tests |
| 123 | + |
| 124 | +When adding new functionality: |
| 125 | + |
| 126 | +1. **Unit Tests**: Add tests in the appropriate `tests/unit/*.test.ts` file |
| 127 | +2. **Integration Tests**: Add end-to-end scenarios to `playground-integration.test.ts` |
| 128 | +3. **Type Tests**: Add type validation to `types.test.ts` for new interfaces |
| 129 | +4. **Mocking**: Use existing mock patterns and add new mocks as needed |
| 130 | + |
| 131 | +### Test Naming Convention |
| 132 | + |
| 133 | +- Describe blocks: Use present tense describing the component |
| 134 | +- Test cases: Use "should" statements describing expected behavior |
| 135 | +- Mock functions: Prefix with "mock" for clarity |
| 136 | + |
| 137 | +### Example Test Structure |
| 138 | + |
| 139 | +```typescript |
| 140 | +describe('ComponentName', () => { |
| 141 | + let component: ComponentType; |
| 142 | + |
| 143 | + beforeEach(() => { |
| 144 | + component = new ComponentType(); |
| 145 | + }); |
| 146 | + |
| 147 | + describe('methodName', () => { |
| 148 | + it('should handle normal case', () => { |
| 149 | + // Test implementation |
| 150 | + }); |
| 151 | + |
| 152 | + it('should handle error case', () => { |
| 153 | + // Error test implementation |
| 154 | + }); |
| 155 | + }); |
| 156 | +}); |
| 157 | +``` |
0 commit comments