kaovilai / kaovilai/github-bot-command-palette

[daily] test: add background.js unit tests and fix trailing-slash bug in buildConfigFileUrl

Open
#64 0 comments 0 reactions 0 assignees View on GitHub
agentic-workflows automation
Dominant language
JavaScript
Stars
1
Forks
2
Avg merge
2m
Merged PRs (30d)
15

Description

Two related improvements bundled together.

## Changes

### 1. New unit tests for `background.js` (12 tests)

`background.js` contains complex YAML-parsing and URL-building logic used to fetch Prow plugin configuration and presubmit jobs. Despite being the most failure-prone code path (network, YAML parsing, cache logic), it had **zero unit tests**.

Added `tests/background.test.js` covering:

**`extractPlugins`** — YAML plugin extraction:
- Empty YAML returns empty array
- No result when repo/org not in config
- Repo-level match (list format)
- Org-level match
- Nested object format (`plugins.plugins`)
- Deduplication when repo and org both match
- Top-level section format (`approve: [{repos: [org/repo]}]`)
- Org-level top-level section match
- Invalid YAML throws `YAMLException` (documents that callers must handle)

**`buildConfigFileUrl`** — URL generation:
- Flat source produces correct blob URL
- Sharded source produces correct path
- Multiple trailing slashes are stripped cleanly (see bug fix below)

### 2. Bug fix: multiple trailing slashes in `pathTemplate` produced double-slash URLs

`fetchYaml` and `buildConfigFileUrl` both did:
```js
const basePath = source.pathTemplate.replace(/\/$/, '');
```
The regex `/\/$/` only removes **one** trailing slash. A `pathTemplate` ending in `//` would produce a URL like `.../path//org/repo/_pluginconfig.yaml` — a broken URL.

Fixed by using `/\/+$/` (one-or-more) in both locations.

### 3. Maintenance comment in `background.js`

Added a comment on the `knownPlugins` list noting it must stay in sync with the keys of `GHBCP_PROW_PLUGIN_MAP` in `prow-plugin-map.js`, preventing silent drift when new plugins are added.

### 4. `package.json` test script updated

`tests/background.test.js` is now included in `npm test`.

---

> [!WARNING]
> **Protected Files — Push Permission Denied**
>
> This was originally intended as a pull request, but the patch modifies protected files. A human must create the pull request manually.
>
>
> Protected files
>
> - `package.json`
>
> The push was rejected because GitHub Actions does not have `workflows` permission to push these changes, and is never allowed to make such changes, or other authorization being used does not have this permission.
>
>

Create the pull request manually

```sh
# Download the patch from the workflow run
gh run download 26053802514 -n agent -D /tmp/agent-26053802514

# Create a new branch
git checkout -b test/background-unit-tests-75f834f4b956e5b9 main

# Apply the patch (--3way handles cross-repo patches)
git am --3way /tmp/agent-26053802514/aw-test-background-unit-tests.patch

# Push the branch and create the pull request
git push origin test/background-unit-tests-75f834f4b956e5b9
gh pr create --title '[daily] test: add background.js unit tests and fix trailing-slash bug in buildConfigFileUrl' --base main --head test/background-unit-tests-75f834f4b956e5b9 --repo kaovilai/github-bot-command-palette
```

> Generated by [Daily Repository Improvement](https://github.com/kaovilai/github-bot-command-palette/actions/runs/26053802514) · ● 23.3M · [◷](https://github.com/search?q=repo%3Akaovilai%2Fgithub-bot-command-palette+%22gh-aw-workflow-id%3A+continuous-improvement%22&type=pullrequests)

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.