|
1 | | -## It takes 3 steps to run the tests :) |
| 1 | +# API Tester |
2 | 2 |
|
3 | | -1. run `npm install` |
4 | | -2. copy `example_config.yml` and add the correct values |
5 | | -3. run `node apitest.js --config=your_config_file.yml` |
| 3 | +A test framework for testing the backend API of puter. |
| 4 | + |
| 5 | +## Table of Contents |
| 6 | + |
| 7 | +- [API Tester](#api-tester) |
| 8 | +- [How to use](#how-to-use) |
| 9 | + - [Workflow](#workflow) |
| 10 | + - [Shorthands](#shorthands) |
| 11 | +- [Basic Concepts](#basic-concepts) |
| 12 | +- [Behaviors](#behaviors) |
| 13 | + - [Isolation of `t.cwd`](#isolation-of-t-cwd) |
| 14 | +- [Implementation](#implementation) |
| 15 | +- [TODO](#todo) |
| 16 | + |
| 17 | +## How to use |
| 18 | + |
| 19 | +### Workflow |
| 20 | + |
| 21 | +All commands below should be run from the root directory of puter. |
| 22 | + |
| 23 | +1. (Optional) Start a backend server: |
| 24 | + |
| 25 | + ```bash |
| 26 | + npm start |
| 27 | + ``` |
| 28 | + |
| 29 | +2. Copy `example_config.yml` and add the correct values: |
| 30 | + |
| 31 | + ```bash |
| 32 | + cp ./tools/api-tester/example_config.yml ./tools/api-tester/config.yml |
| 33 | + ``` |
| 34 | + |
| 35 | + Fields: |
| 36 | + - url: The endpoint of the backend server. (default: http://api.puter.localhost:4100/) |
| 37 | + - username: The username of the admin user. (e.g. admin) |
| 38 | + - token: The token of the user. (can be obtained by typing `puter.authToken` in Developer Tools's console) |
| 39 | +
|
| 40 | +3. Run the tests: |
| 41 | +
|
| 42 | + ```bash |
| 43 | + node ./tools/api-tester/apitest.js --config=./tools/api-tester/config.yml |
| 44 | + ``` |
| 45 | +
|
| 46 | +### Shorthands |
| 47 | +
|
| 48 | +- Run unit tests only: |
| 49 | +
|
| 50 | + ```bash |
| 51 | + node ./tools/api-tester/apitest.js --config=./tools/api-tester/config.yml --unit |
| 52 | + ``` |
| 53 | +
|
| 54 | +- Filter tests by suite name: |
| 55 | +
|
| 56 | + ```bash |
| 57 | + node ./tools/api-tester/apitest.js --config=./tools/api-tester/config.yml --unit --suite=mkdir |
| 58 | + ``` |
| 59 | +
|
| 60 | +- Rerun failed tests in the last run: |
| 61 | +
|
| 62 | + ```bash |
| 63 | + node ./tools/api-tester/apitest.js --config=./tools/api-tester/config.yml --rerun-failed |
| 64 | + ``` |
| 65 | +
|
| 66 | +## Basic Concepts |
| 67 | +
|
| 68 | +A *test case* is a function that tests a specific behavior of the backend API. Test cases can be nested: |
| 69 | +
|
| 70 | +```js |
| 71 | +await t.case('normal mkdir', async () => { |
| 72 | + const result = await t.mkdir_v2('foo'); |
| 73 | + expect(result.name).equal('foo'); |
| 74 | +
|
| 75 | + await t.case('can stat the created directory', async () => { |
| 76 | + const stat = await t.stat('foo'); |
| 77 | + expect(stat.name).equal('foo'); |
| 78 | + }); |
| 79 | +}); |
| 80 | +``` |
| 81 | +
|
| 82 | +A *test suite* is a collection of test cases. A `.js` file should contain exactly one test suite. |
| 83 | +
|
| 84 | +```js |
| 85 | +module.exports = { |
| 86 | + name: 'mkdir', |
| 87 | + do: async t => { |
| 88 | + await t.case('normal mkdir', async () => { |
| 89 | + ... |
| 90 | + }); |
| 91 | +
|
| 92 | + await t.case('recursive mkdir', async () => { |
| 93 | + ... |
| 94 | + }); |
| 95 | + } |
| 96 | +}; |
| 97 | +``` |
| 98 | +
|
| 99 | +## Behaviors |
| 100 | +
|
| 101 | +### Isolation of `t.cwd` |
| 102 | +
|
| 103 | +- `t.cwd` is reset at the beginning of each test suite, since a test suite usually doesn't want to be affected by other test suites. |
| 104 | +- `t.cwd` will be inherited from the cases in the same test suite, since a leaf case might want to share the context with its parent/sibling cases. |
| 105 | + |
| 106 | +```js |
| 107 | +module.exports = { |
| 108 | + name: 'readdir', |
| 109 | + do: async t => { |
| 110 | + // t.cwd is reset to /admin/api_test |
| 111 | +
|
| 112 | + await t.case('normal mkdir', async () => { |
| 113 | + // inherits cwd from parent/sibling cases |
| 114 | +
|
| 115 | + await t.case('mkdir in subdir', async () => { |
| 116 | + // inherits cwd from parent/sibling cases |
| 117 | + }); |
| 118 | + }); |
| 119 | + } |
| 120 | +}; |
| 121 | +``` |
| 122 | +
|
| 123 | +## Implementation |
| 124 | +
|
| 125 | +- Test suites are registered in `tools/api-tester/tests/__entry__.js`. |
| 126 | +
|
| 127 | +## TODO |
| 128 | +
|
| 129 | +- [ ] Update usage of apitest.js. (Is it possible to generate the usage automatically?) |
| 130 | +- [ ] Integrate it into CI, optionally running it only in specific scenarios (e.g., when backend code changes). |
0 commit comments