atagen

malice

public
Star0
Watch0
d07bca5da4ae8096444c8411d8ca0c4bf01dee66
2
ssh://brewery@pls.lobotomise.me:33/atagen/malice.git
crates/malice-worker/run.nu92 lines3.3 KB
1
# drives malice-worker's Host in a headless firefox shared worker against a throwaway
2
# continuwuity, `nix develop -c nu crates/malice-worker/run.nu`
3
4
const crate = path self .
5
const root = path self ../..
6
const username = "malice-harness"
7
const password = "malice-harness-password"
8
const room_name = "malice-harness"
9
const seeded = "seeded by run.nu"
10
11
# the shared build wrapper sets CARGO_TARGET_DIR, the nix sandbox does not
12
def target [] { $env.CARGO_TARGET_DIR? | default ($root | path join target) }
13
14
def scratch [] { target | path join harness }
15
16
def start-homeserver [] {
17
    let dir = scratch
18
    rm -rf $dir
19
    mkdir $dir
20
    let port = 6267
21
    let config = $dir | path join homeserver.toml
22
    let log = $dir | path join homeserver.log
23
    {
24
        global: {
25
            server_name: "localhost"
26
            database_path: ($dir | path join db)
27
            address: ["127.0.0.1"]
28
            port: $port
29
            allow_federation: false
30
            new_user_displayname_suffix: ""
31
        }
32
    } | to toml | save $config
33
    # the first account otherwise needs a one-time token that is only ever printed to the log
34
    let create = $"users create-user ($username) ($password)"
35
    let pid = ^bash -c 'conduwuit -c "$1" --execute "$2" > "$3" 2>&1 & echo $!' _ $config $create $log | str trim
36
    let url = $'http://127.0.0.1:($port)'
37
    for _ in 1..100 {
38
        if (try { login $url | ignore; true } catch { false }) {
39
            return { pid: $pid, url: $url }
40
        }
41
        sleep 100ms
42
    }
43
    try { stop $pid }
44
    print (open --raw $log | lines | last 20 | str join "\n")
45
    error make { msg: "homeserver did not come up" }
46
}
47
48
def login [url: string] {
49
    let body = { type: "m.login.password", identifier: { type: "m.id.user", user: $username }, password: $password }
50
    http post --content-type application/json $'($url)/_matrix/client/v3/login' $body
51
}
52
53
def seed [url: string] {
54
    let auth = [Authorization $'Bearer ((login $url).access_token)']
55
    let room = http post --headers $auth --content-type application/json $'($url)/_matrix/client/v3/createRoom' { name: $room_name }
56
    let room_id = $room.room_id | url encode --all
57
    let message = { msgtype: "m.text", body: $seeded }
58
    http put --headers $auth --content-type application/json $'($url)/_matrix/client/v3/rooms/($room_id)/send/m.room.message/seed' $message | ignore
59
}
60
61
def stop [pid: string] {
62
    ^kill -9 $pid
63
}
64
65
def main [] {
66
    let server = start-homeserver
67
    let result = try {
68
        let versions = http get $'($server.url)/_matrix/client/versions'
69
        if not ($versions.unstable_features | get -o 'org.matrix.simplified_msc3575' | default false) {
70
            error make { msg: "homeserver does not advertise simplified sliding sync" }
71
        }
72
        seed $server.url
73
        cd $crate
74
        with-env {
75
            MALICE_HOMESERVER: $server.url
76
            MALICE_USERNAME: $username
77
            MALICE_PASSWORD: $password
78
            MALICE_ROOM_NAME: $room_name
79
            MALICE_SEEDED: $seeded
80
            GECKODRIVER: (which geckodriver | get 0.path)
81
            WASM_BINDGEN_TEST_TIMEOUT: "180"
82
        } {
83
            ^cargo test --target wasm32-unknown-unknown --test host
84
        }
85
        null
86
    } catch {|error| $error }
87
    stop $server.pid
88
    if $result != null {
89
        print $'homeserver log at (scratch)/homeserver.log'
90
        error make { msg: $result.msg }
91
    }
92
}