1- import {
2- createFederation ,
3- generateCryptoKeyPair ,
4- MemoryKvStore ,
5- } from "@fedify/fedify" ;
6- import { Create , Endpoints , Person } from "@fedify/vocab" ;
71import assert from "node:assert/strict" ;
82import { mkdtemp , writeFile } from "node:fs/promises" ;
93import { tmpdir } from "node:os" ;
104import { join } from "node:path" ;
115import test from "node:test" ;
12- import { serve } from "srvx " ;
6+ import { spawnBenchmarkTarget } from "../../../../test/bench/fixture.ts " ;
137import runBench , { withUserAgent } from "./action.ts" ;
148import type { BenchCommand } from "./command.ts" ;
159
16- async function spawnTarget ( ) {
17- const federation = createFederation < void > ( {
18- kv : new MemoryKvStore ( ) ,
19- benchmarkMode : true ,
20- } ) ;
21- let keyPairs : CryptoKeyPair [ ] | undefined ;
22- const requests : { method : string ; path : string } [ ] = [ ] ;
23- federation
24- . setActorDispatcher ( "/users/{identifier}" , async ( ctx , identifier ) => {
25- if ( identifier !== "alice" ) return null ;
26- const pairs = await ctx . getActorKeyPairs ( identifier ) ;
27- return new Person ( {
28- id : ctx . getActorUri ( identifier ) ,
29- preferredUsername : identifier ,
30- inbox : ctx . getInboxUri ( identifier ) ,
31- endpoints : new Endpoints ( { sharedInbox : ctx . getInboxUri ( ) } ) ,
32- publicKey : pairs [ 0 ] ?. cryptographicKey ,
33- assertionMethods : pairs . map ( ( p ) => p . multikey ) ,
34- } ) ;
35- } )
36- . mapHandle ( ( _ctx , username ) => ( username === "alice" ? "alice" : null ) )
37- . setKeyPairsDispatcher ( async ( _ctx , identifier ) => {
38- if ( identifier !== "alice" ) return [ ] ;
39- keyPairs ??= [
40- await generateCryptoKeyPair ( "RSASSA-PKCS1-v1_5" ) ,
41- await generateCryptoKeyPair ( "Ed25519" ) ,
42- ] ;
43- return keyPairs ;
44- } ) ;
45- federation . setInboxListeners ( "/users/{identifier}/inbox" , "/inbox" ) . on (
46- Create ,
47- ( ) => { } ,
48- ) ;
49- let inboxUserAgent : string | null = null ;
50- const server = serve ( {
51- port : 0 ,
52- hostname : "127.0.0.1" ,
53- silent : true ,
54- fetch : ( request : Request ) => {
55- const url = new URL ( request . url ) ;
56- requests . push ( { method : request . method , path : url . pathname } ) ;
57- if ( request . method === "POST" ) {
58- inboxUserAgent = request . headers . get ( "user-agent" ) ;
59- }
60- return federation . fetch ( request , { contextData : undefined } ) ;
61- } ,
62- } ) ;
63- await server . ready ( ) ;
64- return {
65- url : new URL ( server . url ! ) ,
66- inboxUserAgent : ( ) => inboxUserAgent ,
67- requests : ( ) => requests . slice ( ) ,
68- close : ( ) => server . close ( true ) ,
69- } ;
70- }
71-
7210function command ( overrides : Partial < BenchCommand > ) : BenchCommand {
7311 return {
7412 command : "bench" ,
@@ -108,7 +46,7 @@ ${expectLine}
10846}
10947
11048test ( "runBench - passing gate exits 0 and writes a valid report" , async ( ) => {
111- const target = await spawnTarget ( ) ;
49+ const target = await spawnBenchmarkTarget ( ) ;
11250 try {
11351 const file = await writeSuite (
11452 inboxSuite ( target . url , ' successRate: ">= 99%"' ) ,
@@ -177,7 +115,7 @@ test("withUserAgent - does not override an explicit User-Agent", async () => {
177115} ) ;
178116
179117test ( "runBench - failing gate exits 1" , async ( ) => {
180- const target = await spawnTarget ( ) ;
118+ const target = await spawnBenchmarkTarget ( ) ;
181119 try {
182120 // An impossible latency threshold makes the gate fail.
183121 const file = await writeSuite (
@@ -198,7 +136,7 @@ test("runBench - failing gate exits 1", async () => {
198136} ) ;
199137
200138test ( "runBench - dry run prints a plan and sends nothing" , async ( ) => {
201- const target = await spawnTarget ( ) ;
139+ const target = await spawnBenchmarkTarget ( ) ;
202140 try {
203141 const file = await writeSuite (
204142 inboxSuite ( target . url , ' successRate: ">= 99%"' ) ,
@@ -346,7 +284,7 @@ test("runBench - refuses an inbox destination off the gated target (exit 2)", as
346284 // A loopback target passes the gate, but an explicit public `inbox:` is the
347285 // actual load destination; it must be gated too, or production could be
348286 // benchmarked through the back door.
349- const target = await spawnTarget ( ) ;
287+ const target = await spawnBenchmarkTarget ( ) ;
350288 try {
351289 const file = await writeSuite ( `version: 1
352290target: ${ target . url . href }
0 commit comments