Live video on the AT Protocol
79
fork

Configure Feed

Select the types of activity you want to include in your feed.

at eli/multitesting 113 lines 3.2 kB view raw
1package atproto 2 3import ( 4 "context" 5 "encoding/json" 6 "io/fs" 7 "testing" 8 "testing/fstest" 9 10 "github.com/stretchr/testify/require" 11 "stream.place/streamplace/lexicons" 12 "stream.place/streamplace/pkg/config" 13 "stream.place/streamplace/pkg/model" 14 "stream.place/streamplace/pkg/statedb" 15) 16 17func TestLexiconRepo(t *testing.T) { 18 cli := config.CLI{ 19 BroadcasterHost: "example.com", 20 DBURL: ":memory:", 21 } 22 cli.DataDir = t.TempDir() 23 mod, err := model.MakeDB(":memory:") 24 require.NoError(t, err) 25 state, err := statedb.MakeDB(context.Background(), &cli, nil, mod) 26 require.NoError(t, err) 27 28 // creating a new repo 29 handle, err := MakeLexiconRepo(context.Background(), &cli, mod, state) 30 require.NoError(t, err) 31 r, sess, err := OpenLexiconRepo(context.Background()) 32 require.NoError(t, err) 33 require.NotNil(t, r) 34 require.NotNil(t, sess) 35 c, _, err := r.GetRecord(context.Background(), "com.atproto.lexicon.schema/place.stream.chat.message") 36 require.NoError(t, err) 37 rec, err := GetRecordCBOR(context.Background(), sess, c, "com.atproto.lexicon.schema", "place.stream.chat.message") 38 require.NoError(t, err) 39 require.NotNil(t, rec) 40 handle.Close() 41 42 evts, err := state.GetCommitEventsSinceSeq(cli.MyDID(), 0) 43 require.NoError(t, err) 44 require.Len(t, evts, 1) 45 require.Equal(t, evts[0].RepoDID, cli.MyDID()) 46 47 // opening an existing repo 48 handle, err = MakeLexiconRepo(context.Background(), &cli, mod, state) 49 require.NoError(t, err) 50 handle.Close() 51 52 // Walk all files and build a map of file contents, modifying the first file 53 files := map[string]*fstest.MapFile{} 54 55 err = fs.WalkDir(lexicons.AllFiles, ".", func(path string, d fs.DirEntry, err error) error { 56 if err != nil { 57 return err 58 } 59 if d.IsDir() { 60 return nil 61 } 62 contents, err := lexicons.AllFiles.ReadFile(path) 63 if err != nil { 64 return err 65 } 66 if path == "place/stream/chat/message.json" { 67 // Modify the first file encountered (for example, append a newline) 68 data := map[string]any{} 69 err := json.Unmarshal(contents, &data) 70 if err != nil { 71 return err 72 } 73 data["defs"].(map[string]any)["main"].(map[string]any)["description"] = "Some kind of chat nonsense." 74 contents, err = json.Marshal(data) 75 if err != nil { 76 return err 77 } 78 } 79 info, err := d.Info() 80 if err != nil { 81 return err 82 } 83 files[path] = &fstest.MapFile{ 84 Data: contents, 85 Mode: 0444, 86 ModTime: info.ModTime(), 87 } 88 return nil 89 }) 90 require.NoError(t, err) 91 modifiedFS := fstest.MapFS{} 92 for k, v := range files { 93 modifiedFS[k] = v 94 } 95 AllFiles = modifiedFS 96 97 // opening an existing repo with modified lexicon 98 handle, err = MakeLexiconRepo(context.Background(), &cli, mod, state) 99 require.NoError(t, err) 100 handle.Close() 101 102 evts, err = state.GetCommitEventsSinceSeq(cli.MyDID(), 0) 103 require.NoError(t, err) 104 require.Len(t, evts, 2) 105 require.Equal(t, evts[0].RepoDID, cli.MyDID()) 106 require.Equal(t, evts[1].RepoDID, cli.MyDID()) 107 oldCommit, err := evts[0].ToCommitEvent() 108 require.NoError(t, err) 109 newCommit, err := evts[1].ToCommitEvent() 110 require.NoError(t, err) 111 require.Equal(t, newCommit.Since, &oldCommit.Rev) 112 require.Equal(t, newCommit.PrevData.String(), evts[0].SignedData) 113}