1
use sqlx::{Connection, PgConnection};
2

            
3
use super::{
4
    CLUSTER_PREFIX, RAM_DISK, check_socket_path, cluster_root, first_started, parse_major,
5
    port_is_free, ram_disk_has_room, server_options, start,
6
};
7

            
8
#[tokio::test]
9
1
async fn the_cluster_is_utf8_with_c_collation_like_prod() -> anyhow::Result<()> {
10
1
    let postgres = start().await?;
11
1
    let mut conn = PgConnection::connect(&postgres.database_url()).await?;
12
1
    let (encoding, collate): (String, String) = sqlx::query_as(
13
1
        "SELECT pg_encoding_to_char(encoding), datcollate FROM pg_database WHERE datname = current_database()",
14
1
    )
15
1
    .fetch_one(&mut conn)
16
1
    .await?;
17
1
    assert_eq!((encoding.as_str(), collate.as_str()), ("UTF8", "C"));
18
1
    let nbsp: String = sqlx::query_scalar("SELECT btrim(E'\\u00a0x\\u00a0', E'\\u00a0')")
19
1
        .fetch_one(&mut conn)
20
1
        .await?;
21
1
    assert_eq!(nbsp, "x");
22
2
    Ok(())
23
1
}
24

            
25
#[test]
26
1
fn a_held_port_is_not_free() -> anyhow::Result<()> {
27
1
    let held = std::net::TcpListener::bind(("127.0.0.1", 0))?;
28
1
    assert!(!port_is_free(held.local_addr()?.port()));
29
1
    Ok(())
30
1
}
31

            
32
#[test]
33
1
fn a_port_the_probe_rejects_gets_no_start_attempt() -> anyhow::Result<()> {
34
1
    let (taken, free) = (54_001, 54_002);
35
1
    let mut attempts = Vec::new();
36
1
    let chosen = first_started(
37
1
        [taken, free].into_iter(),
38
2
        |port| port != taken,
39
1
        |port| {
40
1
            attempts.push(port);
41
1
            Ok(true)
42
1
        },
43
    )?;
44
1
    assert_eq!((attempts, chosen), (vec![free], free));
45
1
    Ok(())
46
1
}
47

            
48
#[test]
49
1
fn a_failed_start_falls_through_to_the_next_free_port() -> anyhow::Result<()> {
50
3
    let chosen = first_started(1..4u16, |_| true, |port| Ok(port == 3))?;
51
1
    assert_eq!(chosen, 3);
52
1
    Ok(())
53
1
}
54

            
55
#[test]
56
1
fn no_free_port_and_no_working_server_are_told_apart() {
57
1
    let none_free = first_started(1..4u16, |_| false, |_| Ok(true)).map_err(|e| e.to_string());
58
3
    let none_start = first_started(1..4u16, |_| true, |_| Ok(false)).map_err(|e| e.to_string());
59
1
    assert!(none_free.is_err_and(|e| e.starts_with("no free port")));
60
1
    assert!(none_start.is_err_and(|e| e.starts_with("postgres failed to start")));
61
1
}
62

            
63
#[test]
64
1
fn the_version_banner_yields_its_major() -> anyhow::Result<()> {
65
1
    assert_eq!(parse_major("pg_ctl (PostgreSQL) 18.6\n")?, 18);
66
1
    assert_eq!(
67
1
        parse_major("postgres (PostgreSQL) 17.11 (Debian 17.11-1)\n")?,
68
1
        17
69
    );
70
1
    assert!(parse_major("garbage").is_err());
71
1
    assert!(parse_major("postgres (PostgreSQL) devel\n").is_err());
72
1
    Ok(())
73
1
}
74

            
75
#[test]
76
1
fn setup_durations_are_requested_only_from_postgres_18() {
77
1
    let socket = std::path::Path::new("/s");
78
1
    assert!(server_options(18, 54320, socket).contains("log_connections=setup_durations"));
79
1
    assert!(!server_options(17, 54320, socket).contains("log_connections"));
80
1
}
81

            
82
#[test]
83
1
fn an_overlong_socket_path_is_refused_up_front() {
84
1
    let long = std::path::PathBuf::from("/tmp").join("x".repeat(120));
85
1
    assert!(check_socket_path(&long).is_err_and(|e| e.to_string().contains("shorter TMPDIR")));
86
1
    assert!(check_socket_path(std::path::Path::new("/tmp/s")).is_ok());
87
1
}
88

            
89
#[tokio::test]
90
1
async fn the_cluster_logs_connection_setup_durations() -> anyhow::Result<()> {
91
1
    let postgres = start().await?;
92
1
    let mut conn = PgConnection::connect(&postgres.database_url()).await?;
93
1
    let setting: String = sqlx::query_scalar("SHOW log_connections")
94
1
        .fetch_one(&mut conn)
95
1
        .await?;
96
1
    let version: i32 = sqlx::query_scalar("SELECT current_setting('server_version_num')::int")
97
1
        .fetch_one(&mut conn)
98
1
        .await?;
99
1
    let expected = if version >= 180_000 {
100
        "setup_durations"
101
    } else {
102
1
        "off"
103
    };
104
1
    assert_eq!(setting, expected);
105
2
    Ok(())
106
1
}
107

            
108
#[test]
109
1
fn the_cluster_root_is_in_ram_exactly_when_the_ram_disk_has_room() -> anyhow::Result<()> {
110
1
    let root = cluster_root()?;
111
1
    let name = root
112
1
        .path()
113
1
        .file_name()
114
1
        .and_then(|name| name.to_str())
115
        .unwrap_or_default();
116
1
    assert!(name.starts_with(CLUSTER_PREFIX));
117
1
    assert_eq!(root.path().starts_with(RAM_DISK), ram_disk_has_room());
118
1
    Ok(())
119
1
}