mirror of
https://github.com/openlibrecommunity/olcrtc.git
synced 2026-06-03 19:09:46 +00:00
148 lines
3.9 KiB
Go
148 lines
3.9 KiB
Go
package jitsi
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/openlibrecommunity/olcrtc/internal/engine"
|
|
)
|
|
|
|
func TestNormaliseHost(t *testing.T) {
|
|
tests := []struct {
|
|
raw string
|
|
want string
|
|
}{
|
|
{"meet.example.com", "meet.example.com"},
|
|
{"https://meet.example.com", "meet.example.com"},
|
|
{"https://meet.example.com/", "meet.example.com"},
|
|
{"https://meet.example.com/path", "meet.example.com"},
|
|
{"//meet.example.com", "meet.example.com"},
|
|
{" https://meet.example.com ", "meet.example.com"},
|
|
{"", ""},
|
|
}
|
|
for _, tc := range tests {
|
|
t.Run(tc.raw, func(t *testing.T) {
|
|
if got := normaliseHost(tc.raw); got != tc.want {
|
|
t.Fatalf("normaliseHost(%q) = %q, want %q", tc.raw, got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestDecodeRaw(t *testing.T) {
|
|
const payload = "hello world"
|
|
raw := encodeForTest(t, []byte(payload))
|
|
|
|
got := decodeRaw(makeBridgeMessage("EndpointMessage", map[string]any{"raw": raw}))
|
|
if string(got) != payload {
|
|
t.Fatalf("decodeRaw = %q, want %q", got, payload)
|
|
}
|
|
|
|
if got := decodeRaw(makeBridgeMessage("OtherClass", map[string]any{"raw": raw})); got != nil {
|
|
t.Fatalf("decodeRaw(other class) = %q, want nil", got)
|
|
}
|
|
if got := decodeRaw(makeBridgeMessage("EndpointMessage", map[string]any{})); got != nil {
|
|
t.Fatalf("decodeRaw(no raw) = %q, want nil", got)
|
|
}
|
|
if got := decodeRaw(makeBridgeMessage("EndpointMessage", map[string]any{"raw": "not-base64!!!"})); got != nil {
|
|
t.Fatalf("decodeRaw(bad base64) = %q, want nil", got)
|
|
}
|
|
}
|
|
|
|
func TestNewRequiresHost(t *testing.T) {
|
|
_, err := New(context.Background(), engine.Config{
|
|
Extra: map[string]string{"room": "myroom"},
|
|
})
|
|
if !errors.Is(err, ErrHostRequired) {
|
|
t.Fatalf("err = %v, want ErrHostRequired", err)
|
|
}
|
|
}
|
|
|
|
func TestNewRequiresRoom(t *testing.T) {
|
|
_, err := New(context.Background(), engine.Config{
|
|
URL: "meet.example.com",
|
|
})
|
|
if !errors.Is(err, ErrRoomRequired) {
|
|
t.Fatalf("err = %v, want ErrRoomRequired", err)
|
|
}
|
|
}
|
|
|
|
func TestNewSucceeds(t *testing.T) {
|
|
sess, err := New(context.Background(), engine.Config{
|
|
URL: "https://meet.example.com",
|
|
Extra: map[string]string{"room": "myroom"},
|
|
Name: "olcrtc-test",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("New: %v", err)
|
|
}
|
|
defer func() { _ = sess.Close() }()
|
|
caps := sess.Capabilities()
|
|
if !caps.ByteStream || !caps.VideoTrack {
|
|
t.Fatalf("Capabilities = %+v, want ByteStream && VideoTrack", caps)
|
|
}
|
|
}
|
|
|
|
func TestSendBeforeConnect(t *testing.T) {
|
|
sess, err := New(context.Background(), engine.Config{
|
|
URL: "meet.example.com",
|
|
Extra: map[string]string{"room": "myroom"},
|
|
OnData: func([]byte) {},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("New: %v", err)
|
|
}
|
|
defer func() { _ = sess.Close() }()
|
|
if err := sess.Send([]byte("data")); !errors.Is(err, ErrBridgeNotReady) {
|
|
t.Fatalf("Send err = %v, want ErrBridgeNotReady", err)
|
|
}
|
|
}
|
|
|
|
func TestSendAfterClose(t *testing.T) {
|
|
sess, err := New(context.Background(), engine.Config{
|
|
URL: "meet.example.com",
|
|
Extra: map[string]string{"room": "myroom"},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("New: %v", err)
|
|
}
|
|
if err := sess.Close(); err != nil {
|
|
t.Fatalf("Close: %v", err)
|
|
}
|
|
if err := sess.Send([]byte("data")); !errors.Is(err, ErrSessionClosed) {
|
|
t.Fatalf("Send err = %v, want ErrSessionClosed", err)
|
|
}
|
|
}
|
|
|
|
func TestSanitiseNick(t *testing.T) {
|
|
tests := []struct {
|
|
raw string
|
|
want string
|
|
}{
|
|
{"alice", "alice"},
|
|
{"Alice Smith", "Alice-Smith"},
|
|
{"Конрад Олег", ""},
|
|
{"olcrtc-bot42", "olcrtc-bot42"},
|
|
{" bob ", "bob"},
|
|
{"$$$ %%%", ""},
|
|
{"verylongnicknamethatexceedslimit", "verylongnicknamet"[:16]},
|
|
}
|
|
for _, tc := range tests {
|
|
t.Run(tc.raw, func(t *testing.T) {
|
|
if got := sanitiseNick(tc.raw); got != tc.want {
|
|
t.Fatalf("sanitiseNick(%q) = %q, want %q", tc.raw, got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestEngineRegistration(t *testing.T) {
|
|
if _, err := engine.New(context.Background(), "jitsi", engine.Config{
|
|
URL: "meet.example.com",
|
|
Extra: map[string]string{"room": "myroom"},
|
|
}); err != nil {
|
|
t.Fatalf("engine.New(jitsi) = %v, want nil", err)
|
|
}
|
|
}
|