Live video on the AT Protocol
1package devenv
2
3import (
4 "bufio"
5 "context"
6 "encoding/json"
7 "fmt"
8 "os/exec"
9 "path/filepath"
10 "runtime"
11 "testing"
12
13 comatproto "github.com/bluesky-social/indigo/api/atproto"
14 "github.com/bluesky-social/indigo/xrpc"
15 "github.com/google/uuid"
16 "github.com/stretchr/testify/require"
17 "stream.place/streamplace/pkg/aqhttp"
18 "stream.place/streamplace/pkg/log"
19)
20
21type DevEnv struct {
22 PDSURL string `json:"pds-url"`
23 PLCURL string `json:"plc-url"`
24}
25
26func WithDevEnv(t *testing.T) *DevEnv {
27 _, filename, _, _ := runtime.Caller(0)
28 cmd := exec.Command("node", "../../js/dev-env/run.mjs")
29 cmd.Dir = filepath.Dir(filename)
30
31 // Start the command and get pipes for streaming output
32 stdout, err := cmd.StdoutPipe()
33 if err != nil {
34 t.Logf("Error getting stdout pipe: %v", err)
35 t.FailNow()
36 }
37
38 if err := cmd.Start(); err != nil {
39 t.Logf("Error starting dev env: %v", err)
40 t.FailNow()
41 }
42
43 var env DevEnv
44
45 scanner := bufio.NewScanner(stdout)
46 scanner.Scan()
47 err = json.Unmarshal(scanner.Bytes(), &env)
48 if err != nil {
49 t.Logf("Error unmarshalling dev-env stdout: %v", err)
50 t.FailNow()
51 }
52
53 go func() {
54 scanner := bufio.NewScanner(stdout)
55 for scanner.Scan() {
56 t.Logf("dev-env stdout: %s", scanner.Text())
57 if scanner.Err() != nil {
58 return
59 }
60 }
61 }()
62
63 // Ensure cleanup happens when test finishes
64 t.Cleanup(func() {
65 t.Logf("killing dev env")
66 if cmd.Process != nil {
67 _ = cmd.Process.Kill()
68 _ = cmd.Wait()
69 }
70 })
71
72 return &env
73}
74
75type DevEnvAccount struct {
76 Handle string
77 Email string
78 Password string
79 DID string
80 XRPC *xrpc.Client
81}
82
83func (d *DevEnv) CreateAccount(t *testing.T) *DevEnvAccount {
84
85 xrpcc := &xrpc.Client{
86 Host: d.PDSURL,
87 Client: &aqhttp.Client,
88 }
89
90 uu, err := uuid.NewRandom()
91 require.NoError(t, err)
92
93 handle := fmt.Sprintf("sp-%s.test", uu.String()[:8])
94 email := fmt.Sprintf("%s@example.com", handle)
95 password := "test"
96
97 out, err := comatproto.ServerCreateAccount(context.Background(), xrpcc, &comatproto.ServerCreateAccount_Input{
98 Handle: handle,
99 Email: &email,
100 Password: &password,
101 })
102 require.NoError(t, err)
103 log.Log(context.Background(), "created account", "did", out.Did, "handle", out.Handle)
104
105 session, err := comatproto.ServerCreateSession(context.Background(), xrpcc, &comatproto.ServerCreateSession_Input{
106 Identifier: out.Handle,
107 Password: password,
108 })
109 require.NoError(t, err)
110
111 xrpcc = &xrpc.Client{
112 Host: d.PDSURL,
113 Client: &aqhttp.Client,
114 Auth: &xrpc.AuthInfo{
115 Did: out.Did,
116 AccessJwt: session.AccessJwt,
117 RefreshJwt: session.RefreshJwt,
118 Handle: out.Handle,
119 },
120 }
121
122 return &DevEnvAccount{
123 Handle: out.Handle,
124 Email: email,
125 Password: password,
126 DID: out.Did,
127 XRPC: xrpcc,
128 }
129}