diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..e1a5dfb --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,32 @@ +name: test + +on: + push: + branches: + - main + +jobs: + test: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-node@v4 + with: + node-version: 20 + + - name: Install Dependencies + run: npm ci + + - name: Build Registry + run: npm run build + + - name: Run Test + env: + R2_HOSTNAME: ${{ secrets.TEST_R2_HOSTNAME }} + S3_ACCESS_KEY_ID: ${{ secrets.TEST_S3_ACCESS_KEY_ID }} + S3_BUCKET: ${{ secrets.TEST_S3_BUCKET }} + S3_ENDPOINT: ${{ secrets.S3_ENDPOINT }} + S3_SECRET_ACCESS_KEY: ${{ secrets.TEST_S3_SECRET_ACCESS_KEY }} + run: npm test diff --git a/package.json b/package.json index 5b2db4a..be573d3 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,10 @@ "scripts": { "dev": "astro dev", "start": "node build/index.cjs", - "build": "astro build && esbuild registry/index.ts --bundle --format=cjs --loader:.html=text --loader:.svg=text --external:esbuild --platform=node --outfile=build/index.cjs" + "build": "astro build && esbuild registry/index.ts --bundle --format=cjs --loader:.html=text --loader:.svg=text --external:esbuild --platform=node --outfile=build/index.cjs", + "pretest": "node build/index.cjs &", + "test": "node --test", + "posttest": "kill $(lsof -t -i:3000)" }, "dependencies": { "@aws-sdk/client-s3": "^3.540.0", diff --git a/test/registry.test.js b/test/registry.test.js new file mode 100644 index 0000000..2cbf3c0 --- /dev/null +++ b/test/registry.test.js @@ -0,0 +1,67 @@ +import assert from 'node:assert' +import test from 'node:test' + +async function fetchStr(url) { + const res = await fetch(url) + + if (!res.ok) { + await res.body.cancel() + + return null + } + + return await res.text() +} + +async function fetchJson(url) { + const res = await fetch(url) + + if (!res.ok) { + await res.body.cancel() + + return null + } + + return await res.json() +} + +test('latest tag', async () => { + const latestTag = (await fetchJson('https://api.github.com/repos/esbuild/deno-esbuild/tags'))[0].name + + const res = await fetch('http://localhost:3000/esbuild/deno-esbuild/mod.js', { + redirect: 'manual' + }) + + await res.body.cancel() + + assert.strictEqual(res.headers.get('location', `https://deno.re/esbuild/deno-esbuild@${latestTag}/mod.js`), expected) +}) + +test('specific commit', async () => { + const actual = await fetchStr('http://localhost:3000/esbuild/deno-esbuild@d4ebc0f33d39d5b8cc94eec4b1a94ce5b388cd6f/mod.js') + const expected = await fetchStr('https://raw.githubusercontent.com/esbuild/deno-esbuild/d4ebc0f33d39d5b8cc94eec4b1a94ce5b388cd6f/mod.js') + + assert.strictEqual(actual, expected) +}) + +test('specific tag', async () => { + const actual = await fetchStr('http://localhost:3000/esbuild/deno-esbuild@v0.19.0/mod.js') + const expected = await fetchStr('https://raw.githubusercontent.com/esbuild/deno-esbuild/v0.19.0/mod.js') + + assert.strictEqual(actual, expected) +}) + +test('omit entry point', async () => { + const actual = await fetchStr('http://localhost:3000/esbuild/deno-esbuild@v0.20.0') + const expected = await fetchStr('https://raw.githubusercontent.com/esbuild/deno-esbuild/v0.20.0/mod.js') + + assert.strictEqual(actual, expected) +}) + +test('type header', async () => { + const res = await fetch('http://localhost:3000/esbuild/deno-esbuild@v0.20.0/mod.js') + + await res.body.cancel() + + assert.strictEqual(res.headers.get('x-typescript-types'), `https://deno.re/esbuild/deno-esbuild@v0.20.0/mod.d.ts`) +})