atagen

malice

public
Star0
Watch0
a4555a7eae2dd3e0552c2d01390d93267dc1c296
2
ssh://brewery@pls.lobotomise.me:33/atagen/malice.git

Blame: crates/malice-worker/run.nu

973f8fbdaf80atagen2026-09-28 09:10 UTC1
# drives malice-worker's Host in a headless firefox shared worker against a throwaway
973f8fbdaf80atagen2026-09-28 09:10 UTC2
# continuwuity, `nix develop -c nu crates/malice-worker/run.nu`
973f8fbdaf80atagen2026-09-28 09:10 UTC3
973f8fbdaf80atagen2026-09-28 09:10 UTC4
const crate = path self .
973f8fbdaf80atagen2026-09-28 09:10 UTC5
const root = path self ../..
973f8fbdaf80atagen2026-09-28 09:10 UTC6
const username = "malice-harness"
973f8fbdaf80atagen2026-09-28 09:10 UTC7
const password = "malice-harness-password"
973f8fbdaf80atagen2026-09-28 09:10 UTC8
const room_name = "malice-harness"
973f8fbdaf80atagen2026-09-28 09:10 UTC9
const seeded = "seeded by run.nu"
973f8fbdaf80atagen2026-09-28 09:10 UTC10
973f8fbdaf80atagen2026-09-28 09:10 UTC11
# the shared build wrapper sets CARGO_TARGET_DIR, the nix sandbox does not
973f8fbdaf80atagen2026-09-28 09:10 UTC12
def target [] { $env.CARGO_TARGET_DIR? | default ($root | path join target) }
973f8fbdaf80atagen2026-09-28 09:10 UTC13
973f8fbdaf80atagen2026-09-28 09:10 UTC14
def scratch [] { target | path join harness }
973f8fbdaf80atagen2026-09-28 09:10 UTC15
973f8fbdaf80atagen2026-09-28 09:10 UTC16
def start-homeserver [] {
973f8fbdaf80atagen2026-09-28 09:10 UTC17
    let dir = scratch
973f8fbdaf80atagen2026-09-28 09:10 UTC18
    rm -rf $dir
973f8fbdaf80atagen2026-09-28 09:10 UTC19
    mkdir $dir
973f8fbdaf80atagen2026-09-28 09:10 UTC20
    let port = 6267
973f8fbdaf80atagen2026-09-28 09:10 UTC21
    let config = $dir | path join homeserver.toml
973f8fbdaf80atagen2026-09-28 09:10 UTC22
    let log = $dir | path join homeserver.log
973f8fbdaf80atagen2026-09-28 09:10 UTC23
    {
973f8fbdaf80atagen2026-09-28 09:10 UTC24
        global: {
973f8fbdaf80atagen2026-09-28 09:10 UTC25
            server_name: "localhost"
973f8fbdaf80atagen2026-09-28 09:10 UTC26
            database_path: ($dir | path join db)
973f8fbdaf80atagen2026-09-28 09:10 UTC27
            address: ["127.0.0.1"]
973f8fbdaf80atagen2026-09-28 09:10 UTC28
            port: $port
973f8fbdaf80atagen2026-09-28 09:10 UTC29
            allow_federation: false
973f8fbdaf80atagen2026-09-28 09:10 UTC30
            new_user_displayname_suffix: ""
973f8fbdaf80atagen2026-09-28 09:10 UTC31
        }
973f8fbdaf80atagen2026-09-28 09:10 UTC32
    } | to toml | save $config
973f8fbdaf80atagen2026-09-28 09:10 UTC33
    # the first account otherwise needs a one-time token that is only ever printed to the log
973f8fbdaf80atagen2026-09-28 09:10 UTC34
    let create = $"users create-user ($username) ($password)"
973f8fbdaf80atagen2026-09-28 09:10 UTC35
    let pid = ^bash -c 'conduwuit -c "$1" --execute "$2" > "$3" 2>&1 & echo $!' _ $config $create $log | str trim
973f8fbdaf80atagen2026-09-28 09:10 UTC36
    let url = $'http://127.0.0.1:($port)'
973f8fbdaf80atagen2026-09-28 09:10 UTC37
    for _ in 1..100 {
973f8fbdaf80atagen2026-09-28 09:10 UTC38
        if (try { login $url | ignore; true } catch { false }) {
973f8fbdaf80atagen2026-09-28 09:10 UTC39
            return { pid: $pid, url: $url }
973f8fbdaf80atagen2026-09-28 09:10 UTC40
        }
973f8fbdaf80atagen2026-09-28 09:10 UTC41
        sleep 100ms
973f8fbdaf80atagen2026-09-28 09:10 UTC42
    }
973f8fbdaf80atagen2026-09-28 09:10 UTC43
    try { stop $pid }
973f8fbdaf80atagen2026-09-28 09:10 UTC44
    print (open --raw $log | lines | last 20 | str join "\n")
973f8fbdaf80atagen2026-09-28 09:10 UTC45
    error make { msg: "homeserver did not come up" }
973f8fbdaf80atagen2026-09-28 09:10 UTC46
}
973f8fbdaf80atagen2026-09-28 09:10 UTC47
973f8fbdaf80atagen2026-09-28 09:10 UTC48
def login [url: string] {
973f8fbdaf80atagen2026-09-28 09:10 UTC49
    let body = { type: "m.login.password", identifier: { type: "m.id.user", user: $username }, password: $password }
973f8fbdaf80atagen2026-09-28 09:10 UTC50
    http post --content-type application/json $'($url)/_matrix/client/v3/login' $body
973f8fbdaf80atagen2026-09-28 09:10 UTC51
}
973f8fbdaf80atagen2026-09-28 09:10 UTC52
973f8fbdaf80atagen2026-09-28 09:10 UTC53
def seed [url: string] {
973f8fbdaf80atagen2026-09-28 09:10 UTC54
    let auth = [Authorization $'Bearer ((login $url).access_token)']
973f8fbdaf80atagen2026-09-28 09:10 UTC55
    let room = http post --headers $auth --content-type application/json $'($url)/_matrix/client/v3/createRoom' { name: $room_name }
973f8fbdaf80atagen2026-09-28 09:10 UTC56
    let room_id = $room.room_id | url encode --all
973f8fbdaf80atagen2026-09-28 09:10 UTC57
    let message = { msgtype: "m.text", body: $seeded }
973f8fbdaf80atagen2026-09-28 09:10 UTC58
    http put --headers $auth --content-type application/json $'($url)/_matrix/client/v3/rooms/($room_id)/send/m.room.message/seed' $message | ignore
973f8fbdaf80atagen2026-09-28 09:10 UTC59
}
973f8fbdaf80atagen2026-09-28 09:10 UTC60
973f8fbdaf80atagen2026-09-28 09:10 UTC61
def stop [pid: string] {
973f8fbdaf80atagen2026-09-28 09:10 UTC62
    ^kill -9 $pid
973f8fbdaf80atagen2026-09-28 09:10 UTC63
}
973f8fbdaf80atagen2026-09-28 09:10 UTC64
973f8fbdaf80atagen2026-09-28 09:10 UTC65
def main [] {
973f8fbdaf80atagen2026-09-28 09:10 UTC66
    let server = start-homeserver
973f8fbdaf80atagen2026-09-28 09:10 UTC67
    let result = try {
973f8fbdaf80atagen2026-09-28 09:10 UTC68
        let versions = http get $'($server.url)/_matrix/client/versions'
973f8fbdaf80atagen2026-09-28 09:10 UTC69
        if not ($versions.unstable_features | get -o 'org.matrix.simplified_msc3575' | default false) {
973f8fbdaf80atagen2026-09-28 09:10 UTC70
            error make { msg: "homeserver does not advertise simplified sliding sync" }
973f8fbdaf80atagen2026-09-28 09:10 UTC71
        }
973f8fbdaf80atagen2026-09-28 09:10 UTC72
        seed $server.url
973f8fbdaf80atagen2026-09-28 09:10 UTC73
        cd $crate
973f8fbdaf80atagen2026-09-28 09:10 UTC74
        with-env {
973f8fbdaf80atagen2026-09-28 09:10 UTC75
            MALICE_HOMESERVER: $server.url
973f8fbdaf80atagen2026-09-28 09:10 UTC76
            MALICE_USERNAME: $username
973f8fbdaf80atagen2026-09-28 09:10 UTC77
            MALICE_PASSWORD: $password
973f8fbdaf80atagen2026-09-28 09:10 UTC78
            MALICE_ROOM_NAME: $room_name
973f8fbdaf80atagen2026-09-28 09:10 UTC79
            MALICE_SEEDED: $seeded
973f8fbdaf80atagen2026-09-28 09:10 UTC80
            GECKODRIVER: (which geckodriver | get 0.path)
973f8fbdaf80atagen2026-09-28 09:10 UTC81
            WASM_BINDGEN_TEST_TIMEOUT: "180"
973f8fbdaf80atagen2026-09-28 09:10 UTC82
        } {
973f8fbdaf80atagen2026-09-28 09:10 UTC83
            ^cargo test --target wasm32-unknown-unknown --test host
973f8fbdaf80atagen2026-09-28 09:10 UTC84
        }
973f8fbdaf80atagen2026-09-28 09:10 UTC85
        null
973f8fbdaf80atagen2026-09-28 09:10 UTC86
    } catch {|error| $error }
973f8fbdaf80atagen2026-09-28 09:10 UTC87
    stop $server.pid
973f8fbdaf80atagen2026-09-28 09:10 UTC88
    if $result != null {
973f8fbdaf80atagen2026-09-28 09:10 UTC89
        print $'homeserver log at (scratch)/homeserver.log'
973f8fbdaf80atagen2026-09-28 09:10 UTC90
        error make { msg: $result.msg }
973f8fbdaf80atagen2026-09-28 09:10 UTC91
    }
973f8fbdaf80atagen2026-09-28 09:10 UTC92
}