Skip to content

Commit 0566da3

Browse files
committed
Add tests for github
1 parent cb02dd8 commit 0566da3

File tree

4 files changed

+103
-33
lines changed

4 files changed

+103
-33
lines changed

src/index.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
} from 'cloudflare:test';
55
import { describe, it, expect } from 'vitest';
66
import { Env } from '../worker-configuration';
7-
// Could import any other source file/function here
87
import worker from '../src';
98

109
// For now, you'll need to do something like this to get a correctly-typed

src/legacy/handler.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,16 @@ import { TauriUpdateResponse } from '../types';
55
import { sanitizeVersion, semverGt, semverValid } from '../utils/versioning';
66

77
import { Env } from '../../worker-configuration';
8-
import { Request, ExecutionContext } from '@cloudflare/workers-types';
8+
import { Request } from '@cloudflare/workers-types';
99

1010
export async function handleLegacyRequest(
1111
request: Request,
12-
env: Env,
13-
ctx: ExecutionContext
12+
env: Env
1413
): Promise<Response> {
1514
const path = new URL(request.url).pathname;
1615
const [platform, version] = path.slice(1).split('/');
1716

18-
const releases = await getReleases(request, env, ctx);
17+
const releases = await getReleases(request, env);
1918

2019
const release = (await releases.clone().json()) as {
2120
tag_name: string;

src/services/github.test.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { describe, it, expect } from 'vitest';
2+
import {
3+
getReleases,
4+
getLatestRelease,
5+
findAssetSignature,
6+
Asset
7+
} from './github';
8+
import { Env } from '../../worker-configuration';
9+
10+
const IncomingRequest = Request<unknown, IncomingRequestCfProperties>;
11+
12+
// Mock the Env object
13+
const env: Env = {
14+
GITHUB_ACCOUNT: 'killeencode',
15+
GITHUB_REPO: 'brancato'
16+
};
17+
18+
describe('github', () => {
19+
it('getReleases', async () => {
20+
const request = new IncomingRequest('http://example.com');
21+
const response = await getReleases(request, env);
22+
expect(response).toBeInstanceOf(Response);
23+
expect(response.status).toBe(200);
24+
});
25+
26+
it('getLatestRelease', async () => {
27+
const request = new IncomingRequest('http://example.com');
28+
const release = await getLatestRelease(request, env);
29+
expect(release).toEqual(
30+
expect.objectContaining({
31+
tag_name: expect.any(String),
32+
assets: expect.any(Array),
33+
body: expect.any(String),
34+
published_at: expect.any(String)
35+
})
36+
);
37+
});
38+
39+
it('findAssetSignature', async () => {
40+
const assets: Asset[] = [
41+
{ name: 'test.sig', browser_download_url: 'http://example.com' },
42+
{ name: 'lol.sig', browser_download_url: 'http://example.com/lol' },
43+
{
44+
name: 'test.zip',
45+
browser_download_url: 'http://example.com/test'
46+
}
47+
];
48+
const signature = await findAssetSignature('test', assets);
49+
expect(signature).toContain('Example Domain');
50+
});
51+
});

src/services/github.ts

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,68 @@
11
import { Env } from '../../worker-configuration';
22
import { Request, Response } from '@cloudflare/workers-types';
33

4-
export type Asset = { name: string; browser_download_url: string };
4+
export type Asset = {
5+
name: string;
6+
browser_download_url: string;
7+
};
58

6-
export const getReleases = async (
9+
type Release = {
10+
tag_name: string;
11+
assets: Asset[];
12+
body: string;
13+
published_at: string;
14+
};
15+
16+
/**
17+
* Get the latest release from the GitHub API
18+
* @param request The Worker Request object from the fetch event
19+
* @param env The Worker environment object
20+
* @returns Response object from the GitHub API
21+
*/
22+
export async function getReleases(
723
request: Request,
824
env: Env
9-
): Promise<Response> => {
10-
const reqUrl = new URL(
11-
`https://api.github.com/repos/${env.GITHUB_ACCOUNT}/${env.GITHUB_REPO}/releases/latest`
12-
);
25+
): Promise<Response> {
26+
// build the request headers conditionally
1327
const headers = new Headers({
1428
Accept: 'application/vnd.github.preview',
1529
'User-Agent': request.headers.get('User-Agent') as string
1630
});
1731

18-
if (env.GITHUB_API_TOKEN?.length)
32+
if (env.GITHUB_API_TOKEN?.length) {
1933
headers.set('Authorization', `token ${env.GITHUB_API_TOKEN}`);
34+
}
2035

21-
const response = await fetch(reqUrl.toString(), {
22-
method: 'GET',
23-
headers
24-
});
25-
26-
return response;
27-
};
28-
29-
type Release = {
30-
tag_name: string;
31-
assets: Asset[];
32-
body: string;
33-
published_at: string;
34-
};
36+
// @ts-expect-error - Fetch does not have the webSocket property
37+
return await fetch(
38+
`https://api.github.com/repos/${env.GITHUB_ACCOUNT}/${env.GITHUB_REPO}/releases/latest`,
39+
{
40+
method: 'GET',
41+
headers
42+
}
43+
);
44+
}
3545

36-
export const getLatestRelease = async (
46+
/**
47+
* Get the latest release from the GitHub API as a Release object
48+
* @param request The Worker Request object from the fetch event
49+
* @param env The Worker environment object
50+
* @returns The latest release as a Release object
51+
*/
52+
export async function getLatestRelease(
3753
request: Request,
3854
env: Env
39-
): Promise<Release> => {
40-
const releases = await getReleases(request, env);
41-
55+
): Promise<Release> {
56+
const releases: Response = await getReleases(request, env);
4257
return (await releases.json()) as Release;
43-
};
58+
}
4459

60+
/**
61+
* Find the signature file for a given asset
62+
* @param fileName The name of the file to find the signature for
63+
* @param assets The assets to search for the signature
64+
* @returns The signature as a string or undefined if not found
65+
*/
4566
export async function findAssetSignature(
4667
fileName: string,
4768
assets: Asset[]
@@ -61,6 +82,6 @@ export async function findAssetSignature(
6182
if (response.status !== 200) {
6283
return undefined;
6384
}
64-
const signature = await response.text();
65-
return signature;
85+
86+
return await response.text();
6687
}

0 commit comments

Comments
 (0)