Skip to content

Commit d528a2a

Browse files
authored
fix: correct the fetch and translate types, await static's writes, and keep tests offline (#148)
Co-authored-by: botandbang <337765928+botandbang@users.noreply.github.com>
1 parent 37cd91d commit d528a2a

32 files changed

Lines changed: 387 additions & 251 deletions

‎.github/workflows/ci.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ jobs:
5555
- '.github/workflows/ci.yml'
5656
aliases: [*shared, 'aliases/**']
5757
core: [*shared, 'core/**', 'fetchindex/**', 'parsefiles/**', 'fixtures/**']
58-
fetchindex: [*shared, 'fetchindex/**']
58+
fetchindex: [*shared, 'fetchindex/**', 'fixtures/**']
5959
fixtures: [*shared, 'fixtures/**']
6060
newest: [*shared, 'newest/**', 'core/**', 'fetchindex/**', 'parsefiles/**', 'fixtures/**']
6161
oldest: [*shared, 'oldest/**', 'core/**', 'fetchindex/**', 'parsefiles/**', 'fixtures/**']

‎core/index.js‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@ const optionsParser = require('./util/prod/optionsParser');
55
const versionFetcher = require('@nodevu/fetchindex');
66
const scheduleFetcher = require('./util/prod/schedule');
77

8+
/**
9+
* The part of `fetch` core uses, which `@nodevu/fetchindex` defines.
10+
*
11+
* @typedef {import('@nodevu/fetchindex').Fetch} Fetch
12+
*/
13+
814
/**
915
* @typedef {object} Options
10-
* @property {typeof fetch} [fetch] The `fetch` implementation to request the
16+
* @property {Fetch} [fetch] The `fetch` implementation to request the
1117
* data with. Defaults to `globalThis.fetch`.
1218
* @property {string | import('luxon').DateTime} [now] An ISO 8601 date to work
1319
* out each release line's phase against. Defaults to now.

‎core/test/combos.js‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const assert = require('node:assert');
22
const nodevu = require('../index');
33
const { describe, it, beforeEach } = require('node:test');
4-
const undici = require('undici');
4+
const { fetch: undiciFetch } = require('undici');
55

66
const beforeEachTemplate = require('@nodevu/fixtures/beforeEachTemplate');
77

@@ -32,12 +32,6 @@ function check(data) {
3232

3333
const now = require('@nodevu/fixtures/data/now.json');
3434

35-
// undici's types only take its own Request class, so they don't match the
36-
// global fetch's type even though it works as one
37-
const undiciFetch = /** @type {typeof fetch} */ (
38-
/** @type {unknown} */ (undici.fetch)
39-
);
40-
4135
describe('check to make sure that combining options works as expected', async () => {
4236
beforeEach(beforeEachTemplate);
4337

‎core/test/parseOptions.js‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
const { deepStrictEqual, ok } = require('node:assert');
22
const { describe, it } = require('node:test');
3-
const undici = require('undici');
3+
const { fetch: undiciFetch } = require('undici');
44
const { DateTime } = require('luxon');
55
const parseOptions = require('../util/prod/optionsParser');
66

7-
// undici's types only take its own Request class, so they don't match the
8-
// global fetch's type even though it works as one
9-
const undiciFetch = /** @type {typeof fetch} */ (
10-
/** @type {unknown} */ (undici.fetch)
11-
);
12-
137
describe('the parseOptions module should return all correct defaults', async () => {
148
it('should return the default date', async () => {
159
const now = DateTime.now();

‎core/test/schedule.js‎

Lines changed: 61 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
const { match, rejects } = require('node:assert');
1+
const { deepStrictEqual, match, rejects } = require('node:assert');
22
const { describe, it } = require('node:test');
3-
const undici = require('undici');
3+
const {
4+
fetch: undiciFetch,
5+
MockAgent,
6+
setGlobalDispatcher,
7+
} = require('undici');
48
const schedule = require('../util/prod/schedule');
59
const optionsParser = require('../util/prod/optionsParser');
6-
7-
// undici's types only take its own Request class, so they don't match the
8-
// global fetch's type even though it works as one
9-
const undiciFetch = /** @type {typeof fetch} */ (
10-
/** @type {unknown} */ (undici.fetch)
11-
);
12-
13-
/** @typedef {import('../util/prod/optionsParser').ParsedOptions} ParsedOptions */
10+
const { at } = require('@nodevu/fixtures');
11+
const scheduleSnapshot = require('@nodevu/fixtures/data/schedule.json');
12+
const now = require('@nodevu/fixtures/data/now.json');
1413

1514
// checks that verify the result of data returned
1615
/** @param {import('../util/prod/schedule').Schedule} data */
@@ -27,43 +26,65 @@ function check(data) {
2726
}
2827
}
2928

30-
// set up options object that would normally be passed to the module. the
31-
// module only reads fetch and its own URL
32-
const options = /** @type {ParsedOptions} */ ({
33-
fetch: globalThis.fetch,
34-
urls: {
35-
schedule:
36-
'https://raw.githubusercontent.com/nodejs/Release/master/schedule.json',
37-
},
38-
});
29+
const url =
30+
'https://raw.githubusercontent.com/nodejs/Release/master/schedule.json';
31+
32+
// undici's fetch goes through this mock instead of the network: it serves
33+
// each intercept() once and refuses any other request
34+
const agent = new MockAgent();
35+
36+
agent.disableNetConnect();
37+
38+
setGlobalDispatcher(agent);
39+
40+
describe('under normal conditions, schedule should work', async () => {
41+
it('should work with default options', async (t) => {
42+
const fetch = t.mock.method(globalThis, 'fetch', at(now).fetch);
43+
44+
const data = await schedule({
45+
fetch: globalThis.fetch,
46+
urls: { schedule: url },
47+
});
3948

40-
describe('under normal condiditons, schedule should work', async () => {
41-
it('should work with default options', async () => {
42-
const data = await schedule(options);
4349
check(data);
50+
deepStrictEqual(requested(fetch), [url]);
4451
});
4552

4653
it('should work with Undici fetch', async () => {
47-
options.fetch = undiciFetch;
48-
const data = await schedule(options);
54+
intercept();
55+
56+
const data = await schedule({
57+
fetch: undiciFetch,
58+
urls: { schedule: url },
59+
});
60+
4961
check(data);
62+
agent.assertNoPendingInterceptors();
5063
});
5164
});
5265

5366
describe('schedule should work with optionsParser', async () => {
54-
it('should work with the default output of optionsParser', async () => {
67+
it('should work with the default output of optionsParser', async (t) => {
68+
const fetch = t.mock.method(globalThis, 'fetch', at(now).fetch);
69+
5570
const parsedOptions = optionsParser({});
5671
const data = await schedule(parsedOptions);
72+
5773
check(data);
74+
deepStrictEqual(requested(fetch), [url]);
5875
});
5976

6077
it('should work with a different fetch passed to optionsParser', async () => {
78+
intercept();
79+
6180
const parsedOptions = optionsParser({
6281
fetch: undiciFetch,
6382
});
6483

6584
const data = await schedule(parsedOptions);
85+
6686
check(data);
87+
agent.assertNoPendingInterceptors();
6788
});
6889
});
6990

@@ -74,12 +95,7 @@ describe('schedule', async () => {
7495
'https://raw.githubusercontent.com/nodejs/Release/master/schedule.json';
7596

7697
// Act
77-
const result = schedule(
78-
/** @type {ParsedOptions} */ ({
79-
fetch: notFound,
80-
urls: { schedule: url },
81-
}),
82-
);
98+
const result = schedule({ fetch: notFound, urls: { schedule: url } });
8399

84100
// Assert
85101
await rejects(result, {
@@ -93,3 +109,17 @@ describe('schedule', async () => {
93109
/** @type {typeof fetch} */
94110
const notFound = async () =>
95111
new Response('Not Found', { status: 404, statusText: 'Not Found' });
112+
113+
// the URLs a mocked `fetch` was called with
114+
/** @param {import('node:test').Mock<typeof globalThis.fetch>} mock */
115+
function requested(mock) {
116+
return mock.mock.calls.map((call) => call.arguments[0]);
117+
}
118+
119+
// serves the schedule.json snapshot to undici's fetch once
120+
function intercept() {
121+
agent
122+
.get('https://raw.githubusercontent.com')
123+
.intercept({ path: '/nodejs/Release/master/schedule.json' })
124+
.reply(200, scheduleSnapshot);
125+
}

‎core/types/index.d.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
export = core;
2+
export type Fetch = import('@nodevu/fetchindex').Fetch;
23
export type Options = {
34
/**
45
* The `fetch` implementation to request the
56
* data with. Defaults to `globalThis.fetch`.
67
*/
7-
fetch?: typeof fetch;
8+
fetch?: Fetch;
89
/**
910
* An ISO 8601 date to work
1011
* out each release line's phase against. Defaults to now.
@@ -119,9 +120,14 @@ export type ReleaseLine = {
119120
security: Security;
120121
};
121122
export type Data = Record<string, ReleaseLine>;
123+
/**
124+
* The part of `fetch` core uses, which `@nodevu/fetchindex` defines.
125+
*
126+
* @typedef {import('@nodevu/fetchindex').Fetch} Fetch
127+
*/
122128
/**
123129
* @typedef {object} Options
124-
* @property {typeof fetch} [fetch] The `fetch` implementation to request the
130+
* @property {Fetch} [fetch] The `fetch` implementation to request the
125131
* data with. Defaults to `globalThis.fetch`.
126132
* @property {string | import('luxon').DateTime} [now] An ISO 8601 date to work
127133
* out each release line's phase against. Defaults to now.

‎core/types/util/prod/optionsParser.d.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export = parseOptions;
22
import { DateTime } from 'luxon';
33
export type ParsedOptions = {
4-
fetch: typeof fetch;
4+
fetch: import('../../index').Fetch;
55
now: string | DateTime;
66
urls: {
77
index: string;
@@ -10,7 +10,7 @@ export type ParsedOptions = {
1010
};
1111
/**
1212
* @typedef {object} ParsedOptions
13-
* @property {typeof fetch} fetch
13+
* @property {import('../../index').Fetch} fetch
1414
* @property {string | DateTime} now
1515
* @property {{ index: string, schedule: string }} urls
1616
*/

‎core/types/util/prod/schedule.d.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ export type Schedule = Record<string, {
2020
codename?: string;
2121
}>;
2222
/**
23-
* @param {import('./optionsParser').ParsedOptions} options
23+
* @param {{ fetch: import('../../index').Fetch, urls: { schedule: string } }} options
2424
* @returns {Promise<Schedule>}
2525
*/
26-
declare function schedule(options: import('./optionsParser').ParsedOptions): Promise<Schedule>;
26+
declare function schedule(options: {
27+
fetch: import('../../index').Fetch;
28+
urls: {
29+
schedule: string;
30+
};
31+
}): Promise<Schedule>;

‎core/util/prod/optionsParser.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const { DateTime } = require('luxon');
22

33
/**
44
* @typedef {object} ParsedOptions
5-
* @property {typeof fetch} fetch
5+
* @property {import('../../index').Fetch} fetch
66
* @property {string | DateTime} now
77
* @property {{ index: string, schedule: string }} urls
88
*/

‎core/util/prod/schedule.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*/
1313

1414
/**
15-
* @param {import('./optionsParser').ParsedOptions} options
15+
* @param {{ fetch: import('../../index').Fetch, urls: { schedule: string } }} options
1616
* @returns {Promise<Schedule>}
1717
*/
1818
async function schedule(options) {
@@ -27,7 +27,8 @@ async function schedule(options) {
2727
throw new Error(`Fetching ${url} failed: ${raw.status} ${raw.statusText}`);
2828
}
2929

30-
const schedule = await raw.json();
30+
// schedule.json is keyed by release line
31+
const schedule = /** @type {Schedule} */ (await raw.json());
3132

3233
return schedule;
3334
}

0 commit comments

Comments
 (0)