-- Tests for roles reference table in a multinode/distributed environment
-- This file verifies that the roles table is properly replicated as a
-- reference table across all nodes in the cluster.

SET citus.next_shard_id TO 19840000;
SET documentdb.next_collection_id TO 19840;
SET documentdb.next_collection_index_id TO 19840;

SET search_path TO documentdb_core,documentdb_api_catalog,documentdb_api_internal,public;

-- =============================================================================
-- Test: Verify roles table exists and is a reference table
-- =============================================================================

-- Check that roles table exists
SELECT COUNT(*) > 0 AS roles_exists 
FROM pg_class c 
JOIN pg_namespace n ON c.relnamespace = n.oid 
WHERE n.nspname = 'documentdb_api_catalog' AND c.relname = 'roles';

-- Verify roles is distributed as a reference table (has entry in pg_dist_partition)
SELECT COUNT(*) > 0 AS is_distributed
FROM pg_dist_partition 
WHERE logicalrelid = 'documentdb_api_catalog.roles'::regclass;

-- Verify the distribution method is 'n' (reference table / none)
SELECT partmethod = 'n' AS is_reference_table
FROM pg_dist_partition 
WHERE logicalrelid = 'documentdb_api_catalog.roles'::regclass;

-- =============================================================================
-- Test: Verify roles table is replicated to worker nodes
-- =============================================================================

-- Check that roles has shards on all nodes (reference tables have one shard per node)
SELECT COUNT(*) AS shard_count
FROM pg_dist_shard
WHERE logicalrelid = 'documentdb_api_catalog.roles'::regclass;

-- Verify the shard placements exist (reference tables have placements on all nodes)
SELECT COUNT(*) > 0 AS has_placements
FROM pg_dist_shard s
JOIN pg_dist_shard_placement p ON s.shardid = p.shardid
WHERE s.logicalrelid = 'documentdb_api_catalog.roles'::regclass;
