package app

import (
	"context"
	"os"
	"testing"
	"time"

	"github.com/stretchr/testify/require"
)

func TestListRealtimeRoomIDsForChannelUsesScopeTopology(t *testing.T) {
	t.Parallel()

	source, err := os.ReadFile("app_ws.go")
	require.NoError(t, err)
	require.Contains(t, string(source), "JOIN scopes child")

	ctx := context.Background()
	pool := startAppRollbackDB(t, ctx)
	store := New(pool, Options{})
	now := time.Date(2026, 5, 14, 8, 46, 0, 0, time.UTC)
	fixture := seedAppAdminFixture(t, pool, "ws_scope_rooms", now)

	mustExec(t, pool, `
INSERT INTO scopes (
    id, workspace_id, type, parent_scope_id, root_message_id, root_message_short_id,
    identity_key, latest_seq, latest_global_seq, latest_message_at, archived_until_seq,
    status, created_at, updated_at, archived_at
)
VALUES
    ('thr_ws_scope_room_a', $1, 'thread', $2, 'msg_ws_scope_root_a', 'roota', 'thread:thr_ws_scope_room_a', 0, 0, NULL, 0, 'active', $3, $3, NULL),
    ('thr_ws_scope_room_b', $1, 'thread', $2, 'msg_ws_scope_root_b', 'rootb', 'thread:thr_ws_scope_room_b', 0, 0, NULL, 0, 'active', $3, $3, NULL),
    ('thr_ws_scope_room_deleted', $1, 'thread', $2, 'msg_ws_scope_root_deleted', 'rootd', 'thread:thr_ws_scope_room_deleted', 0, 0, NULL, 0, 'deleted', $3, $3, NULL),
    ('thr_ws_scope_room_archived', $1, 'thread', $2, 'msg_ws_scope_root_archived', 'rootc', 'thread:thr_ws_scope_room_archived', 0, 0, NULL, 0, 'active', $3, $3, $3)
`, fixture.WorkspaceID, fixture.AllChannelID, now)

	rooms, err := store.ListRealtimeRoomIDsForChannel(ctx, fixture.WorkspaceID, fixture.AllChannelID)
	require.NoError(t, err)
	require.Equal(t, []string{fixture.AllChannelID, "thr_ws_scope_room_a", "thr_ws_scope_room_b"}, rooms)
}
