Skip to content

Take control over your live stream video by running it yourself. Streaming + chat out of the box.

2.1 KB
Raw Blame
package outbox

import (
	"testing"

	"code.superseriousbusiness.org/activity/streams"
	"code.superseriousbusiness.org/activity/streams/vocab"
)

// testOutbox carries no deps. Every assertion in this file exercises
// the early-error path inside Add (nil IRI extraction), which returns
// before deps are touched.
var testOutbox = &Service{}

// TestAddWithNilId verifies that Add doesn't panic when given an item with nil JSONLD ID.
func TestAddWithNilId(t *testing.T) {
	defer func() {
		if r := recover(); r != nil {
			t.Errorf("Add panicked with nil ID: %v", r)
		}
	}()

	note := streams.NewActivityStreamsNote()
	// Don't set JSONLD ID

	err := testOutbox.Add(note, "test-id", false)
	if err == nil {
		t.Error("Add with nil ID should return error")
	}
}

// TestAddWithIdButNilIRI verifies that Add doesn't panic when given an item
// with a JSONLD ID property that has no IRI set.
func TestAddWithIdButNilIRI(t *testing.T) {
	defer func() {
		if r := recover(); r != nil {
			t.Errorf("Add panicked with ID but nil IRI: %v", r)
		}
	}()

	note := streams.NewActivityStreamsNote()
	id := streams.NewJSONLDIdProperty()
	// Set the ID property but don't set an IRI on it
	note.SetJSONLDId(id)

	err := testOutbox.Add(note, "test-id", false)
	if err == nil {
		t.Error("Add with ID but nil IRI should return error")
	}
}

// TestAddNoPanic verifies that Add doesn't panic with various nil/empty inputs.
func TestAddNoPanic(t *testing.T) {
	testCases := []struct {
		name string
		item func() vocab.ActivityStreamsNote
	}{
		{
			name: "note with no properties",
			item: func() vocab.ActivityStreamsNote {
				return streams.NewActivityStreamsNote()
			},
		},
		{
			name: "note with empty ID property",
			item: func() vocab.ActivityStreamsNote {
				note := streams.NewActivityStreamsNote()
				note.SetJSONLDId(streams.NewJSONLDIdProperty())
				return note
			},
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			defer func() {
				if r := recover(); r != nil {
					t.Errorf("Add panicked: %v", r)
				}
			}()
			_ = testOutbox.Add(tc.item(), "test-id", false)
		})
	}
}