1//go:build linux
2
3package app
4
5import (
6 "context"
7 "io"
8 "os"
9 "strings"
10 "testing"
11
12 "github.com/bnema/vev/internal/adapters/term"
13 "github.com/bnema/vev/internal/adapters/uiterm"
14 "github.com/bnema/vev/internal/domain"
15 "github.com/bnema/vev/internal/ports"
16 "github.com/stretchr/testify/require"
17)
18
19func TestInteractiveObservationPublishesPhysicalWriterTransaction(t *testing.T) {
20 inputReader, inputWriter, err := os.Pipe()
21 require.NoError(t, err)
22 defer func() { _ = inputReader.Close() }()
23 defer func() { _ = inputWriter.Close() }()
24 outputReader, outputWriter, err := os.Pipe()
25 require.NoError(t, err)
26 defer func() { _ = outputReader.Close() }()
27 defer func() { _ = outputWriter.Close() }()
28
29 ctx, cancel := context.WithCancel(context.Background())
30 defer cancel()
31 mirror, err := uiterm.NewMirror(ctx, domain.Geometry{Size: domain.Size{Cols: 8, Rows: 2}}, "")
32 require.NoError(t, err)
33 defer mirror.Close()
34 physical := term.NewWithFilesAndObservation(inputReader, outputWriter, mirror)
35 observed := observedTerminal{Terminal: physical, UIOutputTransaction: mirror}
36 contextValue := ports.UIContext{AttachmentHandle: "attachment", Generation: 1, Status: ports.UIStatusAttached}
37
38 observed.BeginOutput(contextValue)
39 _, err = observed.Out().Write([]byte("hello"))
40 require.NoError(t, err)
41 require.NoError(t, observed.Flush())
42 observed.EndOutput(true)
43 _, err = io.ReadAll(io.LimitReader(outputReader, int64(len("hello"))))
44 require.NoError(t, err)
45
46 snapshot, err := mirror.Snapshot()
47 require.NoError(t, err)
48 require.Equal(t, contextValue.AttachmentHandle, snapshot.Context.AttachmentHandle)
49 require.Equal(t, contextValue.Generation, snapshot.Context.Generation)
50 require.True(t, strings.HasPrefix(snapshotText(snapshot), "hello"))
51}
52
53func snapshotText(snapshot ports.UISnapshot) string {
54 var text []byte
55 for _, cell := range snapshot.Cells {
56 if cell.Continuation {
57 continue
58 }
59 if cell.Text == "" {
60 text = append(text, ' ')
61 continue
62 }
63 text = append(text, cell.Text...)
64 }
65 return string(text)
66}