Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ export default [
"newlines-between": "always",
},
],
"jsdoc/no-undefined-types": [1, { definedTypes: ["NodeJS"] }],
"jsdoc/no-undefined-types": [
1,
{ definedTypes: ["NodeJS", "RequestInit"] },
],
"jsdoc/require-hyphen-before-param-description": "warn",
"unicorn/comment-content": "off",
"unicorn/consistent-optional-chaining": "off",
Expand Down
110 changes: 110 additions & 0 deletions helpers/mock-agent/store-bitbucket.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { MockAgent } from "undici";

/**
* @param {string} message - Error message
* @returns {object} Bitbucket-shaped error response body
*/
function errorBody(message) {
return { type: "error", error: { message } };
}

/**
* @returns {import("undici").MockAgent} Undici MockAgent
* @see {@link https://undici.nodejs.org/#/docs/api/MockAgent}
*/
export function mockClient() {
const agent = new MockAgent();
agent.disableNetConnect();

const origin = "https://api.bitbucket.org";
const sourcePath = "/2.0/repositories/username/repo/src";

// File exists (foo.txt)
agent
.get(origin)
.intercept({
path: `${sourcePath}/main/foo.txt`,
method: "GET",
query: { format: "meta" },
})
.reply(200, { path: "foo.txt", type: "commit_file" })
.persist();

// File doesn’t exist (404.txt, new.txt, 401.txt)
agent
.get(origin)
.intercept({
path: /\/main\/(404|401|new)\.txt\?format=meta$/,
method: "GET",
})
.reply(404, errorBody("Not found"))
.persist();

// Read file (foo.txt)
agent
.get(origin)
.intercept({ path: `${sourcePath}/main/foo.txt`, method: "GET" })
.reply(200, "foo")
.persist();

// Read file (Not found)
agent
.get(origin)
.intercept({ path: `${sourcePath}/main/404.txt`, method: "GET" })
.reply(404, errorBody("Not found"))
.persist();

/**
* Bitbucket’s `src` endpoint is a single fixed URL for every create,
* update and delete — the file path only appears as a field name inside
* the multipart request body, which Undici’s `MockAgent` can’t inspect
* (it sees a `FormData` body as the literal string `[object FormData]`).
* So, rather than matching per file, these are registered as a queue:
* each `it()` block below consumes the next one in order.
*/

// 1. Creates file (new.txt)
agent.get(origin).intercept({ path: sourcePath, method: "POST" }).reply(201, {
type: "commit",
});

// 2. Throws error creating file (401.txt)
agent
.get(origin)
.intercept({ path: sourcePath, method: "POST" })
.reply(401, errorBody("Unauthorized"));

// 3. Updates file (foo.txt)
agent.get(origin).intercept({ path: sourcePath, method: "POST" }).reply(200, {
type: "commit",
});

// 4. Updates and renames file (bar.txt) …
agent.get(origin).intercept({ path: sourcePath, method: "POST" }).reply(200, {
type: "commit",
});

// …5. … then deletes the old path (foo.txt)
agent.get(origin).intercept({ path: sourcePath, method: "POST" }).reply(200, {
type: "commit",
});

// 6. Throws error updating file (401.txt)
agent
.get(origin)
.intercept({ path: sourcePath, method: "POST" })
.reply(401, errorBody("Unauthorized"));

// 7. Deletes a file (foo.txt)
agent.get(origin).intercept({ path: sourcePath, method: "POST" }).reply(200, {
type: "commit",
});

// 8. Throws error deleting a file (401.txt)
agent
.get(origin)
.intercept({ path: sourcePath, method: "POST" })
.reply(401, errorBody("Unauthorized"));

return agent;
}
163 changes: 1 addition & 162 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@
"mock-req-res": "^1.2.1",
"mock-session": "^0.0.5",
"mongodb-memory-server": "^11.2.0",
"nock": "^14.0.16",
"prettier": "^3.9.4",
"stylelint": "^17.14.0",
"stylelint-config-recommended": "^18.0.0",
Expand Down
Loading
Loading