Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

gitHubMediaFetch.test.ts

104 lines3.6 KB

1import { describe, expect, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import * as Redacted from "effect/Redacted";
5import { HttpClient, HttpClientResponse } from "effect/http";
6​
7import * as GitHubCredentials from "./GitHubCredentials.ts";
8import * as GitHubMediaFetch from "./gitHubMediaFetch.ts";
9​
10describe("GitHubMediaFetch", () => {
11 it.effect("loads private media immediately after login with the GitHub credential", () => {
12 let lookups = 0;
13 const authorizations: Array<string | undefined> = [];
14 return Effect.gen(function* () {
15 const fetchMedia = yield* GitHubMediaFetch.make;
16 const request = {
17 url: "https://raw.githubusercontent.com/owner/repo/main/shot.png",
18 headers: {},
19 };
20 expect((yield* fetchMedia(request))?.status).toBe(404);
21 expect((yield* fetchMedia(request))?.status).toBe(200);
22 expect((yield* fetchMedia(request))?.status).toBe(200);
23 expect(lookups).toBe(3);
24 expect(authorizations).toEqual([undefined, "Bearer signed-in", "Bearer signed-in"]);
25 }).pipe(
26 Effect.provide(
27 Layer.mock(GitHubCredentials.GitHubCredentials)({
28 get: (host) =>
29 ++lookups === 1
30 ? Effect.fail(new GitHubCredentials.GitHubNotSignedInError({ host }))
31 : Effect.succeed({
32 host,
33 token: Redacted.make("signed-in"),
34 source: "gh" as const,
35 fingerprint: "fingerprint",
36 }),
37 }),
38 ),
39 Effect.provideService(
40 HttpClient.HttpClient,
41 HttpClient.make((request) => {
42 authorizations.push(request.headers.authorization);
43 return Effect.succeed(
44 HttpClientResponse.fromWeb(
45 request,
46 new Response(null, {
47 status: request.headers.authorization ? 200 : 404,
48 headers: { "content-type": "image/png" },
49 }),
50 ),
51 );
52 }),
53 ),
54 Effect.scoped,
55 );
56 });
57​
58 it.effect("keeps the credential off the object store a redirect hands the media to", () => {
59 const authorizations: Array<[string, string | undefined]> = [];
60 return Effect.gen(function* () {
61 const fetchMedia = yield* GitHubMediaFetch.make;
62 const response = yield* fetchMedia({
63 url: "https://github.com/user-attachments/assets/1a1842fb",
64 headers: { range: "bytes=0-1" },
65 });
66 expect(response?.status).toBe(200);
67 expect(authorizations).toEqual([
68 ["github.com", "Bearer signed-in"],
69 ["objects.example.com", undefined],
70 ]);
71 }).pipe(
72 Effect.provide(
73 Layer.mock(GitHubCredentials.GitHubCredentials)({
74 get: (host) =>
75 Effect.succeed({
76 host,
77 token: Redacted.make("signed-in"),
78 source: "gh" as const,
79 fingerprint: "fingerprint",
80 }),
81 }),
82 ),
83 Effect.provideService(
84 HttpClient.HttpClient,
85 HttpClient.make((request) => {
86 const url = new URL(request.url);
87 authorizations.push([url.hostname, request.headers.authorization]);
88 return Effect.succeed(
89 HttpClientResponse.fromWeb(
90 request,
91 url.hostname === "github.com"
92 ? new Response(null, {
93 status: 302,
94 headers: { location: "https://objects.example.com/signed" },
95 })
96 : new Response(null, { status: 200 }),
97 ),
98 );
99 }),
100 ),
101 Effect.scoped,
102 );
103 });
104});