@shivaedev/test-kits 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/README.md +22 -0
- package/dist/github/adapters/appProvider.d.ts +31 -0
- package/dist/github/adapters/appProvider.d.ts.map +1 -0
- package/dist/github/adapters/appProvider.js +31 -0
- package/dist/github/adapters/appProvider.js.map +1 -0
- package/dist/github/adapters/credentials.d.ts +21 -0
- package/dist/github/adapters/credentials.d.ts.map +1 -0
- package/dist/github/adapters/credentials.js +48 -0
- package/dist/github/adapters/credentials.js.map +1 -0
- package/dist/github/adapters/webhookDelivery.d.ts +7 -0
- package/dist/github/adapters/webhookDelivery.d.ts.map +1 -0
- package/dist/github/adapters/webhookDelivery.js +15 -0
- package/dist/github/adapters/webhookDelivery.js.map +1 -0
- package/dist/github/fake.d.ts +52 -0
- package/dist/github/fake.d.ts.map +1 -0
- package/dist/github/fake.js +18 -0
- package/dist/github/fake.js.map +1 -0
- package/package.json +10 -9
- package/src/github/adapters/appProvider.ts +34 -0
- package/src/github/adapters/credentials.ts +62 -0
- package/src/github/adapters/webhookDelivery.ts +20 -0
- package/src/github/fake.ts +30 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.5.0 - 2026-11-04
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
|
|
7
|
+
- Publish a scoped fake GitHub App with real HTTP installation-token exchanges, configurable REST and GraphQL routes, provider fault controls and signed webhook delivery for application stories.
|
|
8
|
+
|
|
3
9
|
## 0.4.0 - 2026-11-04
|
|
4
10
|
|
|
5
11
|
### Added
|
package/README.md
CHANGED
|
@@ -31,6 +31,28 @@ Import `signedInUser`, `signInAgain`, `passwordUser` and `storedProviderAccounts
|
|
|
31
31
|
|
|
32
32
|
Import `fakeDiscord` from `@shivaedev/test-kits/discord/fake.ts` and acquire it in a scope. Its loopback `server` retains messages across creation, editing, history reads and deletion, and supports the standard fake HTTP fault controls. Use the token `story-discord-token` with the real Discord REST client and set its API URL to `server.url`. Provide `gateway.transport` as the real gateway client's `GatewayTransport`; it speaks identify, heartbeat and resume frames, retains sessions and replays dispatches emitted during a dropped connection. `gateway.dispatch(type, data)`, `drop(code?)` and `invalidate(resumable)` change the provider, while `frames` and `urls` expose outbound observations. Drive reconnect and rate-limit time through the story kit's controlled clock.
|
|
33
33
|
|
|
34
|
+
### GitHub Apps
|
|
35
|
+
|
|
36
|
+
Import `fakeGitHubApp` from `@shivaedev/test-kits/github/fake.ts` and acquire it in a scope. The provider exchanges signed App JWTs for installation tokens over loopback HTTP. Give the real `@shivaedev/github` client its `credentials.appId`, redacted `credentials.privateKey`, an installation ID and `server.url` as `GitHubConfig.apiUrl`.
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
const github = yield* fakeGitHubApp({
|
|
40
|
+
routes: ({ installationToken }) => ({
|
|
41
|
+
repository: {
|
|
42
|
+
guard: installationToken,
|
|
43
|
+
path: "/repos/acme/widgets/issues/3",
|
|
44
|
+
reply: () => ({ body: { title: "Ship widgets" } }),
|
|
45
|
+
},
|
|
46
|
+
}),
|
|
47
|
+
});
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
`routes` uses test-story's named fake HTTP routes for REST reads and writes and GraphQL POSTs. `installationToken` admits issued tokens, `installationOf(request)` returns their installation ID, and `appRequest` checks an App JWT. Routes own their paths; `prefix` changes the token exchange path for Enterprise API bases. Supply `credentials` from `githubAppCredentials()` in `@shivaedev/test-kits/github/adapters/credentials.ts` when several fixtures need the same App key.
|
|
51
|
+
|
|
52
|
+
`server.requests` records method, path, headers and raw body; `server.scriptFault`, `hold` and the other fake HTTP controls drive rate limits and delayed or refused exchanges. Return rate-limit headers on routes or script a 403/429 reply with `retry-after` to exercise the real client's typed rate-limit handling. The kit does not simulate a GitHub database or interpret GraphQL queries: route replies describe the outside provider's data.
|
|
53
|
+
|
|
54
|
+
`deliverWebhook(url, { body, event, deliveryId })` sends the exact body bytes over HTTP with GitHub event, delivery and HMAC signature headers, consumes the receiver's response and returns its status. Configure the receiver with `credentials.webhookSecret`; `credentials.webhookSignature(body)` is available for signature-specific stories. Drive token expiry and rate-limit time through the story clock. No client implementation is replaced.
|
|
55
|
+
|
|
34
56
|
### MCP bearer tokens
|
|
35
57
|
|
|
36
58
|
Import `mcpBearerToken` from `@shivaedev/test-kits/mcp/adapters/bearerToken.ts`. With the application's mounted Better Auth instance (JWT and MCP provider plugins enabled), an existing signed-in user and OAuth client, mint the token precondition:
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import type { FakeHttpRequest } from "@shivaedev/test-story/server/fakeHttpService.ts";
|
|
3
|
+
import type { GitHubAppAccess } from "#github/fake.ts";
|
|
4
|
+
import type { AppJwt } from "./credentials.ts";
|
|
5
|
+
export declare const appProvider: (credentials: {
|
|
6
|
+
appId: string;
|
|
7
|
+
appJwtOf: (authorization: string | undefined, nowMillis: number) => AppJwt | undefined;
|
|
8
|
+
privateKey: string;
|
|
9
|
+
signedByApp: (jwt: AppJwt | undefined) => boolean;
|
|
10
|
+
tokenPrefix: string;
|
|
11
|
+
webhookSecret: string;
|
|
12
|
+
webhookSignature: (body: Uint8Array) => string;
|
|
13
|
+
}, prefix: string) => Effect.Effect<{
|
|
14
|
+
access: GitHubAppAccess;
|
|
15
|
+
state: {
|
|
16
|
+
exchanges: number;
|
|
17
|
+
jwts: (AppJwt | undefined)[];
|
|
18
|
+
};
|
|
19
|
+
token: {
|
|
20
|
+
guard: (request: FakeHttpRequest) => boolean;
|
|
21
|
+
method: string;
|
|
22
|
+
path: string;
|
|
23
|
+
reply: (request: FakeHttpRequest) => {
|
|
24
|
+
body: {
|
|
25
|
+
expires_at: string;
|
|
26
|
+
token: string;
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
}, never, never>;
|
|
31
|
+
//# sourceMappingURL=appProvider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"appProvider.d.ts","sourceRoot":"","sources":["../../../src/github/adapters/appProvider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,MAAM,EAAE,MAAM,QAAQ,CAAC;AACvC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,iDAAiD,CAAC;AACvF,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AACvD,OAAO,KAAK,EAAE,MAAM,EAAwB,MAAM,kBAAkB,CAAC;AAErE,eAAO,MAAM,WAAW;;;;;;;;;;;mBAEG,MAAM;cAAQ,CAAC,MAAM,GAAG,SAAS,CAAC,EAAE;;;QAe5D,KAAK;QACL,MAAM;QACN,IAAI;QACJ,KAAK,YAAY,eAAe;;;;;;;gBAQjC,CAAC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Clock, Effect } from "effect";
|
|
2
|
+
export const appProvider = Effect.fnUntraced(function* (credentials, prefix) {
|
|
3
|
+
const clock = yield* Clock.Clock;
|
|
4
|
+
const state = { exchanges: 0, jwts: [] };
|
|
5
|
+
const tokens = new Map();
|
|
6
|
+
const access = {
|
|
7
|
+
appRequest: (request) => {
|
|
8
|
+
const jwt = credentials.appJwtOf(request.headers.authorization, clock.currentTimeMillisUnsafe());
|
|
9
|
+
state.jwts.push(jwt);
|
|
10
|
+
return credentials.signedByApp(jwt);
|
|
11
|
+
},
|
|
12
|
+
installationOf: (request) => tokens.get(request.headers.authorization ?? ""),
|
|
13
|
+
installationToken: (request) => tokens.has(request.headers.authorization ?? ""),
|
|
14
|
+
};
|
|
15
|
+
return {
|
|
16
|
+
access,
|
|
17
|
+
state,
|
|
18
|
+
token: {
|
|
19
|
+
guard: access.appRequest,
|
|
20
|
+
method: "POST",
|
|
21
|
+
path: `${prefix}/app/installations/:installationId/access_tokens`,
|
|
22
|
+
reply: (request) => {
|
|
23
|
+
state.exchanges += 1;
|
|
24
|
+
const token = `${credentials.tokenPrefix}-${state.exchanges}`;
|
|
25
|
+
tokens.set(`Bearer ${token}`, Number(request.params.installationId));
|
|
26
|
+
return { body: { "expires_at": new Date(clock.currentTimeMillisUnsafe() + 3_600_000).toISOString(), token } };
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
});
|
|
31
|
+
//# sourceMappingURL=appProvider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"appProvider.js","sourceRoot":"","sources":["../../../src/github/adapters/appProvider.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAKvC,MAAM,CAAC,MAAM,WAAW,GAAG,MAAM,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE,WAAoD,EAAE,MAAc;IAC3H,MAAM,KAAK,GAAG,KAAK,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC;IACjC,MAAM,KAAK,GAAwD,EAAE,SAAS,EAAE,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC;IAC9F,MAAM,MAAM,GAAG,IAAI,GAAG,EAAkB,CAAC;IACzC,MAAM,MAAM,GAAoB;QAC/B,UAAU,EAAE,CAAC,OAAO,EAAE,EAAE;YACvB,MAAM,GAAG,GAAG,WAAW,CAAC,QAAQ,CAAC,OAAO,CAAC,OAAO,CAAC,aAAa,EAAE,KAAK,CAAC,uBAAuB,EAAE,CAAC,CAAC;YACjG,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;YACrB,OAAO,WAAW,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC;QACrC,CAAC;QACD,cAAc,EAAE,CAAC,OAAO,EAAE,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,aAAa,IAAI,EAAE,CAAC;QAC5E,iBAAiB,EAAE,CAAC,OAAO,EAAE,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,aAAa,IAAI,EAAE,CAAC;KAC/E,CAAC;IACF,OAAO;QACN,MAAM;QACN,KAAK;QACL,KAAK,EAAE;YACN,KAAK,EAAE,MAAM,CAAC,UAAU;YACxB,MAAM,EAAE,MAAM;YACd,IAAI,EAAE,GAAG,MAAM,kDAAkD;YACjE,KAAK,EAAE,CAAC,OAAwB,EAAE,EAAE;gBACnC,KAAK,CAAC,SAAS,IAAI,CAAC,CAAC;gBACrB,MAAM,KAAK,GAAG,GAAG,WAAW,CAAC,WAAW,IAAI,KAAK,CAAC,SAAS,EAAE,CAAC;gBAC9D,MAAM,CAAC,GAAG,CAAC,UAAU,KAAK,EAAE,EAAE,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC,CAAC;gBACrE,OAAO,EAAE,IAAI,EAAE,EAAE,YAAY,EAAE,IAAI,IAAI,CAAC,KAAK,CAAC,uBAAuB,EAAE,GAAG,SAAS,CAAC,CAAC,WAAW,EAAE,EAAE,KAAK,EAAE,EAAE,CAAC;YAC/G,CAAC;SACD;KACD,CAAC;AACH,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type KeyObject } from "node:crypto";
|
|
2
|
+
export declare function githubAppCredentials(): {
|
|
3
|
+
appId: string;
|
|
4
|
+
appJwtOf: (authorization: string | undefined, nowMillis: number) => AppJwt | undefined;
|
|
5
|
+
privateKey: string;
|
|
6
|
+
signedByApp: (jwt: AppJwt | undefined) => boolean;
|
|
7
|
+
tokenPrefix: string;
|
|
8
|
+
webhookSecret: string;
|
|
9
|
+
webhookSignature: (body: Uint8Array) => string;
|
|
10
|
+
};
|
|
11
|
+
export interface AppJwt {
|
|
12
|
+
readonly algorithm: string;
|
|
13
|
+
readonly expiresIn: number;
|
|
14
|
+
readonly issuedAgo: number;
|
|
15
|
+
readonly issuer: string;
|
|
16
|
+
readonly signedBy: "the App's key" | "another key";
|
|
17
|
+
readonly type: string;
|
|
18
|
+
}
|
|
19
|
+
export declare function appJwtOf(publicKey: KeyObject, authorization: string | undefined, nowMillis: number): AppJwt | undefined;
|
|
20
|
+
export declare function signedByApp(appId: string, jwt: AppJwt | undefined): boolean;
|
|
21
|
+
//# sourceMappingURL=credentials.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"credentials.d.ts","sourceRoot":"","sources":["../../../src/github/adapters/credentials.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmC,KAAK,SAAS,EAAU,MAAM,aAAa,CAAC;AAGtF,wBAAgB,oBAAoB;;8BAMR,MAAM,GAAG,SAAS,aAAa,MAAM;;uBAE5C,MAAM,GAAG,SAAS;;;6BAGZ,UAAU;EAEpC;AAKD,MAAM,WAAW,MAAM;IACtB,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,QAAQ,EAAE,eAAe,GAAG,aAAa,CAAC;IACnD,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACtB;AAED,wBAAgB,QAAQ,CAAC,SAAS,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,GAAG,SAAS,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAkBvH;AAED,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,SAAS,WAWjE"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { createHmac, generateKeyPairSync, verify } from "node:crypto";
|
|
2
|
+
import { Option, Schema } from "effect";
|
|
3
|
+
export function githubAppCredentials() {
|
|
4
|
+
const keys = generateKeyPairSync("rsa", { modulusLength: 2048 });
|
|
5
|
+
const appId = "123456";
|
|
6
|
+
const webhookSecret = "webhook-secret";
|
|
7
|
+
return {
|
|
8
|
+
appId,
|
|
9
|
+
appJwtOf: (authorization, nowMillis) => appJwtOf(keys.publicKey, authorization, nowMillis),
|
|
10
|
+
privateKey: keys.privateKey.export({ format: "pem", type: "pkcs8" }).toString(),
|
|
11
|
+
signedByApp: (jwt) => signedByApp(appId, jwt),
|
|
12
|
+
tokenPrefix: "installation-token-secret",
|
|
13
|
+
webhookSecret,
|
|
14
|
+
webhookSignature: (body) => `sha256=${createHmac("sha256", webhookSecret).update(body).digest("hex")}`,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
const Claims = Schema.fromJsonString(Schema.Struct({ exp: Schema.Number, iat: Schema.Number, iss: Schema.String }));
|
|
18
|
+
const Header = Schema.fromJsonString(Schema.Struct({ alg: Schema.String, typ: Schema.String }));
|
|
19
|
+
export function appJwtOf(publicKey, authorization, nowMillis) {
|
|
20
|
+
const [header = "", payload = "", signature = ""] = (authorization?.replace(/^Bearer /u, "") ?? "").split(".");
|
|
21
|
+
const claims = Schema.decodeUnknownOption(Claims)(Buffer.from(payload, "base64url").toString());
|
|
22
|
+
const fields = Schema.decodeUnknownOption(Header)(Buffer.from(header, "base64url").toString());
|
|
23
|
+
if (Option.isNone(claims) || Option.isNone(fields)) {
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
const now = nowMillis / 1000;
|
|
27
|
+
return {
|
|
28
|
+
algorithm: fields.value.alg,
|
|
29
|
+
expiresIn: claims.value.exp - now,
|
|
30
|
+
issuedAgo: now - claims.value.iat,
|
|
31
|
+
issuer: claims.value.iss,
|
|
32
|
+
signedBy: verify("RSA-SHA256", Buffer.from(`${header}.${payload}`), publicKey, Buffer.from(signature, "base64url"))
|
|
33
|
+
? "the App's key"
|
|
34
|
+
: "another key",
|
|
35
|
+
type: fields.value.typ,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
export function signedByApp(appId, jwt) {
|
|
39
|
+
return (jwt !== undefined
|
|
40
|
+
&& jwt.signedBy === "the App's key"
|
|
41
|
+
&& jwt.algorithm === "RS256"
|
|
42
|
+
&& jwt.issuer === appId
|
|
43
|
+
&& jwt.issuedAgo >= 0
|
|
44
|
+
&& jwt.issuedAgo <= 600
|
|
45
|
+
&& jwt.expiresIn > 0
|
|
46
|
+
&& jwt.expiresIn <= 600);
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=credentials.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"credentials.js","sourceRoot":"","sources":["../../../src/github/adapters/credentials.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
export declare function webhookDelivery(sign: (body: Uint8Array) => string): (url: string, delivery: {
|
|
3
|
+
readonly body: Uint8Array;
|
|
4
|
+
readonly event: string;
|
|
5
|
+
readonly deliveryId: string;
|
|
6
|
+
}) => Effect.Effect<number, import("effect/http/HttpClientError").HttpClientError, never>;
|
|
7
|
+
//# sourceMappingURL=webhookDelivery.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"webhookDelivery.d.ts","sourceRoot":"","sources":["../../../src/github/adapters/webhookDelivery.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAGhC,wBAAgB,eAAe,CAAC,IAAI,EAAE,CAAC,IAAI,EAAE,UAAU,KAAK,MAAM;mBACW,UAAU;oBAAkB,MAAM;yBAAuB,MAAM;0FAe3I"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { FetchHttpClient, HttpClient, HttpClientRequest } from "effect/http";
|
|
3
|
+
export function webhookDelivery(sign) {
|
|
4
|
+
return Effect.fnUntraced(function* (url, delivery) {
|
|
5
|
+
const client = yield* HttpClient.HttpClient;
|
|
6
|
+
const response = yield* client.execute(HttpClientRequest.post(url).pipe(HttpClientRequest.setHeaders({
|
|
7
|
+
"x-github-delivery": delivery.deliveryId,
|
|
8
|
+
"x-github-event": delivery.event,
|
|
9
|
+
"x-hub-signature-256": sign(delivery.body),
|
|
10
|
+
}), HttpClientRequest.bodyUint8Array(delivery.body, "application/json")));
|
|
11
|
+
yield* response.text;
|
|
12
|
+
return response.status;
|
|
13
|
+
}, Effect.provide(FetchHttpClient.layer));
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=webhookDelivery.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"webhookDelivery.js","sourceRoot":"","sources":["../../../src/github/adapters/webhookDelivery.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAE7E,MAAM,UAAU,eAAe,CAAC,IAAkC;IACjE,OAAO,MAAM,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE,GAAW,EAAE,QAA4F;QAC5I,MAAM,MAAM,GAAG,KAAK,CAAC,CAAC,UAAU,CAAC,UAAU,CAAC;QAC5C,MAAM,QAAQ,GAAG,KAAK,CAAC,CAAC,MAAM,CAAC,OAAO,CACrC,iBAAiB,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,IAAI,CAC/B,iBAAiB,CAAC,UAAU,CAAC;YAC5B,mBAAmB,EAAE,QAAQ,CAAC,UAAU;YACxC,gBAAgB,EAAE,QAAQ,CAAC,KAAK;YAChC,qBAAqB,EAAE,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC;SAC1C,CAAC,EACF,iBAAiB,CAAC,cAAc,CAAC,QAAQ,CAAC,IAAI,EAAE,kBAAkB,CAAC,CACnE,CACD,CAAC;QACF,KAAK,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC;QACrB,OAAO,QAAQ,CAAC,MAAM,CAAC;IACxB,CAAC,EAAE,MAAM,CAAC,OAAO,CAAC,eAAe,CAAC,KAAK,CAAC,CAAC,CAAC;AAC3C,CAAC"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { type FakeHttpRequest, type FakeHttpRoute } from "@shivaedev/test-story/server/fakeHttpService.ts";
|
|
3
|
+
import { githubAppCredentials } from "#github/adapters/credentials.ts";
|
|
4
|
+
export interface GitHubAppAccess {
|
|
5
|
+
readonly appRequest: (request: FakeHttpRequest) => boolean;
|
|
6
|
+
readonly installationOf: (request: FakeHttpRequest) => number | undefined;
|
|
7
|
+
readonly installationToken: (request: FakeHttpRequest) => boolean;
|
|
8
|
+
}
|
|
9
|
+
export declare const fakeGitHubApp: (options?: {
|
|
10
|
+
readonly credentials?: ReturnType<typeof githubAppCredentials>;
|
|
11
|
+
readonly prefix?: string;
|
|
12
|
+
readonly routes?: (access: GitHubAppAccess) => Readonly<Record<string, FakeHttpRoute>>;
|
|
13
|
+
} | undefined) => Effect.Effect<{
|
|
14
|
+
credentials: {
|
|
15
|
+
appId: string;
|
|
16
|
+
appJwtOf: (authorization: string | undefined, nowMillis: number) => import("#github/adapters/credentials.ts").AppJwt | undefined;
|
|
17
|
+
privateKey: string;
|
|
18
|
+
signedByApp: (jwt: import("#github/adapters/credentials.ts").AppJwt | undefined) => boolean;
|
|
19
|
+
tokenPrefix: string;
|
|
20
|
+
webhookSecret: string;
|
|
21
|
+
webhookSignature: (body: Uint8Array) => string;
|
|
22
|
+
};
|
|
23
|
+
deliverWebhook: (url: string, delivery: {
|
|
24
|
+
readonly body: Uint8Array;
|
|
25
|
+
readonly event: string;
|
|
26
|
+
readonly deliveryId: string;
|
|
27
|
+
}) => Effect.Effect<number, import("effect/http/HttpClientError").HttpClientError, never>;
|
|
28
|
+
server: {
|
|
29
|
+
completed: import("effect/Stream").Stream<import("@shivaedev/test-story/server/fakeHttpService.ts").FakeHttpRequestRecord, never, never>;
|
|
30
|
+
fault: (route: string, fault: import("@shivaedev/test-story/server/fakeHttpService.ts").FakeHttpFault, scope?: import("@shivaedev/test-story/server/fakeHttpService.ts").FakeHttpScope) => Effect.Effect<void, never, never>;
|
|
31
|
+
hold: (route: string, holdOptions?: import("@shivaedev/test-story/server/fakeHttpService.ts").FakeHttpHoldOptions) => Effect.Effect<{
|
|
32
|
+
release: Effect.Effect<void, never, never>;
|
|
33
|
+
waiting: Effect.Effect<void, never, never>;
|
|
34
|
+
}, never, never>;
|
|
35
|
+
narrate: (sink: (line: string) => void) => void;
|
|
36
|
+
received: import("effect/Stream").Stream<import("@shivaedev/test-story/server/fakeHttpService.ts").FakeHttpRequestRecord, never, never>;
|
|
37
|
+
releaseOnArrival: (route: string, arrival: string, scope?: import("@shivaedev/test-story/server/fakeHttpService.ts").FakeHttpScope) => Effect.Effect<void, never, never>;
|
|
38
|
+
readonly requests: readonly import("@shivaedev/test-story/server/fakeHttpService.ts").FakeHttpRequestRecord[];
|
|
39
|
+
scriptFault: (route: string, fault: import("@shivaedev/test-story/server/fakeHttpService.ts").FakeHttpFault, scriptOptions?: import("@shivaedev/test-story/server/fakeHttpService.ts").FakeHttpScriptOptions) => Effect.Effect<import("@shivaedev/test-story/server/fakeHttpService.ts").FakeHttpScript, never, never>;
|
|
40
|
+
stall: (route: string, scope?: import("@shivaedev/test-story/server/fakeHttpService.ts").FakeHttpScope) => Effect.Effect<{
|
|
41
|
+
release: Effect.Effect<void, never, never>;
|
|
42
|
+
waiting: Effect.Effect<void, never, never>;
|
|
43
|
+
}, never, never>;
|
|
44
|
+
untilAbandoned: (matches: (request: import("@shivaedev/test-story/server/fakeHttpService.ts").FakeHttpRequestRecord) => boolean, wait: import("@shivaedev/test-story/server/until.ts").UntilOptions) => Effect.Effect<import("@shivaedev/test-story/server/fakeHttpService.ts").FakeHttpRequestRecord, import("@shivaedev/test-story/server/until.ts").SignalNotReceived, never>;
|
|
45
|
+
url: string;
|
|
46
|
+
};
|
|
47
|
+
state: {
|
|
48
|
+
exchanges: number;
|
|
49
|
+
jwts: (import("#github/adapters/credentials.ts").AppJwt | undefined)[];
|
|
50
|
+
};
|
|
51
|
+
}, import("effect/Cause").UnknownError, import("effect/Scope").Scope>;
|
|
52
|
+
//# sourceMappingURL=fake.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fake.d.ts","sourceRoot":"","sources":["../../src/github/fake.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,KAAK,eAAe,EAAE,KAAK,aAAa,EAAmB,MAAM,iDAAiD,CAAC;AAE5H,OAAO,EAAE,oBAAoB,EAAE,MAAM,iCAAiC,CAAC;AAGvE,MAAM,WAAW,eAAe;IAC/B,QAAQ,CAAC,UAAU,EAAE,CAAC,OAAO,EAAE,eAAe,KAAK,OAAO,CAAC;IAC3D,QAAQ,CAAC,cAAc,EAAE,CAAC,OAAO,EAAE,eAAe,KAAK,MAAM,GAAG,SAAS,CAAC;IAC1E,QAAQ,CAAC,iBAAiB,EAAE,CAAC,OAAO,EAAE,eAAe,KAAK,OAAO,CAAC;CAClE;AAED,eAAO,MAAM,aAAa;2BAED,UAAU,CAAC,OAAO,oBAAoB,CAAC;sBAC5C,MAAM;sBACN,CAAC,MAAM,EAAE,eAAe,KAAK,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;qEAatF,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { fakeHttpService } from "@shivaedev/test-story/server/fakeHttpService.ts";
|
|
3
|
+
import { appProvider } from "#github/adapters/appProvider.ts";
|
|
4
|
+
import { githubAppCredentials } from "#github/adapters/credentials.ts";
|
|
5
|
+
import { webhookDelivery } from "#github/adapters/webhookDelivery.ts";
|
|
6
|
+
export const fakeGitHubApp = Effect.fnUntraced(function* (options = {}) {
|
|
7
|
+
const credentials = options.credentials ?? githubAppCredentials();
|
|
8
|
+
const prefix = options.prefix ?? "";
|
|
9
|
+
const { access, state, token } = yield* appProvider(credentials, prefix);
|
|
10
|
+
const server = yield* fakeHttpService({
|
|
11
|
+
routes: {
|
|
12
|
+
token,
|
|
13
|
+
...options.routes?.(access),
|
|
14
|
+
},
|
|
15
|
+
});
|
|
16
|
+
return { credentials, deliverWebhook: webhookDelivery(credentials.webhookSignature), server, state };
|
|
17
|
+
});
|
|
18
|
+
//# sourceMappingURL=fake.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fake.js","sourceRoot":"","sources":["../../src/github/fake.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAA4C,eAAe,EAAE,MAAM,iDAAiD,CAAC;AAC5H,OAAO,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC;AAC9D,OAAO,EAAE,oBAAoB,EAAE,MAAM,iCAAiC,CAAC;AACvE,OAAO,EAAE,eAAe,EAAE,MAAM,qCAAqC,CAAC;AAQtE,MAAM,CAAC,MAAM,aAAa,GAAG,MAAM,CAAC,UAAU,CAAC,QAAQ,CAAC,EACvD,OAAO,GAIH,EAAE;IAEN,MAAM,WAAW,GAAG,OAAO,CAAC,WAAW,IAAI,oBAAoB,EAAE,CAAC;IAClE,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,EAAE,CAAC;IACpC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,KAAK,CAAC,CAAC,WAAW,CAAC,WAAW,EAAE,MAAM,CAAC,CAAC;IACzE,MAAM,MAAM,GAAG,KAAK,CAAC,CAAC,eAAe,CAAC;QACrC,MAAM,EAAE;YACP,KAAK;YACL,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,MAAM,CAAC;SAC3B;KACD,CAAC,CAAC;IACH,OAAO,EAAE,WAAW,EAAE,cAAc,EAAE,eAAe,CAAC,WAAW,CAAC,gBAAgB,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC;AACtG,CAAC,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shivaedev/test-kits",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Reusable story building blocks for Platform applications",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"effect"
|
|
@@ -43,11 +43,12 @@
|
|
|
43
43
|
"README.md"
|
|
44
44
|
],
|
|
45
45
|
"dependencies": {
|
|
46
|
-
"@shivaedev/test-sql": "0.21.
|
|
47
|
-
"@shivaedev/test-story": "0.39.
|
|
46
|
+
"@shivaedev/test-sql": "0.21.2",
|
|
47
|
+
"@shivaedev/test-story": "0.39.2"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
|
-
"@shivaedev/
|
|
50
|
+
"@shivaedev/github": "0.13.0",
|
|
51
|
+
"@shivaedev/quality": "0.62.0",
|
|
51
52
|
"@types/node": "24.19.2",
|
|
52
53
|
"better-auth": "1.7.14",
|
|
53
54
|
"effect": "4.0.7",
|
|
@@ -58,10 +59,10 @@
|
|
|
58
59
|
"@shivaedev/changes": "0.10.1",
|
|
59
60
|
"@shivaedev/contract": "0.36.1",
|
|
60
61
|
"@shivaedev/discord": "0.2.1",
|
|
61
|
-
"@shivaedev/email": "0.4.
|
|
62
|
-
"@shivaedev/identity": "0.26.
|
|
63
|
-
"@shivaedev/jobs": "0.
|
|
64
|
-
"@shivaedev/sql": "0.
|
|
62
|
+
"@shivaedev/email": "0.4.3",
|
|
63
|
+
"@shivaedev/identity": "0.26.1",
|
|
64
|
+
"@shivaedev/jobs": "0.37.0",
|
|
65
|
+
"@shivaedev/sql": "0.40.0",
|
|
65
66
|
"better-auth": "1.7.14",
|
|
66
67
|
"effect": "4.0.7",
|
|
67
68
|
"pg-boss": "12.38.0"
|
|
@@ -76,7 +77,7 @@
|
|
|
76
77
|
"scripts": {
|
|
77
78
|
"build": "node --eval \"import('node:fs').then(({ rmSync }) => rmSync('dist', { force: true, recursive: true }))\" && tsc --project tsconfig.build.json",
|
|
78
79
|
"ready": "pnpm typecheck && pnpm test && pnpm build",
|
|
79
|
-
"test": "vitest run",
|
|
80
|
+
"test": "vitest run --configLoader native",
|
|
80
81
|
"typecheck": "tsc --noEmit"
|
|
81
82
|
}
|
|
82
83
|
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Clock, Effect } from "effect";
|
|
2
|
+
import type { FakeHttpRequest } from "@shivaedev/test-story/server/fakeHttpService.ts";
|
|
3
|
+
import type { GitHubAppAccess } from "#github/fake.ts";
|
|
4
|
+
import type { AppJwt, githubAppCredentials } from "./credentials.ts";
|
|
5
|
+
|
|
6
|
+
export const appProvider = Effect.fnUntraced(function* (credentials: ReturnType<typeof githubAppCredentials>, prefix: string) {
|
|
7
|
+
const clock = yield* Clock.Clock;
|
|
8
|
+
const state: { exchanges: number; jwts: (AppJwt | undefined)[] } = { exchanges: 0, jwts: [] };
|
|
9
|
+
const tokens = new Map<string, number>();
|
|
10
|
+
const access: GitHubAppAccess = {
|
|
11
|
+
appRequest: (request) => {
|
|
12
|
+
const jwt = credentials.appJwtOf(request.headers.authorization, clock.currentTimeMillisUnsafe());
|
|
13
|
+
state.jwts.push(jwt);
|
|
14
|
+
return credentials.signedByApp(jwt);
|
|
15
|
+
},
|
|
16
|
+
installationOf: (request) => tokens.get(request.headers.authorization ?? ""),
|
|
17
|
+
installationToken: (request) => tokens.has(request.headers.authorization ?? ""),
|
|
18
|
+
};
|
|
19
|
+
return {
|
|
20
|
+
access,
|
|
21
|
+
state,
|
|
22
|
+
token: {
|
|
23
|
+
guard: access.appRequest,
|
|
24
|
+
method: "POST",
|
|
25
|
+
path: `${prefix}/app/installations/:installationId/access_tokens`,
|
|
26
|
+
reply: (request: FakeHttpRequest) => {
|
|
27
|
+
state.exchanges += 1;
|
|
28
|
+
const token = `${credentials.tokenPrefix}-${state.exchanges}`;
|
|
29
|
+
tokens.set(`Bearer ${token}`, Number(request.params.installationId));
|
|
30
|
+
return { body: { "expires_at": new Date(clock.currentTimeMillisUnsafe() + 3_600_000).toISOString(), token } };
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
});
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { createHmac, generateKeyPairSync, type KeyObject, verify } from "node:crypto";
|
|
2
|
+
import { Option, Schema } from "effect";
|
|
3
|
+
|
|
4
|
+
export function githubAppCredentials() {
|
|
5
|
+
const keys = generateKeyPairSync("rsa", { modulusLength: 2048 });
|
|
6
|
+
const appId = "123456";
|
|
7
|
+
const webhookSecret = "webhook-secret";
|
|
8
|
+
return {
|
|
9
|
+
appId,
|
|
10
|
+
appJwtOf: (authorization: string | undefined, nowMillis: number) => appJwtOf(keys.publicKey, authorization, nowMillis),
|
|
11
|
+
privateKey: keys.privateKey.export({ format: "pem", type: "pkcs8" }).toString(),
|
|
12
|
+
signedByApp: (jwt: AppJwt | undefined) => signedByApp(appId, jwt),
|
|
13
|
+
tokenPrefix: "installation-token-secret",
|
|
14
|
+
webhookSecret,
|
|
15
|
+
webhookSignature: (body: Uint8Array) => `sha256=${createHmac("sha256", webhookSecret).update(body).digest("hex")}`,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const Claims = Schema.fromJsonString(Schema.Struct({ exp: Schema.Number, iat: Schema.Number, iss: Schema.String }));
|
|
20
|
+
const Header = Schema.fromJsonString(Schema.Struct({ alg: Schema.String, typ: Schema.String }));
|
|
21
|
+
|
|
22
|
+
export interface AppJwt {
|
|
23
|
+
readonly algorithm: string;
|
|
24
|
+
readonly expiresIn: number;
|
|
25
|
+
readonly issuedAgo: number;
|
|
26
|
+
readonly issuer: string;
|
|
27
|
+
readonly signedBy: "the App's key" | "another key";
|
|
28
|
+
readonly type: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function appJwtOf(publicKey: KeyObject, authorization: string | undefined, nowMillis: number): AppJwt | undefined {
|
|
32
|
+
const [header = "", payload = "", signature = ""] = (authorization?.replace(/^Bearer /u, "") ?? "").split(".");
|
|
33
|
+
const claims = Schema.decodeUnknownOption(Claims)(Buffer.from(payload, "base64url").toString());
|
|
34
|
+
const fields = Schema.decodeUnknownOption(Header)(Buffer.from(header, "base64url").toString());
|
|
35
|
+
if (Option.isNone(claims) || Option.isNone(fields)) {
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
38
|
+
const now = nowMillis / 1000;
|
|
39
|
+
return {
|
|
40
|
+
algorithm: fields.value.alg,
|
|
41
|
+
expiresIn: claims.value.exp - now,
|
|
42
|
+
issuedAgo: now - claims.value.iat,
|
|
43
|
+
issuer: claims.value.iss,
|
|
44
|
+
signedBy: verify("RSA-SHA256", Buffer.from(`${header}.${payload}`), publicKey, Buffer.from(signature, "base64url"))
|
|
45
|
+
? "the App's key"
|
|
46
|
+
: "another key",
|
|
47
|
+
type: fields.value.typ,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function signedByApp(appId: string, jwt: AppJwt | undefined) {
|
|
52
|
+
return (
|
|
53
|
+
jwt !== undefined
|
|
54
|
+
&& jwt.signedBy === "the App's key"
|
|
55
|
+
&& jwt.algorithm === "RS256"
|
|
56
|
+
&& jwt.issuer === appId
|
|
57
|
+
&& jwt.issuedAgo >= 0
|
|
58
|
+
&& jwt.issuedAgo <= 600
|
|
59
|
+
&& jwt.expiresIn > 0
|
|
60
|
+
&& jwt.expiresIn <= 600
|
|
61
|
+
);
|
|
62
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { FetchHttpClient, HttpClient, HttpClientRequest } from "effect/http";
|
|
3
|
+
|
|
4
|
+
export function webhookDelivery(sign: (body: Uint8Array) => string) {
|
|
5
|
+
return Effect.fnUntraced(function* (url: string, delivery: { readonly body: Uint8Array; readonly event: string; readonly deliveryId: string }) {
|
|
6
|
+
const client = yield* HttpClient.HttpClient;
|
|
7
|
+
const response = yield* client.execute(
|
|
8
|
+
HttpClientRequest.post(url).pipe(
|
|
9
|
+
HttpClientRequest.setHeaders({
|
|
10
|
+
"x-github-delivery": delivery.deliveryId,
|
|
11
|
+
"x-github-event": delivery.event,
|
|
12
|
+
"x-hub-signature-256": sign(delivery.body),
|
|
13
|
+
}),
|
|
14
|
+
HttpClientRequest.bodyUint8Array(delivery.body, "application/json"),
|
|
15
|
+
),
|
|
16
|
+
);
|
|
17
|
+
yield* response.text;
|
|
18
|
+
return response.status;
|
|
19
|
+
}, Effect.provide(FetchHttpClient.layer));
|
|
20
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { type FakeHttpRequest, type FakeHttpRoute, fakeHttpService } from "@shivaedev/test-story/server/fakeHttpService.ts";
|
|
3
|
+
import { appProvider } from "#github/adapters/appProvider.ts";
|
|
4
|
+
import { githubAppCredentials } from "#github/adapters/credentials.ts";
|
|
5
|
+
import { webhookDelivery } from "#github/adapters/webhookDelivery.ts";
|
|
6
|
+
|
|
7
|
+
export interface GitHubAppAccess {
|
|
8
|
+
readonly appRequest: (request: FakeHttpRequest) => boolean;
|
|
9
|
+
readonly installationOf: (request: FakeHttpRequest) => number | undefined;
|
|
10
|
+
readonly installationToken: (request: FakeHttpRequest) => boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const fakeGitHubApp = Effect.fnUntraced(function* (
|
|
14
|
+
options: {
|
|
15
|
+
readonly credentials?: ReturnType<typeof githubAppCredentials>;
|
|
16
|
+
readonly prefix?: string;
|
|
17
|
+
readonly routes?: (access: GitHubAppAccess) => Readonly<Record<string, FakeHttpRoute>>;
|
|
18
|
+
} = {},
|
|
19
|
+
) {
|
|
20
|
+
const credentials = options.credentials ?? githubAppCredentials();
|
|
21
|
+
const prefix = options.prefix ?? "";
|
|
22
|
+
const { access, state, token } = yield* appProvider(credentials, prefix);
|
|
23
|
+
const server = yield* fakeHttpService({
|
|
24
|
+
routes: {
|
|
25
|
+
token,
|
|
26
|
+
...options.routes?.(access),
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
return { credentials, deliverWebhook: webhookDelivery(credentials.webhookSignature), server, state };
|
|
30
|
+
});
|