ba35e251be
Streamed card content is replayed by the DingTalk client with a typewriter animation it drives itself, so an answer that is already complete on the server can still be typed out - and a client that leaves the foreground pauses that animation, which reads as 'streaming stopped, it continues when I switch back' long after the final frame was pushed. Add DINGTALK_CARD_FINAL_WRITE (streaming default | update): in update mode the streaming finalize is followed by one plain PUT /v1.0/card/instances write with the identical template variables, so the terminal content has no typewriter queue behind it. The follow-up write never fails a delivered answer.
95 lines
2.9 KiB
Go
95 lines
2.9 KiB
Go
package dingtalk
|
|
|
|
import "testing"
|
|
|
|
func TestParseCardPartialUpdatesMode(t *testing.T) {
|
|
tests := []struct {
|
|
raw string
|
|
want CardPartialUpdatesMode
|
|
}{
|
|
{"", CardPartialUpdatesPartial},
|
|
{"on", CardPartialUpdatesPartial},
|
|
{"PARTIAL", CardPartialUpdatesPartial},
|
|
{"sometimes", CardPartialUpdatesPartial},
|
|
{"final", CardPartialUpdatesFinal},
|
|
{" FINAL ", CardPartialUpdatesFinal},
|
|
{"off", CardPartialUpdatesFinal},
|
|
{"0", CardPartialUpdatesFinal},
|
|
{"false", CardPartialUpdatesFinal},
|
|
{"no", CardPartialUpdatesFinal},
|
|
{"answer", CardPartialUpdatesFinal},
|
|
{"answer_only", CardPartialUpdatesFinal},
|
|
{"direct", CardPartialUpdatesDirect},
|
|
{"Single", CardPartialUpdatesDirect},
|
|
{"once", CardPartialUpdatesDirect},
|
|
}
|
|
for _, tt := range tests {
|
|
if got := ParseCardPartialUpdatesMode(tt.raw); got != tt.want {
|
|
t.Fatalf("ParseCardPartialUpdatesMode(%q)=%q want %q", tt.raw, got, tt.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCardPartialUpdatesModeFramesAndPlaceholder(t *testing.T) {
|
|
tests := []struct {
|
|
mode CardPartialUpdatesMode
|
|
streamsPartials bool
|
|
placeholderIssued bool
|
|
}{
|
|
{CardPartialUpdatesPartial, true, true},
|
|
{CardPartialUpdatesFinal, false, true},
|
|
{CardPartialUpdatesDirect, false, false},
|
|
// An unset mode must keep the historical streaming behaviour.
|
|
{"", true, true},
|
|
}
|
|
for _, tt := range tests {
|
|
if got := tt.mode.streamsPartialFrames(); got != tt.streamsPartials {
|
|
t.Fatalf("mode=%q streamsPartialFrames=%v want %v", tt.mode, got, tt.streamsPartials)
|
|
}
|
|
if got := tt.mode.placeholderCardRequested(); got != tt.placeholderIssued {
|
|
t.Fatalf("mode=%q placeholderCardRequested=%v want %v", tt.mode, got, tt.placeholderIssued)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestParseCardFinalWriteMode(t *testing.T) {
|
|
tests := []struct {
|
|
raw string
|
|
want CardFinalWriteMode
|
|
}{
|
|
{"", CardFinalWriteStreaming},
|
|
{"streaming", CardFinalWriteStreaming},
|
|
{"stream", CardFinalWriteStreaming},
|
|
{"sometimes", CardFinalWriteStreaming},
|
|
{"0", CardFinalWriteStreaming},
|
|
{"off", CardFinalWriteStreaming},
|
|
{"update", CardFinalWriteUpdate},
|
|
{" UPDATE ", CardFinalWriteUpdate},
|
|
{"plain", CardFinalWriteUpdate},
|
|
{"plain_update", CardFinalWriteUpdate},
|
|
{"on", CardFinalWriteUpdate},
|
|
{"true", CardFinalWriteUpdate},
|
|
{"yes", CardFinalWriteUpdate},
|
|
{"1", CardFinalWriteUpdate},
|
|
}
|
|
for _, tt := range tests {
|
|
if got := ParseCardFinalWriteMode(tt.raw); got != tt.want {
|
|
t.Fatalf("ParseCardFinalWriteMode(%q)=%q want %q", tt.raw, got, tt.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCardFinalWriteModeAlsoPlainUpdates(t *testing.T) {
|
|
// Only the explicit update mode adds the plain write; the zero value and the
|
|
// historical streaming mode must leave the single-write behaviour untouched.
|
|
for mode, want := range map[CardFinalWriteMode]bool{
|
|
CardFinalWriteStreaming: false,
|
|
CardFinalWriteUpdate: true,
|
|
"": false,
|
|
} {
|
|
if got := mode.alsoPlainUpdates(); got != want {
|
|
t.Fatalf("mode=%q alsoPlainUpdates=%v want %v", mode, got, want)
|
|
}
|
|
}
|
|
}
|