File: textconfaker.go

package info (click to toggle)
golang-github-nicholas-fedor-shoutrrr 0.8.17-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,332 kB
  • sloc: sh: 61; makefile: 5
file content (106 lines) | stat: -rw-r--r-- 2,616 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package testutils

import (
	"bufio"
	"bytes"
	"fmt"
	"net/textproto"
	"strings"
)

type textConFaker struct {
	inputBuffer  *bytes.Buffer
	inputWriter  *bufio.Writer
	outputReader *bufio.Reader
	responses    []string
	delim        string
}

func (tcf *textConFaker) GetInput() string {
	_ = tcf.inputWriter.Flush()

	return tcf.inputBuffer.String()
}

// GetConversation returns the input and output streams as a conversation.
func (tcf *textConFaker) GetConversation(includeGreeting bool) string {
	conv := ""
	inSequence := false
	input := strings.Split(tcf.GetInput(), tcf.delim)
	responseIndex := 0

	if includeGreeting {
		conv += fmt.Sprintf("    %-55s << %-50s\n", "(server greeting)", tcf.responses[0])
		responseIndex = 1
	}

	for i, query := range input {
		if query == "." {
			inSequence = false
		}

		resp := ""
		if len(tcf.responses) > responseIndex && !inSequence {
			resp = tcf.responses[responseIndex]
		}

		if query == "" && resp == "" && i == len(input)-1 {
			break
		}

		conv += fmt.Sprintf("  #%2d >> %50s << %-50s\n", i, query, resp)

		for len(resp) > 3 && resp[3] == '-' {
			responseIndex++
			resp = tcf.responses[responseIndex]
			conv += fmt.Sprintf("         %50s << %-50s\n", " ", resp)
		}

		if !inSequence {
			responseIndex++
		}

		if len(resp) > 0 && resp[0] == '3' {
			inSequence = true
		}
	}

	return conv
}

// GetClientSentences returns all the input received from the client separated by the delimiter.
func (tcf *textConFaker) GetClientSentences() []string {
	_ = tcf.inputWriter.Flush()

	return strings.Split(tcf.inputBuffer.String(), tcf.delim)
}

// CreateReadWriter returns a ReadWriter from the textConFakers internal reader and writer.
func (tcf *textConFaker) CreateReadWriter() *bufio.ReadWriter {
	return bufio.NewReadWriter(tcf.outputReader, tcf.inputWriter)
}

func (tcf *textConFaker) init() {
	tcf.inputBuffer = &bytes.Buffer{}
	stringReader := strings.NewReader(strings.Join(tcf.responses, tcf.delim))
	tcf.outputReader = bufio.NewReader(stringReader)
	tcf.inputWriter = bufio.NewWriter(tcf.inputBuffer)
}

// CreateTextConFaker returns a textproto.Conn to fake textproto based connections.
func CreateTextConFaker(responses []string, delim string) (*textproto.Conn, Eavesdropper) {
	tcfaker := textConFaker{
		responses: responses,
		delim:     delim,
	}
	tcfaker.init()

	// rx := iotest.NewReadLogger("TextConRx", tcfaker.outputReader)
	// tx := iotest.NewWriteLogger("TextConTx", tcfaker.inputWriter)
	// faker := CreateIOFaker(rx, tx)
	faker := ioFaker{
		ReadWriter: tcfaker.CreateReadWriter(),
	}

	return textproto.NewConn(faker), &tcfaker
}