diff --git a/extensions/discord/gleam.toml b/extensions/discord/gleam.toml index f7a8a63a2..aacf1d7c2 100644 --- a/extensions/discord/gleam.toml +++ b/extensions/discord/gleam.toml @@ -26,3 +26,5 @@ albedo_sdk = { path = "../../packages/albedo_sdk" } [dev_dependencies] gleeunit = ">= 1.0.0 and < 2.0.0" +qcheck = ">= 1.0.5 and < 2.0.0" +albedo_property = { path = "../../packages/albedo_property" } diff --git a/extensions/discord/manifest.toml b/extensions/discord/manifest.toml index caa8d0fe8..780d474b9 100644 --- a/extensions/discord/manifest.toml +++ b/extensions/discord/manifest.toml @@ -20,6 +20,7 @@ packages = [ { name = "albedo_openai", version = "0.1.0", build_tools = ["gleam"], requirements = ["albedo_bytes", "albedo_http", "albedo_json", "albedo_model", "albedo_model_stream", "albedo_sse", "gleam_json", "gleam_stdlib"], source = "local", path = "../../packages/albedo_openai" }, { name = "albedo_ports", version = "0.1.0", build_tools = ["gleam"], requirements = ["albedo_clock", "albedo_protect", "gleam_erlang", "gleam_stdlib"], source = "local", path = "../../packages/albedo_ports" }, { name = "albedo_process", version = "0.1.0", build_tools = ["gleam"], requirements = ["albedo_clock", "albedo_protect", "gleam_erlang", "gleam_otp", "gleam_stdlib"], source = "local", path = "../../packages/albedo_process" }, + { name = "albedo_property", version = "0.1.0", build_tools = ["gleam"], requirements = ["albedo_clock", "albedo_env", "albedo_protect", "gleam_erlang", "gleam_stdlib", "qcheck"], source = "local", path = "../../packages/albedo_property" }, { name = "albedo_protect", version = "0.1.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], source = "local", path = "../../packages/albedo_protect" }, { name = "albedo_sdk", version = "0.1.0", build_tools = ["gleam"], requirements = ["albedo_bytes", "albedo_clock", "albedo_env", "albedo_file", "albedo_http", "albedo_image", "albedo_json", "albedo_model", "albedo_model_stream", "albedo_oauth", "albedo_openai", "albedo_ports", "albedo_process", "albedo_protect", "albedo_sqlite", "albedo_web", "gleam_crypto", "gleam_erlang", "gleam_http", "gleam_json", "gleam_otp", "gleam_stdlib", "mist", "sqlight", "yamerl"], source = "local", path = "../../packages/albedo_sdk" }, { name = "albedo_sqlite", version = "0.1.0", build_tools = ["gleam"], requirements = ["albedo_clock", "albedo_process", "esqlite", "gleam_erlang", "gleam_otp", "gleam_stdlib", "sqlight"], source = "local", path = "../../packages/albedo_sqlite" }, @@ -32,13 +33,17 @@ packages = [ { name = "gleam_http", version = "4.4.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "F065CE9941A7756ABBAEAE7F9F32A96CAD29DA3311A381DB2D926EA8E8C156DC" }, { name = "gleam_json", version = "3.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "44FDAA8847BE8FC48CA7A1C089706BD54BADCC4C45B237A992EDDF9F2CDB2836" }, { name = "gleam_otp", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "DE4CA6850842F0266EE95317A25DD6A0A0F20CDFAB7C0ADC2E63251D7C3C72EC" }, + { name = "gleam_regexp", version = "1.1.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_regexp", source = "hex", outer_checksum = "9C215C6CA84A5B35BB934A9B61A9A306EC743153BE2B0425A0D032E477B062A9" }, { name = "gleam_stdlib", version = "1.0.5", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "CEE5B6C076A85B45F60C585F4316C63EC8B7127C119D5738C3958A9C4D50404E" }, + { name = "gleam_yielder", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_yielder", source = "hex", outer_checksum = "8E4E4ECFA7982859F430C57F549200C7749823C106759F4A19A78AEA6687717A" }, { name = "gleeunit", version = "1.11.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "EC31ABA74256AEA531EDF8169931D775BBB384FED0A8A1BDC4DD9354E3E21826" }, { name = "glisten", version = "9.0.1", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_otp", "gleam_stdlib", "logging"], otp_app = "glisten", source = "hex", outer_checksum = "7795AA50830656F3A0316A6B26595F893C83272DA901B3405E31339CAA31A10B" }, { name = "gramps", version = "6.0.1", build_tools = ["gleam"], requirements = ["gleam_crypto", "gleam_erlang", "gleam_http", "gleam_stdlib"], otp_app = "gramps", source = "hex", outer_checksum = "D55636072DEE173F6586A5679D3C02EC7A0DE3F8646B78C351B72908FF223DF7" }, { name = "hpack_erl", version = "0.3.0", build_tools = ["rebar3"], requirements = [], otp_app = "hpack", source = "hex", outer_checksum = "D6137D7079169D8C485C6962DFE261AF5B9EF60FBC557344511C1E65E3D95FB0" }, { name = "logging", version = "1.5.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "logging", source = "hex", outer_checksum = "BC5F18CE5DD9686100229FE5409BDC3DD5C46D5A7DF2F804AD2D8F0DD6C5060E" }, { name = "mist", version = "6.0.3", build_tools = ["gleam"], requirements = ["exception", "gleam_erlang", "gleam_http", "gleam_otp", "gleam_stdlib", "glisten", "gramps", "hpack_erl", "logging"], source = "local", path = "../../vendor/mist" }, + { name = "prng", version = "6.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "prng", source = "hex", outer_checksum = "7FE3879E13150248BD370469205ED07B9404DD90670FEBB8CD0E7643707C420E" }, + { name = "qcheck", version = "1.0.5", build_tools = ["gleam"], requirements = ["exception", "gleam_regexp", "gleam_stdlib", "gleam_yielder", "prng"], otp_app = "qcheck", source = "hex", outer_checksum = "D27C12074776EC95F082EE56E0FC74685506B6EE26176F194D10F0322A39AC11" }, { name = "sqlight", version = "1.2.0", build_tools = ["gleam"], requirements = ["esqlite", "gleam_stdlib"], otp_app = "sqlight", source = "hex", outer_checksum = "841768D0A81107EE2DB46B949354F284A68B2E8098315C28A1FE3C07D206C17A" }, { name = "yamerl", version = "0.10.0", build_tools = ["rebar3"], requirements = [], otp_app = "yamerl", source = "hex", outer_checksum = "346ADB2963F1051DC837A2364E4ACF6EB7D80097C0F53CBDC3046EC8EC4B4E6E" }, ] @@ -50,6 +55,7 @@ albedo_http = { path = "../../packages/albedo_http" } albedo_json = { path = "../../packages/albedo_json" } albedo_ports = { path = "../../packages/albedo_ports" } albedo_process = { path = "../../packages/albedo_process" } +albedo_property = { path = "../../packages/albedo_property" } albedo_protect = { path = "../../packages/albedo_protect" } albedo_sdk = { path = "../../packages/albedo_sdk" } gleam_erlang = { version = ">= 1.3.0 and < 2.0.0" } @@ -59,4 +65,5 @@ gleam_otp = { version = ">= 1.3.0 and < 2.0.0" } gleam_stdlib = { version = ">= 1.0.0 and < 2.0.0" } gleeunit = { version = ">= 1.0.0 and < 2.0.0" } mist = { path = "../../vendor/mist" } +qcheck = { version = ">= 1.0.5 and < 2.0.0" } sqlight = { version = ">= 1.2.0 and < 2.0.0" } diff --git a/extensions/discord/test/albedo_ext_discord/refs_test.gleam b/extensions/discord/test/albedo_ext_discord/refs_test.gleam new file mode 100644 index 000000000..7c23917a2 --- /dev/null +++ b/extensions/discord/test/albedo_ext_discord/refs_test.gleam @@ -0,0 +1,214 @@ +//// Channel handles and the names a model writes for them, against a model. +//// The defects: a subscription label written when the channel was set up +//// putting back a name Discord has since renamed (#296); a configured label +//// not filling a name nothing has observed; a handle renumbered or reused; +//// and a name lookup that misses a channel, ignores case or the `#`, or +//// picks one of two channels that share a name. Operation histories run on +//// the extension's own tables in an in-memory database. + +import albedo_ext_discord/refs +import albedo_property +import gleam/dict.{type Dict} +import gleam/int +import gleam/list +import gleam/option.{type Option, None, Some} +import gleam/string +import qcheck.{type Generator} +import sqlight + +const bot = "bot-1" + +type Op { + /// A message or reaction reached the bot with these names. + Observe(channel: Int, name: Option(String), guild: Option(String)) + /// The subscription's saved labels, read when channels are listed. + Configure(channel: Int, name: Option(String), guild: Option(String)) + /// The model wrote a channel by name. + Lookup(written: String) +} + +type Entry { + Entry(ref: Int, name: Option(String), guild: Option(String)) +} + +pub fn handles_and_names_follow_their_model_test() -> Nil { + use ops <- albedo_property.run( + 300, + albedo_property.sequence( + [#(3, observe()), #(3, configure()), #(2, lookup())], + 1, + 16, + ), + ) + use connection <- sqlight.with_connection(":memory:") + let assert Ok(Nil) = sqlight.exec(refs.schema, connection) + list.fold(ops, dict.new(), fn(model, op) { + case op { + Observe(channel, name, guild) -> { + let model = record(model, channel, name, guild, True) + let assert Ok(got) = + refs.channel(connection, bot, snowflake(channel), name, guild) + assert got == expected(model, channel) + as { "after " <> string.inspect(op) } + model + } + Configure(channel, name, guild) -> { + let model = record(model, channel, name, guild, False) + let assert Ok(got) = + refs.configured_channel( + connection, + bot, + snowflake(channel), + name, + guild, + ) + assert got == expected(model, channel) + as { "after " <> string.inspect(op) } + model + } + Lookup(written) -> { + let assert Ok(got) = refs.resolve_channel(connection, bot, written) + assert resolved(got) == lookup_model(model, written) + as { "after " <> string.inspect(op) } + model + } + } + }) + Nil +} + +/// A first sighting numbers the channel after the highest handle. An observed +/// name replaces the recorded one; a configured label only fills a missing one. +fn record( + model: Dict(Int, Entry), + channel: Int, + name: Option(String), + guild: Option(String), + observed: Bool, +) -> Dict(Int, Entry) { + let entry = case dict.get(model, channel) { + Error(Nil) -> Entry(dict.size(model) + 1, name, guild) + Ok(entry) -> + case observed { + True -> + Entry( + entry.ref, + option.or(name, entry.name), + option.or(guild, entry.guild), + ) + False -> + Entry( + entry.ref, + option.or(entry.name, name), + option.or(entry.guild, guild), + ) + } + } + dict.insert(model, channel, entry) +} + +fn expected(model: Dict(Int, Entry), channel: Int) -> refs.Channel { + let assert Ok(entry) = dict.get(model, channel) + refs.Channel(entry.ref, entry.name, entry.guild) +} + +/// What a written name means: the channels recorded under it, ignoring case +/// and a leading `#`, as handles in handle order. +type Meaning { + Nothing + One(String) + Several(List(String)) +} + +fn lookup_model(model: Dict(Int, Entry), written: String) -> Meaning { + let wanted = string.lowercase(written) |> drop_hash + let matches = + dict.to_list(model) + |> list.filter(fn(pair) { + option.map({ pair.1 }.name, string.lowercase) == Some(wanted) + }) + |> list.sort(fn(a, b) { int.compare({ a.1 }.ref, { b.1 }.ref) }) + case matches { + [] -> Nothing + [#(channel, _)] -> One(snowflake(channel)) + many -> Several(list.map(many, fn(pair) { refs.handle({ pair.1 }.ref) })) + } +} + +fn resolved(got: refs.Resolved) -> Meaning { + case got { + refs.Unknown -> Nothing + refs.Found(id) -> One(id) + // Each choice is a handle and the names it carries: `c2 (#tool in Lab)`. + refs.Ambiguous(choices) -> + Several( + list.map(choices, fn(choice) { + case string.split_once(choice, " ") { + Ok(#(handle, _)) -> handle + Error(Nil) -> choice + } + }), + ) + } +} + +fn drop_hash(text: String) -> String { + case text { + "#" <> rest -> rest + _ -> text + } +} + +fn snowflake(channel: Int) -> String { + "30000000000000000" <> int.to_string(channel) +} + +// Three channels and three names, so renames, shared names and missing +// names all occur. +fn channel() -> Generator(Int) { + qcheck.bounded_int(0, 2) +} + +fn name() -> Generator(Option(String)) { + qcheck.from_weighted_generators(#(1, qcheck.return(None)), [ + #(3, qcheck.map(pick(["tool", "general", "renamed"]), Some)), + ]) +} + +fn guild() -> Generator(Option(String)) { + qcheck.from_weighted_generators(#(1, qcheck.return(None)), [ + #(3, qcheck.map(pick(["Lab", "Home"]), Some)), + ]) +} + +fn observe() -> Generator(Op) { + use channel <- qcheck.bind(channel()) + use name <- qcheck.bind(name()) + use guild <- qcheck.map(guild()) + Observe(channel, name, guild) +} + +fn configure() -> Generator(Op) { + use channel <- qcheck.bind(channel()) + use name <- qcheck.bind(name()) + use guild <- qcheck.map(guild()) + Configure(channel, name, guild) +} + +fn lookup() -> Generator(Op) { + use name <- qcheck.bind(pick(["tool", "general", "renamed", "missing"])) + use form <- qcheck.map(qcheck.bounded_int(0, 2)) + Lookup(case form { + 0 -> name + 1 -> "#" <> name + _ -> string.uppercase(name) + }) +} + +fn pick(choices: List(String)) -> Generator(String) { + qcheck.bounded_int(0, list.length(choices) - 1) + |> qcheck.map(fn(index) { + let assert Ok(choice) = list.drop(choices, index) |> list.first + choice + }) +}