|
1 | 1 | import { describe, it, expect } from 'vitest'; |
2 | | -import type { IJobService, JobHandler, JobRunOutcome, JobExecution } from './job-service'; |
| 2 | +import { readFileSync } from 'node:fs'; |
| 3 | +import { fileURLToPath } from 'node:url'; |
| 4 | +import type { IJobService, JobHandler, JobRunOutcome, JobExecution, JobReplayOptions } from './job-service'; |
3 | 5 |
|
4 | 6 | describe('Job Service Contract', () => { |
5 | 7 | it('should allow a minimal IJobService implementation with required methods', () => { |
@@ -244,3 +246,103 @@ describe('[#6617] JobHandler degraded-outcome channel', () => { |
244 | 246 | expect(thrownAttempts).toBe(4); // ← a throw still retries, unchanged |
245 | 247 | }); |
246 | 248 | }); |
| 249 | + |
| 250 | +/** |
| 251 | + * [#14766] `IJobService.replay` gains an optional third argument carrying |
| 252 | + * `force: true` — the contract half of the maintainer's A + a2 ruling on |
| 253 | + * #14501 (whose behaviour half lands in `DbJobAdapter.replay`). |
| 254 | + * |
| 255 | + * COMPILE-TIME pins first (`tsconfig.test.json` type-checks this file under |
| 256 | + * `check:test-typecheck`, so an assignability pin here is a real check), and |
| 257 | + * one source-reading pin for the prose the services implementer codes |
| 258 | + * against: the refusal is declared on the contract, and prose is unassertable |
| 259 | + * except by reading it. Reverse verification for the block: narrow the |
| 260 | + * signature back to `(name, data?)` — (b), (c) and the identity pin go red, |
| 261 | + * (a) stays green. That asymmetry IS additivity. |
| 262 | + */ |
| 263 | +describe('[#14766] IJobService.replay force option — contract half of the #14501 A+a2 ruling', () => { |
| 264 | + type Eq<A, B> = (<T>() => T extends A ? 1 : 2) extends (<T>() => T extends B ? 1 : 2) ? true : false; |
| 265 | + type ReplayParams = Parameters<NonNullable<IJobService['replay']>>; |
| 266 | + |
| 267 | + /** |
| 268 | + * The replay signature EXACTLY as it stood before #14766, pinned standalone |
| 269 | + * so the additivity claim is falsifiable: if the third parameter ever stops |
| 270 | + * being optional, this implementation stops being assignable. |
| 271 | + */ |
| 272 | + type PreIssue14766Replay = (name: string, data?: unknown) => Promise<void>; |
| 273 | + |
| 274 | + const base = { |
| 275 | + schedule: async () => {}, |
| 276 | + cancel: async () => {}, |
| 277 | + trigger: async () => {}, |
| 278 | + } satisfies Pick<IJobService, 'schedule' | 'cancel' | 'trigger'>; |
| 279 | + |
| 280 | + it('(a) an existing two-argument replay implementation is unchanged — additivity, implementer side', async () => { |
| 281 | + // `DbJobAdapter.replay(name, data?)` as it stands on main: it declares no |
| 282 | + // third parameter, and must keep compiling untouched (#14501 widens it). |
| 283 | + const legacy: PreIssue14766Replay = async (_name, _data) => {}; |
| 284 | + const service: IJobService = { ...base, replay: legacy }; |
| 285 | + |
| 286 | + await expect(service.replay!('digest')).resolves.toBeUndefined(); |
| 287 | + await expect(service.replay!('digest', { since: 'yesterday' })).resolves.toBeUndefined(); |
| 288 | + }); |
| 289 | + |
| 290 | + it('(b) the options type is exported, and a call site passes { force: true } through it', async () => { |
| 291 | + const seen: Array<JobReplayOptions | undefined> = []; |
| 292 | + const service: IJobService = { |
| 293 | + ...base, |
| 294 | + replay: async (_name, _data, options) => { |
| 295 | + seen.push(options); |
| 296 | + }, |
| 297 | + }; |
| 298 | + |
| 299 | + // THE pin that goes red when the third parameter is removed. |
| 300 | + const force: JobReplayOptions = { force: true }; |
| 301 | + await service.replay!('digest', undefined, force); |
| 302 | + await service.replay!('digest', undefined, { force: false }); |
| 303 | + await service.replay!('digest'); |
| 304 | + |
| 305 | + expect(seen).toEqual([{ force: true }, { force: false }, undefined]); |
| 306 | + }); |
| 307 | + |
| 308 | + it('(c) the third parameter IS JobReplayOptions, optional, and force is its only member', () => { |
| 309 | + // Identity, not assignability: a widening or a narrowing on either side |
| 310 | + // turns this alias red under check:test-typecheck. |
| 311 | + const identity: Eq<ReplayParams[2], JobReplayOptions | undefined> = true; |
| 312 | + expect(identity).toBe(true); |
| 313 | + |
| 314 | + const bare: JobReplayOptions = {}; |
| 315 | + expect(bare.force).toBeUndefined(); |
| 316 | + |
| 317 | + // @ts-expect-error force is a boolean — a truthy string is not the door. |
| 318 | + const stringly: JobReplayOptions = { force: 'yes' }; |
| 319 | + expect(stringly.force).toBe('yes'); |
| 320 | + |
| 321 | + // @ts-expect-error force is the ONLY knob; a second one is a spec decision |
| 322 | + // (a new key on this interface), not a free-text field. |
| 323 | + const extra: JobReplayOptions = { force: true, skipClaim: true }; |
| 324 | + expect(extra.force).toBe(true); |
| 325 | + }); |
| 326 | + |
| 327 | + it('(d) the contract text declares the refusal the services half codes against', () => { |
| 328 | + const source = readFileSync(fileURLToPath(new URL('./job-service.ts', import.meta.url)), 'utf8'); |
| 329 | + const start = source.indexOf('replay?(name: string, data?: unknown, options?: JobReplayOptions)'); |
| 330 | + expect(start).toBeGreaterThan(0); |
| 331 | + // The doc block immediately above the declaration. |
| 332 | + const docBlock = source.slice(source.lastIndexOf('/**', start), start); |
| 333 | + |
| 334 | + // The three outcomes, by the claim state that selects them… |
| 335 | + expect(docBlock).toContain('(flow, tick-window)'); |
| 336 | + expect(docBlock).toContain('**absent**'); |
| 337 | + expect(docBlock).toContain('**failed**'); |
| 338 | + expect(docBlock).toContain('**succeeded**'); |
| 339 | + // …the refusal as an ADR-0112 envelope on code AND status, naming what it refuses… |
| 340 | + expect(docBlock).toContain('ADR-0112'); |
| 341 | + expect(docBlock).toContain("code: 'RESOURCE_CONFLICT'"); |
| 342 | + expect(docBlock).toContain('status: 409'); |
| 343 | + expect(docBlock).toContain('**the window**'); |
| 344 | + expect(docBlock).toContain('**the claim**'); |
| 345 | + // …and the one door past it. |
| 346 | + expect(docBlock).toContain('options.force: true'); |
| 347 | + }); |
| 348 | +}); |
0 commit comments