Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests, scripts: cleanup tests and dev environment things #252

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
6 changes: 6 additions & 0 deletions dev-bucket/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# dev-bucket

This is a very slimmed down recreation of the `dist-prod` bucket meant for testing the Release Worker.
The files here have little to no content, they're here just to build the structure of the bucket itself so that the Release Worker can be used in development easier.

For more information on the structure of the bucket, read [TODO]().
9 changes: 9 additions & 0 deletions dev-bucket/metrics/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<p>metrics/index.html</p>
</body>
</html>
Empty file.
1 change: 1 addition & 0 deletions dev-bucket/nodejs/chakracore-nightly/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<p>nodejs/chakracore-nightly/v10.13.0-nightly2018112084bd6f3c82/docs/api/index.html</p>
</body>
</html>
1 change: 1 addition & 0 deletions dev-bucket/nodejs/chakracore-rc/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Empty file.
Empty file.
Empty file.
Empty file.
1 change: 1 addition & 0 deletions dev-bucket/nodejs/chakracore-release/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<p>nodejs/chakracore-release/v10.0.0/docs/api/index.html</p>
</body>
</html>
Empty file.
Empty file.
Empty file.
9 changes: 9 additions & 0 deletions dev-bucket/nodejs/docs/v0.0.1/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<p>nodejs/docs/v0.0.1/index.html</p>
</body>
</html>
1 change: 1 addition & 0 deletions dev-bucket/nodejs/nightly/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<p>nodejs/nightly/v24.0.0-nightly20241219756077867b/docs/api/index.html</p>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions dev-bucket/nodejs/rc/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Empty file.
9 changes: 9 additions & 0 deletions dev-bucket/nodejs/rc/v23.0.0-rc.3/docs/api/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<p>nodejs/rc/v23.0.0-rc.3/docs/api/index.html</p>
</body>
</html>
1 change: 1 addition & 0 deletions dev-bucket/nodejs/rc/v23.0.0-rc.3/docs/apilinks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Empty file.
Empty file.
1 change: 1 addition & 0 deletions dev-bucket/nodejs/release/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Empty file.
9 changes: 9 additions & 0 deletions dev-bucket/nodejs/release/v20.0.0/docs/api/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<p>nodejs/release/v20.0.0/docs/api/index.html</p>
</body>
</html>
1 change: 1 addition & 0 deletions dev-bucket/nodejs/release/v20.0.0/docs/apilinks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Empty file.
Empty file.
Empty file.
1 change: 1 addition & 0 deletions dev-bucket/nodejs/test/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Empty file.
Empty file.
Empty file.
1 change: 1 addition & 0 deletions dev-bucket/nodejs/v8-canary/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<p>nodejs/v8-canary/v24.0.0-v8-canary202412221f947c1730/docs/api/index.html</p>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
"type": "module",
"scripts": {
"start": "wrangler dev --remote",
"dev": "wrangler deploy --dry-run --outdir=dist && node --import=tsx scripts/dev-server.ts",
"format": "prettier --check --write \"**/*.{ts,js,mjs,json,md}\"",
"prettier": "prettier --check \"**/*.{ts,js,mjs,json,md}\"",
"lint": "eslint ./src",
"_test": "wrangler deploy --dry-run --outdir=dist && node --test --test-reporter=@reporters/github --test-reporter-destination=stdout --test-reporter=spec --test-reporter-destination=stdout --import=tsx",
"test": "npm run test:unit && npm run test:e2e",
"test:unit": "node --test --test-reporter=@reporters/github --test-reporter-destination=stdout --test-reporter=spec --test-reporter-destination=stdout --import=tsx ./tests/unit/index.test.ts",
"test:e2e": "wrangler deploy --dry-run --outdir=dist && node --test --test-reporter=@reporters/github --test-reporter-destination=stdout --test-reporter=spec --test-reporter-destination=stdout --import=tsx ./tests/e2e/index.test.ts",
Expand Down
73 changes: 73 additions & 0 deletions scripts/constants.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,76 @@ export const PROD_BUCKET = process.env.PROD_BUCKET ?? 'dist-prod';
export const STAGING_BUCKET = process.env.STAGING_BUCKET ?? 'dist-staging';

export const R2_RETRY_COUNT = 3;

/**
* @type {Record<string, string>}
*/
export const DEV_BUCKET = {
'metrics/index.html': '<p>metrics/index.html</p>',
'metrics/logs/nodejs.org-access.log.20241222.0000000000.csv': '',

'nodejs/chakracore-nightly/index.json': '{}',
'nodejs/chakracore-nightly/v10.13.0-nightly2018112084bd6f3c82/SHASUMS256.txt':
'',
'nodejs/chakracore-nightly/v10.13.0-nightly2018112084bd6f3c82/docs/api/index.html':
'<p>nodejs/chakracore-nightly/v10.13.0-nightly2018112084bd6f3c82/docs/api/index.html</p>\n',
'nodejs/chakracore-nightly/v10.13.0-nightly2018112084bd6f3c82/win-arm64/.gitkeep':
'',
'nodejs/chakracore-nightly/v10.13.0-nightly2018112084bd6f3c82/win-x64/.gitkeep':
'',
'nodejs/chakracore-nightly/v10.13.0-nightly2018112084bd6f3c82/win-x86/.gitkeep':
'',

'nodejs/chakracore-rc/index.json': '{}',
'nodejs/chakracore-rc/v10.0.0-rc.0/SHASUMS256.txt': '',
'nodejs/chakracore-rc/v10.0.0-rc.0/win-arm64/.gitkeep': '',
'nodejs/chakracore-rc/v10.0.0-rc.0/win-x64/.gitkeep': '',
'nodejs/chakracore-rc/v10.0.0-rc.0/win-x86/.gitkeep': '',

'nodejs/chakracore-release/index.json': '{}',
'nodejs/chakracore-release/v10.0.0/SHASUMS256.txt': '',
'nodejs/chakracore-release/v10.0.0/docs/api/index.html':
'<p>nodejs/chakracore-release/v10.0.0/docs/api/index.html</p>\n',
'nodejs/chakracore-release/v10.0.0/win-arm64/.gitkeep': '',
'nodejs/chakracore-release/v10.0.0/win-x64/.gitkeep': '',
'nodejs/chakracore-release/v10.0.0/win-x86/.gitkeep': '',

'nodejs/docs/v0.0.1/index.html': '<p>nodejs/docs/v0.0.1/index.html</p>',

'nodejs/nightly/index.json': '{}',
'nodejs/nightly/v24.0.0-nightly20241219756077867b/SHASUMS256.txt': '',
'nodejs/nightly/v24.0.0-nightly20241219756077867b/docs/api/index.html':
'<p>nodejs/nightly/v24.0.0-nightly20241219756077867b/docs/api/index.html</p>\n',
'nodejs/nightly/v24.0.0-nightly20241219756077867b/docs/apilinks.json': '{}',
'nodejs/nightly/v24.0.0-nightly20241219756077867b/win-arm64/.gitkeep': '',
'nodejs/nightly/v24.0.0-nightly20241219756077867b/win-x64/.gitkeep': '',

'nodejs/rc/index.json': '{}',
'nodejs/rc/v23.0.0-rc.3/SHASUMS256.txt': '',
'nodejs/rc/v23.0.0-rc.3/docs/api/index.html':
'<p>nodejs/rc/v23.0.0-rc.3/docs/api/index.html</p>\n',
'nodejs/rc/v23.0.0-rc.3/docs/apilinks.json': '{}',
'nodejs/rc/v23.0.0-rc.3/win-arm64/.gitkeep': '',
'nodejs/rc/v23.0.0-rc.3/win-x64/.gitkeep': '',

'nodejs/release/index.json': '{}',
'nodejs/release/v20.0.0/SHASUMS256.txt': '',
'nodejs/release/v20.0.0/docs/api/index.html':
'<p>nodejs/release/v20.0.0/docs/api/index.html</p>\n',
'nodejs/release/v20.0.0/docs/apilinks.json': '{}',
'nodejs/release/v20.0.0/win-arm64/.gitkeep': '',
'nodejs/release/v20.0.0/win-x64/.gitkeep': '',
'nodejs/release/v20.0.0/win-x86/.gitkeep': '',

'nodejs/test/index.json': '{}',
'nodejs/test/v24.0.0-test6af5c4e2b40/SHASUMS256.txt': '',
'nodejs/test/v24.0.0-test6af5c4e2b40/win-arm64/.gitkeep': '',
'nodejs/test/v24.0.0-test6af5c4e2b40/win-x64/.gitkeep': '',

'nodejs/v8-canary/index.json': '{}',
'nodejs/v8-canary/v24.0.0-v8-canary202412221f947c1730/SHASUMS256.txt': '',
'nodejs/v8-canary/v24.0.0-v8-canary202412221f947c1730/docs/api/index.html':
'<p>nodejs/v8-canary/v24.0.0-v8-canary202412221f947c1730/docs/api/index.html</p>\n',
'nodejs/v8-canary/v24.0.0-v8-canary202412221f947c1730/docs/apilinks.json':
'{}',
};
40 changes: 40 additions & 0 deletions scripts/dev-server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Starts the dev server with the R2 bucket populated with the contents of the
// `dev-bucket` directory

// TODO this is still annoying since autoreload doesn't work

import { setupMiniflare } from '../src/e2e-tests/util';
import type { Env } from '../src/env';

const [, url] = await setupMiniflare(getEnvironment());

console.log(`Listening on ${url}`);

function getEnvironment() {
const environment: Partial<Env> = {};

const environmentVariables: Array<keyof Env> = [
'ENVIRONMENT',
'LOG_ERRORS',
'S3_ENDPOINT',
'S3_ACCESS_KEY_ID',
'S3_ACCESS_KEY_SECRET',
'BUCKET_NAME',
'SENTRY_DSN',
'ORIGIN_HOST',
];

for (const variable of environmentVariables) {
if (variable in process.env) {
// @ts-expect-error todo
environment[variable as keyof Env] = process.env[variable]!;
}
}

if (environment.LOG_ERRORS) {
// Parse string value to boolean
environment.LOG_ERRORS = JSON.parse(environment.LOG_ERRORS);
}

return environment;
}
3 changes: 3 additions & 0 deletions src/e2e-tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# e2e-tests

TODO
12 changes: 12 additions & 0 deletions src/e2e-tests/directory.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// import { SELF, env } from 'cloudflare:test';
// import { beforeAll, test, expect } from 'vitest';
// import { populateR2Bucket } from './util';

// beforeAll(async () => {
// // @ts-expect-error
// await populateR2Bucket(env.R2_BUCKET);
// });

// test('something', async () => {
// await SELF.fetch('https://localhost/123');
// });
130 changes: 130 additions & 0 deletions src/e2e-tests/file.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import assert from 'node:assert';
import test, { after, before, describe } from 'node:test';
import { type Miniflare, type DispatchFetch } from 'miniflare';
import { setupMiniflare } from './util';
import { CACHE_HEADERS } from '../constants/cache';

describe('file tests', async () => {
let mf: Miniflare;
let url: URL;

before(async () => {
[mf, url] = await setupMiniflare();
});

const sendRequest: DispatchFetch = (path, init) => {
if (typeof path === 'string' && path.startsWith('/')) {
path = path.substring(1);
}

return mf.dispatchFetch(`${url}${path}`, init);
};

// TODO go through each file and make sure it works

// test('GET `/dist/index.json`', async () => {
// const res = await sendRequest('/dist/index.json')

// assert.strictEqual(res.status, 200)
// assert.strictEqual(res.headers.get('content-type'), 'application/json')
// assert.strictEqual(res.headers.get('cache-control'), CACHE_HEADERS.success)
// assert.ok(res.headers.has('etag'))
// assert.ok(res.headers.has('last-modified'))

// const body = await res.text();
// // assert.strictEqual(body, )
// })

test('returns 404 for missing file', async () => {
const res = await sendRequest('/dist/asd123.json');

assert.equal(res.status, 404);
assert.strictEqual(res.headers.get('cache-control'), CACHE_HEADERS.failure);

const body = await res.text();
assert.strictEqual(body, 'File not found');
});

test('if-modified-since', async () => {
let lastModified: string;

// Make first request to grab its last modified date
{
const res = await sendRequest('/dist/index.json');

assert.strictEqual(res.status, 200);
assert.ok(res.headers.has('last-modified'));

lastModified = res.headers.get('last-modified')!;
}

// Make sure it returns a 304 when if-modified-since >= the file's last
// modified
{
const res = await sendRequest('/dist/index.json', {
headers: {
'if-modified-since': lastModified,
},
});

assert.equal(res.status, 304);
}

// Now let's send a request to before the file's last modified date.
// This should give us a 200 since the file has changed.
{
const res = await sendRequest('/dist/index.json', {
headers: {
'if-modified-since': new Date(0).toUTCString(),
},
});

assert.equal(res.status, 200);
}
});

test('if-unmodified-since', async () => {
let lastModified: string;

// Send request to get the file's last modified date
{
const res = await sendRequest('/dist/index.json');

assert.equal(res.status, 200);
assert.ok(res.headers.has('last-modified'));

lastModified = res.headers.get('last-modified')!;
}

// Send request before the last modified. This should return a 412.
{
const res = await sendRequest('/dist/index.json', {
headers: {
'if-unmodified-since': new Date(0).toUTCString(),
},
});

assert.equal(res.status, 412);
}

// Send request after the last modified. This should return a 200
{
const timestamp = new Date(lastModified);
timestamp.setMinutes(timestamp.getMinutes() + 1);

const res = await sendRequest('/dist/index.json', {
headers: {
'if-unmodified-since': timestamp.toUTCString(),
},
});

assert.equal(res.status, 412);
}
});

test('if-match', async () => {});

after(async () => {
await mf.dispose();
});
});
6 changes: 6 additions & 0 deletions src/e2e-tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"types": ["@cloudflare/workers-types", "@types/node"]
}
}
Loading
Loading