Skip to content

Commit

Permalink
test: cloudflare-workers
Browse files Browse the repository at this point in the history
  • Loading branch information
shaokeyibb committed Aug 23, 2024
1 parent 48ef488 commit 9d805c2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 17 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/check-format-and-test-cloudflare-workers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,27 @@ jobs:
name: Check Format
with:
cmd: prettier --check .
dir: cloudflare-workers

test:
name: Test PR
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./cloudflare-workers
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
name: Set Node.js 20.x
with:
node-version: 20.x
- uses: borales/actions-yarn@v5
name: Run install
with:
cmd: install
dir: cloudflare-workers
- uses: borales/actions-yarn@v5
name: Run Tests
with:
cmd: test
dir: cloudflare-workers
44 changes: 27 additions & 17 deletions cloudflare-workers/test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
// test/index.spec.ts
import { env, createExecutionContext, waitOnExecutionContext, SELF } from 'cloudflare:test';
import { describe, it, expect } from 'vitest';
import worker from '../src/index';
import { calcOffsetModification } from '../src/utils';
import { ModifiedCommentBody, Offset } from '../src/types';

// For now, you'll need to do something like this to get a correctly-typed
// `Request` to pass to `worker.fetch()`.
const IncomingRequest = Request<unknown, IncomingRequestCfProperties>;
describe('Commit offset patch', () => {
// https://github.com/OI-wiki/OI-wiki/blob/master/docs/index.md
it('OI-Wiki index unit test', async () => {
const offsets: Offset[] = [
{ start: 372, end: 439 },
{ start: 441, end: 586 },
{ start: 588, end: 744 },
{ start: 746, end: 810 },
];
const diff: ModifiedCommentBody['diff'] = [
{ tag: 'delete', i1: 20, i2: 298, j1: 20, j2: 20 },
{ tag: 'replace', i1: 420, i2: 423, j1: 142, j2: 145 },
{ tag: 'insert', i1: 538, i2: 538, j1: 260, j2: 265 },
{ tag: 'insert', i1: 586, i2: 586, j1: 313, j2: 321 },
{ tag: 'delete', i1: 696, i2: 712, j1: 431, j2: 431 },
{ tag: 'replace', i1: 752, i2: 755, j1: 471, j2: 473 },
{ tag: 'replace', i1: 770, i2: 773, j1: 488, j2: 490 },
];

describe('Hello World worker', () => {
it('responds with Hello World! (unit style)', async () => {
const request = new IncomingRequest('http://example.com');
// Create an empty context to pass to `worker.fetch()`.
const ctx = createExecutionContext();
const response = await worker.fetch(request, env, ctx);
// Wait for all `Promise`s passed to `ctx.waitUntil()` to settle before running test assertions
await waitOnExecutionContext(ctx);
expect(await response.text()).toMatchInlineSnapshot(`"Hello World!"`);
});
const res = calcOffsetModification(offsets, diff);

it('responds with Hello World! (integration style)', async () => {
const response = await SELF.fetch('https://example.com');
expect(await response.text()).toMatchInlineSnapshot(`"Hello World!"`);
expect(res).toEqual([
{ from: { start: 372, end: 439 }, to: { start: 94, end: 161 } },
{ from: { start: 441, end: 586 }, to: { start: 163, end: 313 } },
{ from: { start: 588, end: 744 }, to: { start: 323, end: 463 } },
{ from: { start: 746, end: 810 }, to: { start: 465, end: 527 } },
]);
});
});

0 comments on commit 9d805c2

Please sign in to comment.