Files
renovate/test/setup.ts
secustor 3890ecf3b5 refactor(platform): provide defaults for optional Platform methods once
Finding P10. Optional `Platform` members were guarded ad hoc at ~12 call
sites in three styles: optional call (`platform.labelCharLimit?.() ?? 50`),
explicit guard (the identical `if (platform.refreshPr)` block three times)
and ternary fallback (`platform.filterUnavailableUsers ? ... : ...`).

The type is now split. `PlatformModule` is what a platform module exports
and what `api.ts` registers; `Platform` is that module type with the
defaulted members made required, and is what the exported `platform` Proxy
hands to consumers. `setPlatformApi` composes
`{ ...platformDefaults, ...platformModule }` — spreading an ES module
namespace copies its own enumerable exports and keeps function identity,
so `platform.x` stays referentially stable.

Members made required, with their default:

- getIssue -> null
- getVulnerabilityAlerts -> []
- getBranchForceRebase -> false
- refreshPr -> no-op
- expandGroupMembers -> identity
- filterUnavailableUsers -> identity
- labelCharLimit -> 50
- extractRulesFromCodeOwnersLines -> the plain CODEOWNERS parser that
  used to live in `workers/repository/update/pr/code-owners.ts`

Members left optional, because consumers read their absence as a
capability flag rather than as "do nothing":

- tryReuseAutoclosedPr — `pr-reuse.ts` skips the `findPr` call entirely
- reattemptPlatformAutomerge — `branch/index.ts` skips the dry-run log line
- isBranchMergeQueueEnabled / isPrInMergeQueue — a platform either has a
  merge queue, in which case `mergePr` enqueues instead of merging, or it
  has neither member
- addLabel, commitFiles — no consumer reaches them through `platform`
- experimental — a marker, not a method

`syncForkWithUpstream` is on `PlatformScm`, not `Platform`, so it is out
of scope here.

Behaviour changes:

- `init/index.ts` warns about unsupported `filterUnavailableUsers` /
  `expandCodeOwnersGroups` through the new `platformSupports()` helper,
  which compares the selected platform's member against the shared
  default. The warnings are unchanged.
- `config-migration/branch/index.ts` now calls `getBranchPr` on every
  platform rather than only on those implementing `refreshPr`; the extra
  lookup happens only when a config-migration PR was just rebased.
- `onboarding/branch/index.ts` and `update/branch/index.ts` drop an
  `istanbul ignore if` and a `v8 ignore else`; both `refreshPr` calls are
  now covered.
2026-09-20 20:40:45 +02:00

40 lines
1.1 KiB
TypeScript

// Check for missing or pending http mocks
import './http-mock.ts';
import { mockDeep } from 'vitest-mock-extended';
import type { Platform, PlatformScm } from '../lib/modules/platform/index.ts';
import * as _fixtures from './fixtures.ts';
// Set timezone so snapshots are consistent
process.env.TZ = 'UTC';
vi.mock('../lib/modules/platform/index.ts', () => ({
platform: mockDeep<Platform>(),
initPlatform: vi.fn(),
getPlatformList: vi.fn(),
platformSupports: vi.fn(),
}));
vi.mock('../lib/modules/platform/scm.ts', () => ({
scm: mockDeep<PlatformScm>(),
}));
vi.mock('../lib/logger/index.ts', () => {
return mockDeep({
withMeta: <T>(_: Record<string, unknown>, cb: () => T): T => cb(),
});
});
vi.mock('../lib/util/git/index.ts', () => mockDeep());
vi.mock('../lib/util/exec/common.ts', () => ({ rawExec: vi.fn() }));
Object.defineProperty(global, 'fixtures', { value: _fixtures });
declare global {
const fixtures: typeof _fixtures;
}
vi.mock('../lib/util/mutex.ts', () => ({
initMutexes: () => vi.fn(),
acquireLock: () => vi.fn().mockImplementation(() => () => undefined),
}));