Repository navigation
Expand file tree
/
Copy pathfake-github.ts
More file actions
118 lines (110 loc) · 3.11 KB
/
Copy pathfake-github.ts
File metadata and controls
118 lines (110 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* A stand-in for GitHub's GraphQL API in the sponsors tests: an
* `HttpClient` that answers each request with `respond`, and records what
* was sent.
*/
import { Effect, Layer, Predicate } from 'effect';
import { HttpClient, HttpClientResponse } from 'effect/http';
export interface SentQuery {
readonly authorization: string | undefined;
readonly query: string;
readonly url: string;
readonly variables: {
readonly cursor: string | null;
readonly login: string;
};
}
export interface FakeReply {
readonly body: unknown;
readonly status?: number;
}
/** A page of a connection, as `connectionQuery`'s queries get it. */
export function connectionPage(
nodes: ReadonlyArray<unknown>,
next: string | null = null,
) {
return {
data: {
account: {
connection: {
nodes,
pageInfo: { endCursor: next, hasNextPage: next !== null },
},
},
},
};
}
/** A sponsorship node, as GitHub sends it to the account's owner. */
export function sponsorshipNode(
login: string,
overrides: Record<string, unknown> = {},
) {
return {
createdAt: '2025-01-01T00:00:00Z',
privacyLevel: 'PUBLIC',
sponsorEntity: {
avatarUrl: `https://avatars.githubusercontent.com/${login}?v=4`,
login,
name: login.toUpperCase(),
websiteUrl: null,
},
tier: { isOneTime: false, monthlyPriceInDollars: 10 },
...overrides,
};
}
/** A sponsors activity log entry, as GitHub sends it to the account's owner. */
export function activityNode(
login: string,
action: string,
opts: {
readonly dollars?: number;
readonly isOneTime?: boolean;
readonly timestamp?: string;
} = {},
) {
return {
action,
sponsor: { login },
sponsorsTier:
opts.dollars === undefined
? null
: {
isOneTime: opts.isOneTime ?? false,
monthlyPriceInDollars: opts.dollars,
},
timestamp: opts.timestamp ?? '2025-01-01T00:00:00Z',
};
}
/** An activity that has `login` pay exactly `cents`, once. */
export const paymentNode = (login: string, cents: number) =>
activityNode(login, 'NEW_SPONSORSHIP', {
dollars: cents / 100,
isOneTime: true,
});
export const isPaymentsQuery = (sent: SentQuery) =>
sent.query.includes('sponsorsActivities');
export function fakeGithub(respond: (sent: SentQuery) => FakeReply) {
const sent: Array<SentQuery> = [];
const client = HttpClient.make((request) =>
Effect.sync(() => {
const bytes = Predicate.isTagged(request.body, 'Uint8Array')
? request.body.body
: new Uint8Array();
const query: SentQuery = {
authorization: request.headers['authorization'],
url: request.url,
...JSON.parse(new TextDecoder().decode(bytes)),
};
sent.push(query);
const reply = respond(query);
return HttpClientResponse.fromWeb(
request,
new Response(JSON.stringify(reply.body), {
headers: { 'content-type': 'application/json' },
status: reply.status ?? 200,
}),
);
}),
);
return { client, layer: Layer.succeed(HttpClient.HttpClient, client), sent };
}