|
| 1 | +import { MockAgent } from "undici"; |
| 2 | + |
| 3 | +/** |
| 4 | + * @param {string} message - Error message |
| 5 | + * @returns {object} Bitbucket-shaped error response body |
| 6 | + */ |
| 7 | +function errorBody(message) { |
| 8 | + return { type: "error", error: { message } }; |
| 9 | +} |
| 10 | + |
| 11 | +/** |
| 12 | + * @returns {import("undici").MockAgent} Undici MockAgent |
| 13 | + * @see {@link https://undici.nodejs.org/#/docs/api/MockAgent} |
| 14 | + */ |
| 15 | +export function mockClient() { |
| 16 | + const agent = new MockAgent(); |
| 17 | + agent.disableNetConnect(); |
| 18 | + |
| 19 | + const origin = "https://api.bitbucket.org"; |
| 20 | + const sourcePath = "/2.0/repositories/username/repo/src"; |
| 21 | + |
| 22 | + // File exists (foo.txt) |
| 23 | + agent |
| 24 | + .get(origin) |
| 25 | + .intercept({ |
| 26 | + path: `${sourcePath}/main/foo.txt`, |
| 27 | + method: "GET", |
| 28 | + query: { format: "meta" }, |
| 29 | + }) |
| 30 | + .reply(200, { path: "foo.txt", type: "commit_file" }) |
| 31 | + .persist(); |
| 32 | + |
| 33 | + // File doesn’t exist (404.txt, new.txt, 401.txt) |
| 34 | + agent |
| 35 | + .get(origin) |
| 36 | + .intercept({ |
| 37 | + path: /\/main\/(404|401|new)\.txt\?format=meta$/, |
| 38 | + method: "GET", |
| 39 | + }) |
| 40 | + .reply(404, errorBody("Not found")) |
| 41 | + .persist(); |
| 42 | + |
| 43 | + // Read file (foo.txt) |
| 44 | + agent |
| 45 | + .get(origin) |
| 46 | + .intercept({ path: `${sourcePath}/main/foo.txt`, method: "GET" }) |
| 47 | + .reply(200, "foo") |
| 48 | + .persist(); |
| 49 | + |
| 50 | + // Read file (Not found) |
| 51 | + agent |
| 52 | + .get(origin) |
| 53 | + .intercept({ path: `${sourcePath}/main/404.txt`, method: "GET" }) |
| 54 | + .reply(404, errorBody("Not found")) |
| 55 | + .persist(); |
| 56 | + |
| 57 | + /** |
| 58 | + * Bitbucket’s `src` endpoint is a single fixed URL for every create, |
| 59 | + * update and delete — the file path only appears as a field name inside |
| 60 | + * the multipart request body, which Undici’s `MockAgent` can’t inspect |
| 61 | + * (it sees a `FormData` body as the literal string `[object FormData]`). |
| 62 | + * So, rather than matching per file, these are registered as a queue: |
| 63 | + * each `it()` block below consumes the next one in order. |
| 64 | + */ |
| 65 | + |
| 66 | + // 1. Creates file (new.txt) |
| 67 | + agent.get(origin).intercept({ path: sourcePath, method: "POST" }).reply(201, { |
| 68 | + type: "commit", |
| 69 | + }); |
| 70 | + |
| 71 | + // 2. Throws error creating file (401.txt) |
| 72 | + agent |
| 73 | + .get(origin) |
| 74 | + .intercept({ path: sourcePath, method: "POST" }) |
| 75 | + .reply(401, errorBody("Unauthorized")); |
| 76 | + |
| 77 | + // 3. Updates file (foo.txt) |
| 78 | + agent.get(origin).intercept({ path: sourcePath, method: "POST" }).reply(200, { |
| 79 | + type: "commit", |
| 80 | + }); |
| 81 | + |
| 82 | + // 4. Updates and renames file (bar.txt) … |
| 83 | + agent.get(origin).intercept({ path: sourcePath, method: "POST" }).reply(200, { |
| 84 | + type: "commit", |
| 85 | + }); |
| 86 | + |
| 87 | + // …5. … then deletes the old path (foo.txt) |
| 88 | + agent.get(origin).intercept({ path: sourcePath, method: "POST" }).reply(200, { |
| 89 | + type: "commit", |
| 90 | + }); |
| 91 | + |
| 92 | + // 6. Throws error updating file (401.txt) |
| 93 | + agent |
| 94 | + .get(origin) |
| 95 | + .intercept({ path: sourcePath, method: "POST" }) |
| 96 | + .reply(401, errorBody("Unauthorized")); |
| 97 | + |
| 98 | + // 7. Deletes a file (foo.txt) |
| 99 | + agent.get(origin).intercept({ path: sourcePath, method: "POST" }).reply(200, { |
| 100 | + type: "commit", |
| 101 | + }); |
| 102 | + |
| 103 | + // 8. Throws error deleting a file (401.txt) |
| 104 | + agent |
| 105 | + .get(origin) |
| 106 | + .intercept({ path: sourcePath, method: "POST" }) |
| 107 | + .reply(401, errorBody("Unauthorized")); |
| 108 | + |
| 109 | + return agent; |
| 110 | +} |
0 commit comments