diff --git a/Cargo.lock b/Cargo.lock index ba1f99a2..f03bdf54 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -243,7 +243,7 @@ checksum = "4f8abc12baad266b1c8cec146854c195b5864b4221d4b2ca7296a7ae82d9e451" dependencies = [ "proc-macro2", "quote", - "syn 3.0.9", + "syn 3.0.10", ] [[package]] @@ -278,7 +278,7 @@ checksum = "82f6aeea286b8eb4dd3431a1be1b59d290ace00f5bfd8e2a159bc2a05e2c1667" dependencies = [ "proc-macro2", "quote", - "syn 3.0.9", + "syn 3.0.10", ] [[package]] @@ -390,7 +390,16 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "08807e080ed7f9d5433fa9b275196cfc35414f66a0c79d864dc51a0d825231a3" dependencies = [ - "bit-vec", + "bit-vec 0.8.0", +] + +[[package]] +name = "bit-set" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56d87354e4229f54a44f7bf2435906a4656dba36026ab6eaca629a2c436a691c" +dependencies = [ + "bit-vec 0.10.2", ] [[package]] @@ -399,6 +408,12 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5e764a1d40d510daf35e07be9eb06e75770908c27d411ee6c92109c9840eaaf7" +[[package]] +name = "bit-vec" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72503af1e72be411d74ca45cd6b309878fcb35354e252fc2a3a8bda3a97082a0" + [[package]] name = "bitflags" version = "1.3.2" @@ -621,9 +636,9 @@ dependencies = [ [[package]] name = "brotli-decompressor" -version = "6.3.0" +version = "6.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97d9071e5b45944c5a9c06baf13a7a65ffa2c88c204015967852c90869692c05" +checksum = "75c74c9c51b9140663e196b5fb88bc94c334d7a497c8311cb759d8557874747f" dependencies = [ "alloc-no-stdlib", "alloc-stdlib", @@ -667,7 +682,7 @@ checksum = "6a1f896587b6f2c069c73d2f0913e2d590c3990285cd2f0b6aa02b786b4c679c" dependencies = [ "proc-macro2", "quote", - "syn 3.0.9", + "syn 3.0.10", ] [[package]] @@ -878,7 +893,7 @@ dependencies = [ "heck 0.5.0", "proc-macro2", "quote", - "syn 3.0.9", + "syn 3.0.10", ] [[package]] @@ -938,9 +953,9 @@ dependencies = [ [[package]] name = "const-oid" -version = "0.10.2" +version = "0.10.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a6ef517f0926dd24a1582492c791b6a4818a4d94e789a334894aa15b0d12f55c" +checksum = "be95d4a884358a21a199689323c29e9256b4a1b315e22f183d155422041d3a9a" [[package]] name = "cookie" @@ -1028,9 +1043,9 @@ dependencies = [ [[package]] name = "crc32fast" -version = "1.5.4" +version = "1.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f97d3416d5337d15c4b0d78c8c91df922ca143f57ec20e2d1c188333047ef04" +checksum = "2b929dd2a135771f40afabd1073f85eed59c800b7c61901e5c254270497dad64" dependencies = [ "cfg-if", ] @@ -1083,6 +1098,19 @@ dependencies = [ "smallvec", ] +[[package]] +name = "cssparser" +version = "0.38.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11119743ad110e8c1bdccd930d7f5c30c99e5fc76a7b63ec9807e84eef0c5f59" +dependencies = [ + "cssparser-macros", + "dtoa-short", + "itoa", + "phf 0.14.0", + "smallvec", +] + [[package]] name = "cssparser-macros" version = "0.7.1" @@ -1090,7 +1118,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d045de693cb712d0b22c6a64be5b953f67b3ce00ab5ad3dd5d8b441886ab8e1a" dependencies = [ "quote", - "syn 3.0.9", + "syn 3.0.10", ] [[package]] @@ -1128,7 +1156,7 @@ dependencies = [ "proc-macro2", "quote", "strsim", - "syn 3.0.9", + "syn 3.0.10", ] [[package]] @@ -1139,7 +1167,7 @@ checksum = "2ac7135c3ef02b2f7833bbeb1be5ba7f966dcde8a87c6b87f65a778d71a02785" dependencies = [ "darling_core", "quote", - "syn 3.0.9", + "syn 3.0.10", ] [[package]] @@ -1191,37 +1219,6 @@ dependencies = [ "zeroize", ] -[[package]] -name = "defmt" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e4a0795ac740cb54d7b3775c9c17345f6aaecf884c01733b39d6017738e0e3e1" -dependencies = [ - "bitflags 1.3.2", - "defmt-macros", -] - -[[package]] -name = "defmt-macros" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "840bb056bd99c495c9188198a258ab787c79f306d864db3e6e8451442d6d8366" -dependencies = [ - "defmt-parser", - "proc-macro2", - "quote", - "syn 3.0.9", -] - -[[package]] -name = "defmt-parser" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "71958f69fb2de856c5dd51ac9377e986ca1aa5919f931bf5395a807277f889a0" -dependencies = [ - "thiserror 2.0.21", -] - [[package]] name = "deranged" version = "0.5.9" @@ -1325,7 +1322,7 @@ checksum = "c6232dd377dcc64799954cbd3a9bb882e9cdc1308ccd87b1c098f1fb2eaf82a8" dependencies = [ "proc-macro2", "quote", - "syn 3.0.9", + "syn 3.0.10", ] [[package]] @@ -1357,12 +1354,27 @@ version = "0.28.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fac5fca71e65e94cc718a6e2af65d6e0f9c6027751c2aa562fbb5087fda639bc" dependencies = [ - "bit-set", - "cssparser", + "bit-set 0.8.0", + "cssparser 0.37.0", "foldhash 0.2.0", - "html5ever", + "html5ever 0.39.0", "precomputed-hash", - "selectors", + "selectors 0.38.0", + "tendril", +] + +[[package]] +name = "dom_query" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "498c698ac51127148a3984450e51fca7f01805a91d7a1f7a24af0a5f761e3385" +dependencies = [ + "bit-set 0.11.1", + "cssparser 0.38.0", + "foldhash 0.2.0", + "html5ever 0.40.1", + "precomputed-hash", + "selectors 0.41.0", "tendril", ] @@ -1531,7 +1543,7 @@ version = "0.19.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d301f5bf187b3c295fce6468d3875037a0bccc5f6b151c63cac2f85babf21912" dependencies = [ - "bit-set", + "bit-set 0.8.0", "regex-automata", "regex-syntax", ] @@ -1585,9 +1597,9 @@ checksum = "1c66915cc5391097de7a2b0b972e8505aa33c0df401fd3a6811dbdb32881ba48" [[package]] name = "fixedbitset" -version = "0.5.7" +version = "0.5.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d674e81391d1e1ab681a28d99df07927c6d4aa5b027d7da16ba32d1d21ecd99" +checksum = "5905ec755cc8386d7928353e4d356d2be3e89e81e5ca01fe630a1ca4d08dc175" [[package]] name = "flate2" @@ -1658,7 +1670,7 @@ checksum = "ea5190182e6915eb873ddbc16e23b711b6eb1f9c00a0d0a3a91b5f6228475225" dependencies = [ "proc-macro2", "quote", - "syn 3.0.9", + "syn 3.0.10", ] [[package]] @@ -1739,7 +1751,7 @@ checksum = "9fb9654ba8355388abeb8dcb4fc62f511300867002afc858860463bdd9fe0c44" dependencies = [ "proc-macro2", "quote", - "syn 3.0.9", + "syn 3.0.10", ] [[package]] @@ -2186,7 +2198,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "46a1761807faccc9a19e86944bbf40610014066306f96edcdedc2fb714bcb7b8" dependencies = [ "log", - "markup5ever", + "markup5ever 0.39.0", +] + +[[package]] +name = "html5ever" +version = "0.40.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "456a1a377e608e555d22ddab27ac0114bc7a7b4199078108e34c2aeae6c9b130" +dependencies = [ + "log", + "markup5ever 0.40.0", + "memchr", ] [[package]] @@ -2692,9 +2715,9 @@ dependencies = [ [[package]] name = "itoa" -version = "1.0.18" +version = "1.0.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" +checksum = "3bd1574e617a9a6c1f8f8181bd7e841e1f20e4277607ef28945765188d6284d4" [[package]] name = "javascriptcore-rs" @@ -2721,11 +2744,10 @@ dependencies = [ [[package]] name = "jiff" -version = "0.2.39" +version = "0.2.42" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ac703058abac89e5c39b0b879dce2b6b14a4ba36600c7e7e3d0c60a6883ef3c" +checksum = "fefca06093eeaed707a905795e68206da0edca4edef23d2a3997a3ddbee462aa" dependencies = [ - "defmt", "jiff-core", "jiff-static", "jiff-tzdb-platform", @@ -2738,19 +2760,18 @@ dependencies = [ [[package]] name = "jiff-core" -version = "0.1.2" +version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aee43309eb53bafb8c36d35d6e444769c8f8e6206a9fbf7546f5db9d7d4a0e93" +checksum = "77a24020db0ee88d1c681d677681bd31f9f4ea189a455425223d52ce4f1508aa" dependencies = [ - "defmt", "log", ] [[package]] name = "jiff-static" -version = "0.2.39" +version = "0.2.42" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6dc32470c67eb7d0e9ccb74303fa925313dc04cecb1aa048beaa6f391e36a93d" +checksum = "41d3213549742662eea1a18e6c5af9be5dfbabeb7611ab71356a54928ec398f0" dependencies = [ "jiff-core", "proc-macro2", @@ -3107,7 +3128,18 @@ checksum = "7122d987ec5f704ee56f6e5b41a7d93722e9aae27ae07cafa4036c4d3f9757de" dependencies = [ "log", "tendril", - "web_atoms", + "web_atoms 0.2.6", +] + +[[package]] +name = "markup5ever" +version = "0.40.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ab3dc68ac4a0f5719e560136778c1ee716e296030d75dbd4484e37e39e3a842" +dependencies = [ + "log", + "tendril", + "web_atoms 0.3.0", ] [[package]] @@ -3917,6 +3949,17 @@ dependencies = [ "serde", ] +[[package]] +name = "phf" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "010378780309880b08997fae13be7834dba947d36393bd372f2b1556deb2a2f6" +dependencies = [ + "phf_macros 0.14.0", + "phf_shared 0.14.0", + "serde", +] + [[package]] name = "phf_codegen" version = "0.13.1" @@ -3927,6 +3970,16 @@ dependencies = [ "phf_shared 0.13.1", ] +[[package]] +name = "phf_codegen" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41b585a510fb76fdebead6897982ef2a03a21d8e6cbcca904999742a4afc6ffe" +dependencies = [ + "phf_generator 0.14.0", + "phf_shared 0.14.0", +] + [[package]] name = "phf_generator" version = "0.11.3" @@ -3947,6 +4000,16 @@ dependencies = [ "phf_shared 0.13.1", ] +[[package]] +name = "phf_generator" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aeb62e0959d5a1bebc965f4d15d9e2b7cea002b6b0f5ba8cde6cc26738467100" +dependencies = [ + "fastrand", + "phf_shared 0.14.0", +] + [[package]] name = "phf_macros" version = "0.11.3" @@ -3973,6 +4036,19 @@ dependencies = [ "syn 2.0.119", ] +[[package]] +name = "phf_macros" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5fa8d0ca26d424d27630da600c6624696e7dec8bf7b3b492b383c5dc49e5e085" +dependencies = [ + "phf_generator 0.14.0", + "phf_shared 0.14.0", + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "phf_shared" version = "0.11.3" @@ -3991,6 +4067,15 @@ dependencies = [ "siphasher", ] +[[package]] +name = "phf_shared" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6fd9027e2d9319be6349febd1db4e8d02aa544921200c9b777720ac34a3aa89" +dependencies = [ + "siphasher", +] + [[package]] name = "pin-project" version = "1.1.13" @@ -4530,7 +4615,7 @@ checksum = "92ecd8964f8453721699a1ed72037b0db49ce2f5a5138486ee89bed6f67cdf3a" dependencies = [ "proc-macro2", "quote", - "syn 3.0.9", + "syn 3.0.10", ] [[package]] @@ -4633,7 +4718,7 @@ dependencies = [ [[package]] name = "resonance" -version = "3.6.0" +version = "3.6.1" dependencies = [ "axum", "base64 0.23.1", @@ -4691,7 +4776,7 @@ dependencies = [ [[package]] name = "resonance-cli" -version = "3.6.0" +version = "3.6.1" dependencies = [ "axum", "clap", @@ -4706,7 +4791,7 @@ dependencies = [ [[package]] name = "resonance-collections" -version = "3.6.0" +version = "3.6.1" dependencies = [ "resonance-core", "serde", @@ -4720,7 +4805,7 @@ dependencies = [ [[package]] name = "resonance-core" -version = "3.6.0" +version = "3.6.1" dependencies = [ "axum", "base64 0.23.1", @@ -4728,6 +4813,7 @@ dependencies = [ "bytes", "chrono", "encoding_rs", + "getrandom 0.4.3", "hex", "hmac 0.13.0", "http", @@ -4747,7 +4833,7 @@ dependencies = [ "tower", "url", "uuid", - "webpki-roots 1.0.11", + "webpki-roots 1.0.12", ] [[package]] @@ -4875,9 +4961,9 @@ dependencies = [ [[package]] name = "rustls-pki-types" -version = "1.15.1" +version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2f4925028c7eb5d1fcdaf196971378ed9d2c1c4efc7dc5d011256f76c99c0a96" +checksum = "102f35f7de7b06d8c61f1cff6592f5253e0dea431c74867aad5dfd70f44663fb" dependencies = [ "zeroize", ] @@ -5086,15 +5172,32 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8adfa1c298912827b8a28b223b3b874357397ae706e6190acd9bf28cee99114d" dependencies = [ "bitflags 2.13.2", - "cssparser", + "cssparser 0.37.0", "derive_more", "log", "new_debug_unreachable", "phf 0.13.1", - "phf_codegen", + "phf_codegen 0.13.1", "precomputed-hash", "rustc-hash", - "servo_arc", + "servo_arc 0.4.3", + "smallvec", +] + +[[package]] +name = "selectors" +version = "0.41.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8322aebea94a6cc288ff884e8a49bdbceef60206c400c6c46d22e62fc4fd91a6" +dependencies = [ + "bitflags 2.13.2", + "cssparser 0.38.0", + "derive_more", + "log", + "new_debug_unreachable", + "precomputed-hash", + "rustc-hash", + "servo_arc 0.5.0", "smallvec", ] @@ -5157,7 +5260,7 @@ checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348" dependencies = [ "proc-macro2", "quote", - "syn 3.0.9", + "syn 3.0.10", ] [[package]] @@ -5255,7 +5358,7 @@ checksum = "8d3b1629de253c70a0508c3899572da79ca359fdab27c7920ff00406df418906" dependencies = [ "proc-macro2", "quote", - "syn 3.0.9", + "syn 3.0.10", ] [[package]] @@ -5318,7 +5421,7 @@ dependencies = [ "darling", "proc-macro2", "quote", - "syn 3.0.9", + "syn 3.0.10", ] [[package]] @@ -5365,6 +5468,15 @@ dependencies = [ "stable_deref_trait", ] +[[package]] +name = "servo_arc" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4eceb519ec34d8290aaf6e46f435102f2bfa63f22b05a144fb16ab266d3b198" +dependencies = [ + "stable_deref_trait", +] + [[package]] name = "sha1" version = "0.10.7" @@ -5568,6 +5680,18 @@ dependencies = [ "precomputed-hash", ] +[[package]] +name = "string_cache" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffa8a5dbe8b3f0bbe29d4c3225daafaeead63afdc1b65fc4c01a1384166038e6" +dependencies = [ + "new_debug_unreachable", + "parking_lot", + "phf_shared 0.14.0", + "precomputed-hash", +] + [[package]] name = "string_cache_codegen" version = "0.6.1" @@ -5580,6 +5704,18 @@ dependencies = [ "quote", ] +[[package]] +name = "string_cache_codegen" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "928dcdf75e47626b3617a976ec205d9f057584c371c1f23b782129268d0e6edc" +dependencies = [ + "phf_generator 0.14.0", + "phf_shared 0.14.0", + "proc-macro2", + "quote", +] + [[package]] name = "strsim" version = "0.11.1" @@ -5668,9 +5804,9 @@ dependencies = [ [[package]] name = "syn" -version = "3.0.9" +version = "3.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d78c8dee4c7bf0e14673097256fed6142ce9d3b85a408189d07482442145823b" +checksum = "4ae9ccbea3a7b0b09443036490c8f34a8b1b760fd345e5c0dae0aac718faea79" dependencies = [ "proc-macro2", "quote", @@ -5705,7 +5841,7 @@ checksum = "901704edd0dfe137f1987838ee4f259e4e063c31371bdb423f7ae38ec6f77f02" dependencies = [ "proc-macro2", "quote", - "syn 3.0.9", + "syn 3.0.10", ] [[package]] @@ -6113,7 +6249,7 @@ dependencies = [ "brotli", "cargo_metadata", "ctor", - "dom_query", + "dom_query 0.28.0", "dunce", "glob", "http", @@ -6217,7 +6353,7 @@ checksum = "fe5197923287db20a58125f0bc85c062f7f2c892de97b18c356f9efb14b28524" dependencies = [ "proc-macro2", "quote", - "syn 3.0.9", + "syn 3.0.10", ] [[package]] @@ -6304,7 +6440,7 @@ checksum = "78773a2a397f451582ce068015985c33193cf6dea8b74d2a639fe457b2f07b0e" dependencies = [ "proc-macro2", "quote", - "syn 3.0.9", + "syn 3.0.10", ] [[package]] @@ -6916,7 +7052,7 @@ dependencies = [ "bumpalo", "proc-macro2", "quote", - "syn 3.0.9", + "syn 3.0.10", "wasm-bindgen-shared", ] @@ -6969,9 +7105,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ba8b815c1b593dc0baf78dd0f4fc8fdb2de53198fb1163738093e9a311c33fb3" dependencies = [ "phf 0.13.1", - "phf_codegen", - "string_cache", - "string_cache_codegen", + "phf_codegen 0.13.1", + "string_cache 0.9.0", + "string_cache_codegen 0.6.1", +] + +[[package]] +name = "web_atoms" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7572660c8890448ba236b7376f27e389c6a7e1c70195622faced601f855c0ada" +dependencies = [ + "phf 0.14.0", + "phf_codegen 0.14.0", + "string_cache 0.11.0", + "string_cache_codegen 0.11.2", ] [[package]] @@ -7020,9 +7168,9 @@ dependencies = [ [[package]] name = "webpki-root-certs" -version = "1.0.11" +version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e49d0e65abc00c142e8f9428da991da9f1a250c39be97459901eb12b9b49f62a" +checksum = "293198dcebfb821155b5092ab94b5a82ff180eddd7241df3bd6e66a4ffd1a585" dependencies = [ "rustls-pki-types", ] @@ -7033,14 +7181,14 @@ version = "0.26.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "521bc38abb08001b01866da9f51eb7c5d647a19260e00054a8c7fd5f9e57f7a9" dependencies = [ - "webpki-roots 1.0.11", + "webpki-roots 1.0.12", ] [[package]] name = "webpki-roots" -version = "1.0.11" +version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f5f280bbb2b287bc8e7de7e3852945a0ff50d1bbcb5ed42b6c7a568c546df171" +checksum = "7265c9301c16a8711c992c2d8da90ee77ef54b1ed51482d6dfeb00dc756b1297" dependencies = [ "rustls-pki-types", ] @@ -7605,16 +7753,16 @@ checksum = "3ad82d2a33cdc9674dc7465672f271e096168fcdbe0f799d9e6db8c5892679dc" [[package]] name = "wry" -version = "0.57.1" +version = "0.57.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "759c50ddad0ef9913bb5496cb9d509a50433a3b78cb0238d61105526589cf0e9" +checksum = "a490119196d02a51d0871df7b9119a1f3e46d75b7e90053a801454d5c6b3491c" dependencies = [ "base64 0.23.1", "block2", "cookie", "crossbeam-channel", "dirs", - "dom_query", + "dom_query 0.29.0", "dpi", "dunce", "gdkx11", @@ -7730,7 +7878,7 @@ checksum = "ec8ebde2db3681e8c9980cc27822030e68752690ddfa9473e739aeb4dbde6d71" dependencies = [ "proc-macro2", "quote", - "syn 3.0.9", + "syn 3.0.10", "synstructure 0.14.0", ] @@ -7818,7 +7966,7 @@ dependencies = [ "proc-macro-crate 3.5.0", "proc-macro2", "quote", - "syn 3.0.9", + "syn 3.0.10", "zbus_names 4.3.5", "zvariant 5.15.1", "zvariant_utils 4.2.0", @@ -7892,7 +8040,7 @@ checksum = "f75b4683f6c7f45248d4d64056a24298c6281e0993356d7d1b4a1a962ef10d4a" dependencies = [ "proc-macro2", "quote", - "syn 3.0.9", + "syn 3.0.10", "synstructure 0.14.0", ] @@ -7968,7 +8116,7 @@ checksum = "c3743ddbf47738d929cd1ae2e199d40d0162979dfe31aef8ae646d2bf00bf97a" dependencies = [ "proc-macro2", "quote", - "syn 3.0.9", + "syn 3.0.10", ] [[package]] @@ -8034,7 +8182,7 @@ dependencies = [ "proc-macro-crate 3.5.0", "proc-macro2", "quote", - "syn 3.0.9", + "syn 3.0.10", "zvariant_utils 4.2.0", ] @@ -8058,6 +8206,6 @@ dependencies = [ "proc-macro2", "quote", "serde", - "syn 3.0.9", + "syn 3.0.10", "winnow 1.0.5", ] diff --git a/Cargo.toml b/Cargo.toml index c9d818a1..b742a653 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,7 @@ default-members = [ ] [workspace.package] -version = "3.6.0" +version = "3.6.1" edition = "2024" license = "MIT" authors = ["Dennis Blümer "] diff --git a/crates/resonance-cli/tests/run.rs b/crates/resonance-cli/tests/run.rs index a5b94d66..98d48446 100644 --- a/crates/resonance-cli/tests/run.rs +++ b/crates/resonance-cli/tests/run.rs @@ -123,15 +123,15 @@ async fn bail_stops_after_the_first_failure_and_skips_the_rest() { } #[tokio::test(flavor = "multi_thread")] -async fn a_v1_collection_and_a_plain_directory_are_refused() { +async fn a_directory_without_a_collection_yaml_is_refused() { let server = Server::start().await; - let legacy = Collection::new("Legacy"); - std::fs::remove_file(legacy.path().join("collection.yaml")).unwrap(); - legacy.write("collection.json", "{}"); - let cli = run(&legacy, &server, &[]); + let unmarked = Collection::new("Unmarked"); + std::fs::remove_file(unmarked.path().join("collection.yaml")).unwrap(); + unmarked.write("collection.json", "{}"); + let cli = run(&unmarked, &server, &[]); assert_eq!(cli.code(), 2); assert!( - cli.stderr().contains("open it in Resonance once"), + cli.stderr().contains("no collection.yaml"), "{}", cli.stderr() ); diff --git a/crates/resonance-collections/src/ipc.rs b/crates/resonance-collections/src/ipc.rs index 55502431..b3319ffe 100644 --- a/crates/resonance-collections/src/ipc.rs +++ b/crates/resonance-collections/src/ipc.rs @@ -537,11 +537,13 @@ fn index_requests(loaded: &LoadedCollection) -> HashMap { } /// Indexes an existing tree's folders by id, to keep their directories. +/// +/// A directory without `_folder.yaml` is indexed under its derived id, the +/// same one the frontend was given, so a save finds it instead of creating a +/// suffixed sibling. fn index_folders(node: &FolderNode, out: &mut HashMap) { for folder in &node.folders { - if let Some(meta) = &folder.meta { - out.insert(meta.id.clone(), folder.clone()); - } + out.insert(folder_id(folder), folder.clone()); index_folders(folder, out); } } @@ -737,7 +739,10 @@ fn folder_from_ipc( .and_then(|f| f.meta.as_ref()) .and_then(|m| m.auth.clone()) }), - extra: Map::new(), + extra: previous + .and_then(|f| f.meta.as_ref()) + .map(|m| m.extra.clone()) + .unwrap_or_default(), }), source: previous.and_then(|f| f.source.clone()), requests: Vec::new(), @@ -800,12 +805,8 @@ pub fn find_request<'a>(loaded: &'a LoadedCollection, request_id: &str) -> Optio #[cfg(test)] mod tests { use super::*; - use crate::legacy::v1_to_v2; use crate::model::{FORMAT_VERSION, FolderDoc}; - use crate::read::{RequestEntry, read_collection_dir}; - use crate::write::write_collection_dir; - use std::collections::HashMap; - use tempfile::TempDir; + use crate::read::RequestEntry; fn request(id: &str, name: &str) -> RequestEntry { RequestEntry::new(RequestDoc::new(id.into(), name.into())) @@ -1147,69 +1148,6 @@ mod tests { assert!(request_to_endpoint_data(&doc).scripts.is_none()); } } - - /// The step-9 promise: a v2 directory written by a teammate opens - /// correctly in a build that still writes v1. - #[test] - fn a_v2_directory_on_disk_reads_back_through_the_ipc_shape() { - let v1 = Collection { - id: "c1".into(), - name: "Petstore".into(), - base_url: "https://api.example.com".into(), - endpoints: vec![ - json!({"id": "custom_1", "name": "Health", "method": "GET", "path": "/health"}), - json!({"id": "custom_2", "name": "Create Pet", "method": "POST", "path": "/pets"}), - ], - folders: vec![json!({ - "id": "folder_pets", - "name": "pets", - "endpoints": [{"id": "custom_2", "name": "Create Pet", "method": "POST", "path": "/pets"}] - })], - default_headers: json!({"Accept": "application/json"}), - auth_config: None, - open_api_spec: None, - storage_path: None, - storage_parent_path: None, - linked: false, - can_close: false, - git_branch: None, - }; - - let mut data = HashMap::new(); - data.insert( - "custom_2".to_string(), - EndpointData { - modified_body: Some("{\"name\": \"Rex\"}".into()), - headers: vec![json!({"key": "Accept", "value": "*/*"})], - ..Default::default() - }, - ); - - let temp = TempDir::new().unwrap(); - let mut converted = v1_to_v2(&v1, &data); - write_collection_dir(temp.path(), &mut converted).unwrap(); - - let reread = read_collection_dir(temp.path()).unwrap(); - let ipc = to_ipc_collection(&reread, &temp.path().to_string_lossy(), false); - - assert_eq!(ipc.id, "c1"); - assert_eq!(ipc.default_headers["Accept"], "application/json"); - - // Both requests are reachable, and the foldered one is in both views. - assert_eq!(ipc.endpoints.len(), 2); - assert_eq!(ipc.folders.len(), 1); - assert_eq!(ipc.folders[0]["endpoints"].as_array().unwrap().len(), 1); - assert_eq!(ipc.folders[0]["endpoints"][0]["id"], "custom_2"); - - // The per-request data survives the trip through the files. - let request = find_request(&reread, "custom_2").unwrap(); - let endpoint_data = request_to_endpoint_data(request); - assert_eq!( - endpoint_data.modified_body.as_deref(), - Some("{\"name\": \"Rex\"}") - ); - assert_eq!(endpoint_data.headers.len(), 1); - } } #[cfg(test)] @@ -1569,6 +1507,60 @@ mod save_path { ); } + #[test] + fn a_folder_without_metadata_keeps_its_directory() { + let mut existing = existing_with_state(); + existing.root.folders.push(FolderNode { + meta: None, + source: Some(std::path::PathBuf::from("/c/pets")), + requests: Vec::new(), + folders: Vec::new(), + }); + let id = folder_id(&existing.root.folders[0]); + + let incoming = ipc_collection( + vec![], + vec![json!({"id": id, "name": "pets", "endpoints": []})], + ); + let root = tree_from_ipc(&incoming, Some(&existing)); + + assert_eq!( + root.folders[0].source.as_ref().unwrap(), + &std::path::PathBuf::from("/c/pets") + ); + } + + #[test] + fn unknown_folder_keys_survive_a_save() { + let mut existing = existing_with_state(); + let mut extra = Map::new(); + extra.insert("color".into(), json!("red")); + existing.root.folders.push(FolderNode { + meta: Some(crate::model::FolderDoc { + format: FORMAT_VERSION, + id: "f1".into(), + name: "pets".into(), + seq: 10, + auth: None, + extra, + }), + source: Some(std::path::PathBuf::from("/c/pets")), + requests: Vec::new(), + folders: Vec::new(), + }); + + let incoming = ipc_collection( + vec![], + vec![json!({"id": "f1", "name": "pets", "endpoints": []})], + ); + let root = tree_from_ipc(&incoming, Some(&existing)); + + assert_eq!( + root.folders[0].meta.as_ref().unwrap().extra["color"], + json!("red") + ); + } + mod reordering { use super::*; diff --git a/crates/resonance-collections/src/layout.rs b/crates/resonance-collections/src/layout.rs index 1d881f45..282d9fd4 100644 --- a/crates/resonance-collections/src/layout.rs +++ b/crates/resonance-collections/src/layout.rs @@ -1,6 +1,5 @@ //! Naming rules for the files and directories a collection occupies. -use std::fs; use std::path::{Path, PathBuf}; /// File names reserved inside a collection directory. A request whose name @@ -10,10 +9,6 @@ pub const VARIABLES_YAML: &str = "variables.yaml"; pub const OPENAPI_YAML: &str = "openapi.yaml"; pub const FOLDER_YAML: &str = "_folder.yaml"; -/// The v1 marker, kept so a directory written by an older build is still -/// recognised and can be converted. -pub const COLLECTION_JSON: &str = "collection.json"; - const RESERVED_STEMS: &[&str] = &["collection", "variables", "openapi"]; /// Directories the layout owns at the collection root. The reader never @@ -139,25 +134,6 @@ pub fn slugify(input: &str) -> String { } } -pub fn sanitize_file_component(input: &str) -> String { - let mut out = String::new(); - for c in input.chars() { - let ch = c.to_ascii_lowercase(); - if ch.is_ascii_alphanumeric() || ch == '-' || ch == '_' { - out.push(ch); - } else if !out.ends_with('-') && !out.is_empty() { - out.push('-'); - } - } - - let out = out.trim_matches('-').to_string(); - if out.is_empty() { - "item".to_string() - } else { - out - } -} - pub fn find_available_dir(parent: &Path, base_name: &str, current_dir: Option<&Path>) -> PathBuf { let mut candidate = parent.join(base_name); if current_dir == Some(candidate.as_path()) || !candidate.exists() { @@ -206,50 +182,10 @@ pub fn find_available_folder_dir( } } -pub fn desired_endpoint_file_name(endpoint_name: &str, endpoint_id: &str) -> String { - format!( - "{}--{}.json", - slugify(endpoint_name), - sanitize_file_component(endpoint_id) - ) -} - -pub fn find_endpoint_data_file( - requests_dir: &Path, - endpoint_id: &str, -) -> Result, String> { - let legacy_file = requests_dir.join(format!("{}.json", endpoint_id)); - if legacy_file.exists() { - return Ok(Some(legacy_file)); - } - - if !requests_dir.exists() { - return Ok(None); - } - - let suffix = format!("--{}.json", sanitize_file_component(endpoint_id)); - let entries = - fs::read_dir(requests_dir).map_err(|e| format!("Failed to read requests dir: {}", e))?; - - for entry in entries { - let path = entry - .map_err(|e| format!("Failed to read dir entry: {}", e))? - .path(); - - if path.is_file() - && let Some(name) = path.file_name().and_then(|value| value.to_str()) - && name.ends_with(&suffix) - { - return Ok(Some(path)); - } - } - - Ok(None) -} - #[cfg(test)] mod tests { use super::*; + use std::fs; use tempfile::TempDir; #[test] @@ -280,40 +216,6 @@ mod tests { assert_eq!(slugify("日本語"), "collection"); } - #[test] - fn sanitize_keeps_dashes_and_underscores() { - assert_eq!(sanitize_file_component("custom_3"), "custom_3"); - assert_eq!(sanitize_file_component("req-abc"), "req-abc"); - } - - #[test] - fn sanitize_falls_back_for_symbol_only_input() { - assert_eq!(sanitize_file_component("///"), "item"); - assert_eq!(sanitize_file_component(""), "item"); - } - - /// Defect: distinct endpoint ids sanitize to the same file component, so - /// find_endpoint_data_file's suffix scan can hand one endpoint another's - /// data file. Pinned here as a characterization of today's behaviour; the - /// v2 layout drops ids from filenames entirely, which removes the hazard. - #[test] - fn distinct_ids_can_collide_after_sanitizing() { - let collided: Vec = ["a.b", "a b", "a-b", "A-B"] - .iter() - .map(|id| sanitize_file_component(id)) - .collect(); - - assert_eq!(collided, vec!["a-b", "a-b", "a-b", "a-b"]); - } - - #[test] - fn endpoint_file_name_joins_the_slug_and_the_id() { - assert_eq!( - desired_endpoint_file_name("List Users", "custom_3"), - "list-users--custom_3.json" - ); - } - #[test] fn available_dir_returns_the_plain_name_when_free() { let temp = TempDir::new().unwrap(); @@ -408,68 +310,6 @@ mod tests { assert_ne!(chosen, temp.path().join("collection.yaml")); } - #[test] - fn finds_a_data_file_by_its_id_suffix() { - let temp = TempDir::new().unwrap(); - let file = temp.path().join("list-users--custom_3.json"); - fs::write(&file, "{}").unwrap(); - - let found = find_endpoint_data_file(temp.path(), "custom_3").unwrap(); - assert_eq!(found, Some(file)); - } - - #[test] - fn finds_a_legacy_data_file_named_only_by_id() { - let temp = TempDir::new().unwrap(); - let file = temp.path().join("custom_3.json"); - fs::write(&file, "{}").unwrap(); - - let found = find_endpoint_data_file(temp.path(), "custom_3").unwrap(); - assert_eq!(found, Some(file)); - } - - #[test] - fn prefers_the_legacy_name_when_both_exist() { - let temp = TempDir::new().unwrap(); - let legacy = temp.path().join("custom_3.json"); - fs::write(&legacy, "{}").unwrap(); - fs::write(temp.path().join("list-users--custom_3.json"), "{}").unwrap(); - - let found = find_endpoint_data_file(temp.path(), "custom_3").unwrap(); - assert_eq!(found, Some(legacy)); - } - - #[test] - fn reports_no_file_for_an_unknown_id() { - let temp = TempDir::new().unwrap(); - fs::write(temp.path().join("list-users--custom_3.json"), "{}").unwrap(); - - assert_eq!( - find_endpoint_data_file(temp.path(), "custom_9").unwrap(), - None - ); - } - - #[test] - fn a_missing_requests_dir_is_not_an_error() { - let temp = TempDir::new().unwrap(); - let absent = temp.path().join("requests"); - - assert_eq!(find_endpoint_data_file(&absent, "custom_3").unwrap(), None); - } - - /// Defect: the suffix match is unanchored on the left, so an id that is a - /// tail of another id's filename can match the wrong file. - #[test] - fn suffix_matching_is_unanchored_on_the_left() { - let temp = TempDir::new().unwrap(); - let other = temp.path().join("req--a--b.json"); - fs::write(&other, "{}").unwrap(); - - let found = find_endpoint_data_file(temp.path(), "b").unwrap(); - assert_eq!(found, Some(other)); - } - #[test] fn a_symbol_only_runner_name_falls_back_to_runner() { assert_eq!(runner_stem("///"), "runner"); diff --git a/crates/resonance-collections/src/legacy.rs b/crates/resonance-collections/src/legacy.rs deleted file mode 100644 index 639eb15d..00000000 --- a/crates/resonance-collections/src/legacy.rs +++ /dev/null @@ -1,1013 +0,0 @@ -//! Converting a v1 collection into the v2 model. -//! -//! v1 kept a request's metadata in the shared `collection.json` and its actual -//! values in `requests/--.json`, with foldered requests duplicated -//! into a flat `endpoints` array. This module rejoins those halves into one -//! document per request and turns the folder array into a tree. -//! -//! The conversion is pure: it takes parsed v1 data and returns the v2 model, -//! touching no filesystem and needing no AppHandle, which is what keeps its -//! tests cheap. - -use resonance_core::tags::normalize; -use serde::de::DeserializeOwned; -use serde_json::{Map, Value}; -use std::collections::{HashMap, HashSet}; -use std::fs; -use std::path::Path; - -use super::model::{CollectionDoc, FORMAT_VERSION, FolderDoc, Params, RequestDoc, Spec}; -use super::read::{FolderNode, LoadedCollection, RequestEntry}; -use crate::ipc::{ - Collection, EndpointData, body_from_endpoint_data, scripts_from_endpoint_data, string_field, -}; -use crate::layout::{COLLECTION_JSON, find_endpoint_data_file}; - -/// Reads and parses one JSON file. -pub fn read_json_file(path: &Path) -> Result { - let content = fs::read_to_string(path).map_err(|e| format!("Failed to read file: {}", e))?; - serde_json::from_str(&content).map_err(|e| format!("Failed to parse JSON: {}", e)) -} - -/// A v1 endpoint's display name, falling back to its path. -pub fn extract_endpoint_name(endpoint: &Value) -> Option { - endpoint - .get("name") - .and_then(|value| value.as_str()) - .filter(|value| !value.trim().is_empty()) - .map(|value| value.to_string()) - .or_else(|| { - endpoint - .get("path") - .and_then(|value| value.as_str()) - .filter(|value| !value.trim().is_empty()) - .map(|value| value.to_string()) - }) -} - -/// Every endpoint id/name pair a v1 collection lists, each once. -pub fn list_collection_endpoints(collection: &Collection) -> Vec<(String, String)> { - let mut seen = HashSet::new(); - let mut endpoints = Vec::new(); - - let mut collect_endpoint = |endpoint: &Value| { - if let Some(endpoint_id) = endpoint.get("id").and_then(|value| value.as_str()) - && seen.insert(endpoint_id.to_string()) - { - let endpoint_name = - extract_endpoint_name(endpoint).unwrap_or_else(|| endpoint_id.to_string()); - endpoints.push((endpoint_id.to_string(), endpoint_name)); - } - }; - - for endpoint in &collection.endpoints { - collect_endpoint(endpoint); - } - - for folder in &collection.folders { - if let Some(folder_endpoints) = folder.get("endpoints").and_then(|value| value.as_array()) { - for endpoint in folder_endpoints { - collect_endpoint(endpoint); - } - } - } - - endpoints -} - -/// Reads a v1 collection directory (`collection.json`, `requests/*.json`, -/// `variables.json`) straight into the v2 model, without touching any cache. -/// -/// @param dir - A directory whose layout is V1 -/// @returns The collection as a v2 tree, ready to be written -pub fn load_v1_dir(dir: &Path) -> Result { - let collection: Collection = read_json_file(&dir.join(COLLECTION_JSON))?; - - let requests_dir = dir.join("requests"); - let mut data = HashMap::new(); - for (endpoint_id, _) in list_collection_endpoints(&collection) { - if let Some(file) = find_endpoint_data_file(&requests_dir, &endpoint_id)? - && let Ok(endpoint_data) = read_json_file::(&file) - { - data.insert(endpoint_id, endpoint_data); - } - } - - let mut loaded = v1_to_v2(&collection, &data); - - let variables_file = dir.join("variables.json"); - if variables_file.exists() { - loaded.variables = read_json_file(&variables_file).unwrap_or_default(); - } - - Ok(loaded) -} - -/// Gap between generated `seq` values, matching the writer's step. -const SEQ_STEP: i64 = 10; - -/// Reads a nested object field, treating an empty object as absent. -fn object_field(value: &Value, key: &str) -> Option { - value - .get(key) - .filter(|v| !v.is_null()) - .filter(|v| !matches!(v, Value::Object(map) if map.is_empty())) - .cloned() -} - -/// Keeps a value only when it carries something, so v1's ubiquitous nulls and -/// empty objects do not become keys in the v2 file. -fn non_empty_object(value: &Value) -> Option { - match value { - Value::Null => None, - Value::Object(map) if map.is_empty() => None, - other => Some(other.clone()), - } -} - -/// Drops the empty `{query: {}, header: {}, path: {}}` skeleton the frontend -/// attached to every request it created, so it does not become noise in every -/// v2 file. Buckets that hold something are kept. -/// -/// @param parameters - The v1 `parameters` object, if any -/// @returns The parameters with empty buckets removed, or None when nothing is left -fn prune_parameter_buckets(parameters: Option) -> Option { - let map = match parameters { - Some(Value::Object(map)) => map, - other => return other, - }; - - let pruned: Map = map - .into_iter() - .filter(|(_, value)| !matches!(value, Value::Object(inner) if inner.is_empty())) - .filter(|(_, value)| !value.is_null()) - .collect(); - - if pruned.is_empty() { - None - } else { - Some(Value::Object(pruned)) - } -} - -/// Rejoins one endpoint's metadata and its data file into a request document. -/// -/// A v1 endpoint carries two auth sources: the `security` block an OpenAPI -/// import derived from the operation's security requirement, and the -/// `authConfig` the user edited. The data file wins where it exists; the spec -/// requirement is the fallback, so an imported request keeps the auth its -/// operation declares instead of silently falling back to "inherit". -/// -/// @param endpoint - The endpoint entry from collection.json -/// @param data - The endpoint's data file, if it had one -/// @param seq - Position in its folder, derived from the v1 array order -/// @returns The v2 request document -pub fn endpoint_to_request(endpoint: &Value, data: Option<&EndpointData>, seq: i64) -> RequestDoc { - let id = string_field(endpoint, "id").unwrap_or_default(); - let name = string_field(endpoint, "name") - .or_else(|| string_field(endpoint, "path")) - .unwrap_or_else(|| id.clone()); - - let mut doc = RequestDoc::new(id, name); - doc.format = FORMAT_VERSION; - doc.seq = seq; - doc.protocol = string_field(endpoint, "protocol"); - doc.method = string_field(endpoint, "method"); - doc.http_method = string_field(endpoint, "httpMethod"); - doc.path = string_field(endpoint, "path"); - doc.description = string_field(endpoint, "description"); - doc.tags = endpoint - .get("tags") - .and_then(Value::as_array) - .map(|tags| normalize(tags.iter().filter_map(Value::as_str))) - .unwrap_or_default(); - - doc.spec = Spec { - parameters: prune_parameter_buckets(object_field(endpoint, "parameters")), - request_body: object_field(endpoint, "requestBody"), - responses: object_field(endpoint, "responses"), - }; - - if let Some(data) = data { - doc.url = data.url.clone().filter(|u| !u.is_empty()); - doc.auth = data.auth_config.clone().filter(|a| !a.is_null()); - doc.params = Params { - path: data.path_params.clone(), - query: data.query_params.clone(), - headers: data.headers.clone(), - }; - doc.body = body_from_endpoint_data(data); - doc.grpc = data.grpc_data.clone().filter(|v| !v.is_null()); - doc.mqtt = data.mqtt_data.clone().filter(|v| !v.is_null()); - doc.websocket = data.websocket_data.clone().filter(|v| !v.is_null()); - doc.scripts = scripts_from_endpoint_data(data.scripts.as_ref()); - doc.response_schema = data.response_schema.clone().filter(|v| !v.is_null()); - } - - if doc.auth.is_none() { - doc.auth = object_field(endpoint, "security"); - } - - doc -} - -/// Lists the endpoint ids a v1 collection files inside folders. -fn foldered_ids(collection: &Collection) -> Vec { - let mut ids = Vec::new(); - for folder in &collection.folders { - if let Some(endpoints) = folder.get("endpoints").and_then(|e| e.as_array()) { - for endpoint in endpoints { - if let Some(id) = string_field(endpoint, "id") { - ids.push(id); - } - } - } - } - ids -} - -/// Converts a v1 collection and its endpoint data files into the v2 model. -/// -/// Endpoints duplicated across the flat list and a folder become one request, -/// filed in the folder. Ids are preserved exactly: they key the keychain auth -/// scopes, pinned requests, scripts, mock overrides and workspace tabs that -/// live outside the collection file, so changing one detaches all of it. -/// -/// @param collection - The parsed collection.json -/// @param data - Endpoint data files, keyed by endpoint id -/// @returns The collection in the v2 model, ready to be written -pub fn v1_to_v2(collection: &Collection, data: &HashMap) -> LoadedCollection { - let meta = CollectionDoc { - format: FORMAT_VERSION, - id: collection.id.clone(), - name: collection.name.clone(), - base_url: collection.base_url.clone(), - description: None, - default_headers: non_empty_object(&collection.default_headers), - auth: collection.auth_config.clone().filter(|a| !a.is_null()), - // Set by the writer once the spec file is placed beside this document. - open_api_spec: None, - extra: Map::new(), - }; - - let foldered = foldered_ids(collection); - let mut root = FolderNode::default(); - - let mut seq = 0; - for endpoint in &collection.endpoints { - let id = match string_field(endpoint, "id") { - Some(id) => id, - None => continue, - }; - if foldered.contains(&id) { - continue; - } - seq += SEQ_STEP; - root.requests.push(RequestEntry::new(endpoint_to_request( - endpoint, - data.get(&id), - seq, - ))); - } - - let mut folder_seq = 0; - for folder in &collection.folders { - let folder_id = match string_field(folder, "id") { - Some(id) => id, - None => continue, - }; - folder_seq += SEQ_STEP; - - let mut node = FolderNode { - meta: Some(FolderDoc { - format: FORMAT_VERSION, - id: folder_id, - name: string_field(folder, "name").unwrap_or_else(|| "folder".to_string()), - seq: folder_seq, - auth: object_field(folder, "authConfig"), - extra: Map::new(), - }), - source: None, - requests: Vec::new(), - folders: Vec::new(), - }; - - let mut inner_seq = 0; - if let Some(endpoints) = folder.get("endpoints").and_then(|e| e.as_array()) { - for endpoint in endpoints { - let id = match string_field(endpoint, "id") { - Some(id) => id, - None => continue, - }; - inner_seq += SEQ_STEP; - node.requests.push(RequestEntry::new(endpoint_to_request( - endpoint, - data.get(&id), - inner_seq, - ))); - } - } - - root.folders.push(node); - } - - LoadedCollection { - meta, - open_api_spec: collection.open_api_spec.clone().filter(|v| !v.is_null()), - variables: Vec::new(), - root, - runners: Vec::new(), - reassigned_runner_ids: Vec::new(), - } -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::model::{Body, BodyKind}; - use serde_json::json; - - fn v1_collection(endpoints: Vec, folders: Vec) -> Collection { - Collection { - id: "c1".into(), - name: "Petstore".into(), - base_url: "https://api.example.com".into(), - endpoints, - folders, - default_headers: Value::Null, - auth_config: None, - open_api_spec: None, - storage_path: Some("/home/someone/.local/share/resonance/collections/petstore".into()), - storage_parent_path: None, - linked: false, - can_close: false, - git_branch: None, - } - } - - fn endpoint(id: &str, name: &str) -> Value { - json!({"id": id, "name": name, "method": "GET", "path": "/x", "protocol": "http"}) - } - - fn convert(collection: &Collection, data: HashMap) -> LoadedCollection { - v1_to_v2(collection, &data) - } - - #[test] - fn collection_metadata_carries_over() { - let converted = convert(&v1_collection(vec![], vec![]), HashMap::new()); - - assert_eq!(converted.meta.id, "c1"); - assert_eq!(converted.meta.name, "Petstore"); - assert_eq!(converted.meta.base_url, "https://api.example.com"); - assert_eq!(converted.meta.format, FORMAT_VERSION); - } - - /// The absolute path was machine-specific noise in a committed file. - #[test] - fn the_storage_path_is_not_carried_over() { - let converted = convert(&v1_collection(vec![], vec![]), HashMap::new()); - let yaml = serde_yaml_ng::to_string(&converted.meta).unwrap(); - - assert!(!yaml.contains("storagePath"), "{}", yaml); - assert!(!yaml.contains("/home/someone"), "{}", yaml); - } - - #[test] - fn the_openapi_spec_moves_off_the_collection_document() { - let mut collection = v1_collection(vec![], vec![]); - collection.open_api_spec = Some(json!({"openapi": "3.0.0"})); - - let converted = convert(&collection, HashMap::new()); - - assert_eq!(converted.open_api_spec.unwrap()["openapi"], "3.0.0"); - assert!( - converted.meta.open_api_spec.is_none(), - "the file name is set by the writer, not the conversion" - ); - } - - /// v1 stored a foldered request twice: once flat, once in the folder. The - /// conversion must produce one file, in the folder. - #[test] - fn a_request_duplicated_into_a_folder_becomes_one_request() { - let collection = v1_collection( - vec![endpoint("r1", "Health"), endpoint("r2", "List Pets")], - vec![json!({"id": "f1", "name": "pets", "endpoints": [endpoint("r2", "List Pets")]})], - ); - - let converted = convert(&collection, HashMap::new()); - - assert_eq!(converted.requests().len(), 2); - assert_eq!(converted.root.requests.len(), 1); - assert_eq!(converted.root.requests[0].doc.id, "r1"); - assert_eq!(converted.root.folders[0].requests.len(), 1); - assert_eq!(converted.root.folders[0].requests[0].doc.id, "r2"); - } - - #[test] - fn a_folderless_collection_puts_every_request_at_the_root() { - let collection = v1_collection(vec![endpoint("r1", "A"), endpoint("r2", "B")], vec![]); - let converted = convert(&collection, HashMap::new()); - - assert_eq!(converted.root.requests.len(), 2); - assert!(converted.root.folders.is_empty()); - } - - /// Ids key the keychain scopes, pinned requests, scripts and mock - /// overrides that live outside the collection file. Changing one on - /// migration would detach all of it. - #[test] - fn endpoint_ids_are_preserved_exactly() { - let collection = v1_collection(vec![endpoint("custom_3", "Legacy")], vec![]); - let converted = convert(&collection, HashMap::new()); - - assert_eq!(converted.root.requests[0].doc.id, "custom_3"); - } - - #[test] - fn seq_follows_the_v1_array_order() { - let collection = v1_collection( - vec![ - endpoint("r1", "First"), - endpoint("r2", "Second"), - endpoint("r3", "Third"), - ], - vec![], - ); - let converted = convert(&collection, HashMap::new()); - - let seqs: Vec<_> = converted.root.requests.iter().map(|e| e.doc.seq).collect(); - assert_eq!(seqs, vec![10, 20, 30]); - } - - #[test] - fn folder_auth_carries_over() { - let collection = v1_collection( - vec![], - vec![json!({ - "id": "f1", - "name": "pets", - "endpoints": [], - "authConfig": {"type": "bearer", "config": {"token": ""}} - })], - ); - - let converted = convert(&collection, HashMap::new()); - let auth = converted.root.folders[0] - .meta - .as_ref() - .unwrap() - .auth - .as_ref() - .unwrap(); - - assert_eq!(auth["type"], "bearer"); - } - - #[test] - fn an_endpoint_without_a_data_file_still_becomes_a_request() { - let collection = v1_collection(vec![endpoint("r1", "Bare")], vec![]); - let converted = convert(&collection, HashMap::new()); - - assert_eq!(converted.root.requests[0].doc.name, "Bare"); - assert!(converted.root.requests[0].doc.body.is_none()); - } - - #[test] - fn endpoint_data_is_rejoined_onto_the_request() { - let mut data = HashMap::new(); - data.insert( - "r1".to_string(), - EndpointData { - url: Some("https://api.example.com/x".into()), - headers: vec![json!({"key": "Accept", "value": "*/*"})], - query_params: vec![json!({"key": "page", "value": "1"})], - auth_config: Some(json!({"type": "bearer", "config": {"token": ""}})), - response_schema: Some(json!({"type": "object"})), - ..Default::default() - }, - ); - - let converted = convert(&v1_collection(vec![endpoint("r1", "X")], vec![]), data); - let doc = &converted.root.requests[0].doc; - - assert_eq!(doc.url.as_deref(), Some("https://api.example.com/x")); - assert_eq!(doc.params.headers.len(), 1); - assert_eq!(doc.params.query.len(), 1); - assert_eq!(doc.auth.as_ref().unwrap()["type"], "bearer"); - assert!(doc.response_schema.is_some()); - } - - #[test] - fn a_spec_security_requirement_becomes_the_requests_auth() { - let endpoint = json!({ - "id": "r1", - "name": "Secured", - "security": {"type": "bearer", "config": {"token": "{{bearerToken}}"}} - }); - - let converted = convert(&v1_collection(vec![endpoint], vec![]), HashMap::new()); - let auth = converted.root.requests[0].doc.auth.as_ref().unwrap(); - - assert_eq!(auth["type"], "bearer"); - assert_eq!(auth["config"]["token"], "{{bearerToken}}"); - } - - #[test] - fn an_edited_auth_config_wins_over_the_spec_requirement() { - let endpoint = json!({ - "id": "r1", - "name": "Secured", - "security": {"type": "bearer", "config": {"token": "{{bearerToken}}"}} - }); - let mut data = HashMap::new(); - data.insert( - "r1".to_string(), - EndpointData { - auth_config: Some(json!({"type": "none", "config": {}})), - ..Default::default() - }, - ); - - let converted = convert(&v1_collection(vec![endpoint], vec![]), data); - - assert_eq!( - converted.root.requests[0].doc.auth.as_ref().unwrap()["type"], - "none" - ); - } - - #[test] - fn spec_material_moves_under_the_spec_key() { - let endpoint = json!({ - "id": "r1", - "name": "X", - "parameters": {"query": {"limit": {"example": "20"}}}, - "requestBody": {"required": true}, - "responses": {"200": {"description": "ok"}} - }); - - let converted = convert(&v1_collection(vec![endpoint], vec![]), HashMap::new()); - let spec = &converted.root.requests[0].doc.spec; - - assert!(spec.parameters.is_some()); - assert!(spec.request_body.is_some()); - assert!(spec.responses.is_some()); - } - - #[test] - fn an_empty_parameters_skeleton_is_dropped() { - let endpoint = json!({"id": "r1", "name": "X", "parameters": {}, "requestBody": null}); - let converted = convert(&v1_collection(vec![endpoint], vec![]), HashMap::new()); - - assert!(converted.root.requests[0].doc.spec.is_empty()); - } - - /// The frontend attached this skeleton to every request it created, so - /// keeping it would put three empty maps in every migrated file. - #[test] - fn the_default_parameter_skeleton_is_dropped() { - let endpoint = json!({ - "id": "r1", - "name": "X", - "parameters": {"query": {}, "header": {}, "path": {}} - }); - let converted = convert(&v1_collection(vec![endpoint], vec![]), HashMap::new()); - - assert!(converted.root.requests[0].doc.spec.is_empty()); - - let yaml = serde_yaml_ng::to_string(&converted.root.requests[0].doc).unwrap(); - assert!(!yaml.contains("spec"), "{}", yaml); - } - - #[test] - fn a_populated_parameter_bucket_is_kept_and_empty_siblings_dropped() { - let endpoint = json!({ - "id": "r1", - "name": "X", - "parameters": {"query": {"limit": {"example": "20"}}, "header": {}, "path": {}} - }); - let converted = convert(&v1_collection(vec![endpoint], vec![]), HashMap::new()); - - let parameters = converted.root.requests[0] - .doc - .spec - .parameters - .as_ref() - .unwrap(); - assert!(parameters.get("query").is_some()); - assert!(parameters.get("header").is_none()); - assert!(parameters.get("path").is_none()); - } - - mod bodies { - use super::*; - - fn body_of(data: EndpointData) -> Option { - let mut map = HashMap::new(); - map.insert("r1".to_string(), data); - let converted = convert(&v1_collection(vec![endpoint("r1", "X")], vec![]), map); - converted.root.requests[0].doc.body.clone() - } - - #[test] - fn a_modified_body_becomes_a_json_body() { - let body = body_of(EndpointData { - modified_body: Some("{\"a\": 1}".into()), - ..Default::default() - }); - - assert!( - matches!(body, Some(Body::Known(BodyKind::Json { content })) if content == "{\"a\": 1}") - ); - } - - #[test] - fn form_data_becomes_a_formdata_body() { - let body = body_of(EndpointData { - form_body_data: Some(json!({ - "mode": "formdata", - "fields": [{"key": "a", "value": "b", "enabled": true}] - })), - ..Default::default() - }); - - assert!( - matches!(body, Some(Body::Known(BodyKind::FormData { fields })) if fields.len() == 1) - ); - } - - #[test] - fn urlencoded_becomes_an_urlencoded_body() { - let body = body_of(EndpointData { - form_body_data: Some(json!({"mode": "urlencoded", "fields": []})), - ..Default::default() - }); - - assert!(matches!( - body, - Some(Body::Known(BodyKind::UrlEncoded { .. })) - )); - } - - #[test] - fn a_binary_body_keeps_its_path_and_type() { - let body = body_of(EndpointData { - form_body_data: Some(json!({ - "mode": "binary", - "filePath": "/tmp/x.bin", - "contentType": "application/octet-stream" - })), - ..Default::default() - }); - - match body { - Some(Body::Known(BodyKind::Binary { - file_path, - content_type, - })) => { - assert_eq!(file_path, "/tmp/x.bin"); - assert_eq!(content_type, "application/octet-stream"); - } - other => panic!("expected a binary body, got {:?}", other), - } - } - - #[test] - fn a_text_body_keeps_its_content() { - let body = body_of(EndpointData { - form_body_data: Some(json!({"mode": "text", "content": "hello"})), - ..Default::default() - }); - - assert!( - matches!(body, Some(Body::Known(BodyKind::Text { content })) if content == "hello") - ); - } - - /// Older data stored form fields as a flat object rather than rows. - #[test] - fn legacy_flat_form_fields_normalize_to_rows() { - let body = body_of(EndpointData { - form_body_data: Some(json!({ - "mode": "formdata", - "fields": {"alpha": "one", "beta": "two"} - })), - ..Default::default() - }); - - match body { - Some(Body::Known(BodyKind::FormData { fields })) => { - assert_eq!(fields.len(), 2); - for row in &fields { - assert!(row.get("key").is_some()); - assert!(row.get("value").unwrap().is_string()); - assert_eq!(row["enabled"], true); - assert_eq!(row["type"], "text"); - } - } - other => panic!("expected form data rows, got {:?}", other), - } - } - - /// Three writers produced three key sets for graphqlData; all convert. - #[test] - fn every_graphql_key_set_converts() { - let shapes = vec![ - json!({"query": "{ me }", "variables": "{}", "operationName": "Me"}), - json!({"query": "{ me }", "variables": "{}", "operationName": null}), - json!({"mode": "graphql", "query": "{ me }", "variables": "{}"}), - ]; - - for shape in shapes { - let body = body_of(EndpointData { - graphql_data: Some(shape.clone()), - ..Default::default() - }); - - assert!( - matches!(body, Some(Body::Known(BodyKind::Graphql { ref query, .. })) if query == "{ me }"), - "failed for {}", - shape - ); - } - } - - #[test] - fn form_body_data_wins_over_a_stale_modified_body() { - let body = body_of(EndpointData { - modified_body: Some("stale".into()), - form_body_data: Some(json!({"mode": "text", "content": "current"})), - ..Default::default() - }); - - assert!( - matches!(body, Some(Body::Known(BodyKind::Text { content })) if content == "current") - ); - } - - #[test] - fn an_endpoint_with_no_body_fields_gets_no_body() { - assert!(body_of(EndpointData::default()).is_none()); - } - - #[test] - fn an_empty_graphql_payload_is_not_a_body() { - let body = body_of(EndpointData { - graphql_data: Some(json!({"query": "", "variables": ""})), - ..Default::default() - }); - - assert!(body.is_none()); - } - } - - #[test] - fn grpc_and_mqtt_data_carry_over_verbatim() { - let mut data = HashMap::new(); - data.insert( - "r1".to_string(), - EndpointData { - grpc_data: Some(json!({ - "target": "localhost:50051", - "service": "UserService", - "fullMethod": "/UserService/Get", - "requestJson": "{}", - "metadata": {"x-key": "v"}, - "useTls": false, - "protoPath": null, - "clientStreaming": false, - "serverStreaming": true - })), - mqtt_data: Some(json!({"clientId": "c", "qos": 2})), - websocket_data: Some(json!({"protocols": ["chat"], "messages": []})), - ..Default::default() - }, - ); - - let converted = convert(&v1_collection(vec![endpoint("r1", "X")], vec![]), data); - let doc = &converted.root.requests[0].doc; - - assert_eq!(doc.grpc.as_ref().unwrap()["serverStreaming"], true); - assert_eq!(doc.grpc.as_ref().unwrap()["metadata"]["x-key"], "v"); - assert_eq!(doc.mqtt.as_ref().unwrap()["qos"], 2); - assert_eq!(doc.websocket.as_ref().unwrap()["protocols"][0], "chat"); - } - - /// The four-key gRPC writer produced a subset of the nine-key one. - #[test] - fn the_short_grpc_writer_also_converts() { - let mut data = HashMap::new(); - data.insert( - "r1".to_string(), - EndpointData { - grpc_data: Some(json!({ - "target": "localhost:50051", - "service": "S", - "fullMethod": "/S/M", - "requestJson": "{}" - })), - ..Default::default() - }, - ); - - let converted = convert(&v1_collection(vec![endpoint("r1", "X")], vec![]), data); - assert_eq!( - converted.root.requests[0].doc.grpc.as_ref().unwrap()["service"], - "S" - ); - } - - #[test] - fn scripts_are_renamed_to_their_v2_keys() { - let mut data = HashMap::new(); - data.insert( - "r1".to_string(), - EndpointData { - scripts: Some(json!({ - "preRequestScript": "pre();", - "testScript": "expect(1).toBe(1);" - })), - ..Default::default() - }, - ); - - let converted = convert(&v1_collection(vec![endpoint("r1", "X")], vec![]), data); - let scripts = &converted.root.requests[0].doc.scripts; - - assert_eq!(scripts.pre_request.as_deref(), Some("pre();")); - assert_eq!(scripts.test.as_deref(), Some("expect(1).toBe(1);")); - } - - #[test] - fn empty_scripts_are_dropped() { - let mut data = HashMap::new(); - data.insert( - "r1".to_string(), - EndpointData { - scripts: Some(json!({"preRequestScript": "", "testScript": ""})), - ..Default::default() - }, - ); - - let converted = convert(&v1_collection(vec![endpoint("r1", "X")], vec![]), data); - assert!(converted.root.requests[0].doc.scripts.is_empty()); - } - - #[test] - fn a_request_falls_back_to_its_path_then_its_id_for_a_name() { - let unnamed = json!({"id": "r1", "path": "/pets"}); - let bare = json!({"id": "r2"}); - - let converted = convert(&v1_collection(vec![unnamed, bare], vec![]), HashMap::new()); - - assert_eq!(converted.root.requests[0].doc.name, "/pets"); - assert_eq!(converted.root.requests[1].doc.name, "r2"); - } - - /// v1 emitted a null for every unset field; none may reach the v2 file. - #[test] - fn a_converted_request_writes_no_nulls() { - let mut data = HashMap::new(); - data.insert("r1".to_string(), EndpointData::default()); - - let converted = convert(&v1_collection(vec![endpoint("r1", "X")], vec![]), data); - let yaml = serde_yaml_ng::to_string(&converted.root.requests[0].doc).unwrap(); - - assert!(!yaml.contains("null"), "{}", yaml); - } -} - -#[cfg(test)] -mod end_to_end { - use super::*; - use crate::read::read_collection_dir; - use crate::write::write_collection_dir; - use serde_json::json; - use std::fs; - use tempfile::TempDir; - - /// The whole migration path: a v1 collection converts, writes as a v2 - /// tree, and reads back with everything intact. - #[test] - fn a_v1_collection_survives_the_full_round_trip() { - let collection = Collection { - id: "c1".into(), - name: "Petstore".into(), - base_url: "https://api.example.com".into(), - endpoints: vec![ - json!({"id": "custom_1", "name": "Health", "method": "GET", "path": "/health", "protocol": "http"}), - json!({"id": "custom_2", "name": "Create Pet", "method": "POST", "path": "/pets", "protocol": "http"}), - ], - folders: vec![json!({ - "id": "folder_pets", - "name": "pets", - "authConfig": {"type": "bearer", "config": {"token": ""}}, - "endpoints": [ - {"id": "custom_2", "name": "Create Pet", "method": "POST", "path": "/pets", "protocol": "http"} - ] - })], - default_headers: json!({"Accept": "application/json"}), - auth_config: Some( - json!({"type": "api-key", "config": {"keyName": "X-Key", "keyValue": ""}}), - ), - open_api_spec: Some(json!({"openapi": "3.0.0"})), - storage_path: Some("/machine/specific/path".into()), - storage_parent_path: None, - linked: false, - can_close: false, - git_branch: None, - }; - - let mut data = HashMap::new(); - data.insert( - "custom_2".to_string(), - EndpointData { - modified_body: Some("{\n \"name\": \"Rex\"\n}\n".into()), - headers: vec![json!({"key": "Content-Type", "value": "application/json"})], - scripts: Some( - json!({"preRequestScript": "", "testScript": "expect(1).toBe(1);\n"}), - ), - ..Default::default() - }, - ); - - let temp = TempDir::new().unwrap(); - let mut converted = v1_to_v2(&collection, &data); - converted.variables = vec![json!({"key": "baseUrl", "value": "https://api.example.com"})]; - - write_collection_dir(temp.path(), &mut converted).unwrap(); - - // The layout is what the format promised. - assert!(temp.path().join("collection.yaml").exists()); - assert!(temp.path().join("variables.yaml").exists()); - assert!(temp.path().join("openapi.yaml").exists()); - assert!(temp.path().join("health.yaml").exists()); - assert!(temp.path().join("pets/_folder.yaml").exists()); - assert!(temp.path().join("pets/create-pet.yaml").exists()); - - // The duplicated endpoint became exactly one file. - assert!(!temp.path().join("create-pet.yaml").exists()); - - // The body reads as literal text, not an escaped string. - let request = fs::read_to_string(temp.path().join("pets/create-pet.yaml")).unwrap(); - assert!(request.contains("content: |"), "{}", request); - assert!(!request.contains("\\n"), "{}", request); - assert!(request.contains("test: |"), "{}", request); - - // Nothing machine-specific reached the committed files. - let meta = fs::read_to_string(temp.path().join("collection.yaml")).unwrap(); - assert!(!meta.contains("/machine/specific"), "{}", meta); - assert!( - !meta.contains("3.0.0"), - "the spec must not be inlined: {}", - meta - ); - - // And it all reads back. - let read = read_collection_dir(temp.path()).unwrap(); - assert_eq!(read.requests().len(), 2); - assert_eq!(read.meta.name, "Petstore"); - assert_eq!(read.variables.len(), 1); - assert_eq!(read.open_api_spec.as_ref().unwrap()["openapi"], "3.0.0"); - - let ids: Vec<_> = read.requests().iter().map(|e| e.doc.id.clone()).collect(); - assert!(ids.contains(&"custom_1".to_string())); - assert!(ids.contains(&"custom_2".to_string())); - - assert_eq!(read.root.folders[0].requests[0].doc.name, "Create Pet"); - } - - /// Converting the same v1 collection twice must produce identical bytes, - /// or a migration would show up as a diff on every save. - #[test] - fn converting_twice_produces_identical_bytes() { - let collection = Collection { - id: "c1".into(), - name: "P".into(), - base_url: String::new(), - endpoints: vec![json!({"id": "r1", "name": "Health"})], - folders: vec![], - default_headers: Value::Null, - auth_config: None, - open_api_spec: None, - storage_path: None, - storage_parent_path: None, - linked: false, - can_close: false, - git_branch: None, - }; - - let temp = TempDir::new().unwrap(); - let mut first = v1_to_v2(&collection, &HashMap::new()); - write_collection_dir(temp.path(), &mut first).unwrap(); - let before = fs::read_to_string(temp.path().join("health.yaml")).unwrap(); - - let mut second = v1_to_v2(&collection, &HashMap::new()); - write_collection_dir(temp.path(), &mut second).unwrap(); - let after = fs::read_to_string(temp.path().join("health.yaml")).unwrap(); - - assert_eq!(before, after); - } -} diff --git a/crates/resonance-collections/src/lib.rs b/crates/resonance-collections/src/lib.rs index db98a205..220a1500 100644 --- a/crates/resonance-collections/src/lib.rs +++ b/crates/resonance-collections/src/lib.rs @@ -8,7 +8,6 @@ pub mod fs_secure; pub mod git; pub mod ipc; pub mod layout; -pub mod legacy; pub mod link; pub mod load; pub mod model; @@ -16,7 +15,6 @@ pub mod read; pub mod runners; pub mod secrets; pub mod snapshot; -pub mod startup; pub mod variables; pub mod write; diff --git a/crates/resonance-collections/src/link.rs b/crates/resonance-collections/src/link.rs index e20063f0..dd38abca 100644 --- a/crates/resonance-collections/src/link.rs +++ b/crates/resonance-collections/src/link.rs @@ -13,7 +13,7 @@ use std::collections::HashMap; use std::path::{Path, PathBuf}; -use super::read::Layout; +use super::read::is_collection_dir; /// Store key holding `{collectionId: absolutePath}` for linked collections. pub const LINKED_COLLECTIONS_KEY: &str = "linkedCollections"; @@ -142,7 +142,7 @@ pub fn discover_collection_dirs(root: &Path) -> Result, String> { return Err(format!("{} is not a directory", root.display())); } - if Layout::detect(root).is_some() { + if is_collection_dir(root) { return Ok(vec![root.to_path_buf()]); } @@ -164,7 +164,7 @@ pub fn discover_collection_dirs(root: &Path) -> Result, String> { continue; } - if Layout::detect(&path).is_some() { + if is_collection_dir(&path) { found.push(path); } } @@ -189,7 +189,7 @@ pub fn classify_open(id: &str, path: &Path, index: &HashMap) -> // A stale entry is not a conflict: the collection was moved or // deleted, and re-registering is exactly what the user wants. - if Layout::detect(®istered_path).is_some() { + if is_collection_dir(®istered_path) { return Decision::Conflict { existing: registered_path, same_path_different_id: false, @@ -227,11 +227,6 @@ mod tests { .unwrap(); } - fn v1_collection(dir: &Path) { - fs::create_dir_all(dir).unwrap(); - fs::write(dir.join("collection.json"), "{}").unwrap(); - } - fn index_of(pairs: &[(&str, &Path)]) -> HashMap { pairs .iter() @@ -253,14 +248,6 @@ mod tests { ); } - #[test] - fn a_v1_directory_is_returned_as_itself() { - let temp = TempDir::new().unwrap(); - v1_collection(temp.path()); - - assert_eq!(discover_collection_dirs(temp.path()).unwrap().len(), 1); - } - #[test] fn a_parent_yields_only_its_collection_children() { let temp = TempDir::new().unwrap(); diff --git a/crates/resonance-collections/src/load.rs b/crates/resonance-collections/src/load.rs index 0ef51dd5..a32cee1e 100644 --- a/crates/resonance-collections/src/load.rs +++ b/crates/resonance-collections/src/load.rs @@ -3,14 +3,12 @@ use std::path::{Path, PathBuf}; -use crate::read::{Layout, LoadedCollection, read_collection_dir}; +use crate::read::{LoadedCollection, is_collection_dir, read_collection_dir}; #[derive(Debug, thiserror::Error)] pub enum LoadError { #[error("{0} is not a collection directory (no collection.yaml)")] NotACollection(PathBuf), - #[error("{0} uses the legacy collection.json layout; open it in Resonance once to convert it")] - LegacyLayout(PathBuf), #[error("{0}")] Read(String), #[error("no request matches {selector}")] @@ -30,19 +28,15 @@ pub enum LoadError { Environment(String), } -/// Reads a v2 collection directory. -/// -/// A v1 directory is refused rather than converted: conversion rewrites the -/// user's files, which is the desktop's job, not a CI run's. +/// Reads a collection directory. /// /// @param dir - The collection directory /// @returns The collection in memory pub fn load_collection(dir: &Path) -> Result { - match Layout::detect(dir) { - Some(Layout::V2) => read_collection_dir(dir).map_err(LoadError::Read), - Some(Layout::V1) => Err(LoadError::LegacyLayout(dir.to_path_buf())), - None => Err(LoadError::NotACollection(dir.to_path_buf())), + if !is_collection_dir(dir) { + return Err(LoadError::NotACollection(dir.to_path_buf())); } + read_collection_dir(dir).map_err(LoadError::Read) } #[cfg(test)] @@ -71,7 +65,7 @@ mod tests { } #[test] - fn load_collection_refuses_v1_with_conversion_hint() { + fn load_collection_rejects_a_dir_holding_only_a_collection_json() { let temp = TempDir::new().unwrap(); fs::write( temp.path().join("collection.json"), @@ -80,8 +74,7 @@ mod tests { .unwrap(); let error = load_collection(temp.path()).unwrap_err(); - assert!(matches!(error, LoadError::LegacyLayout(_))); - assert!(error.to_string().contains("open it in Resonance")); + assert!(matches!(error, LoadError::NotACollection(_))); } #[test] diff --git a/crates/resonance-collections/src/read.rs b/crates/resonance-collections/src/read.rs index 67d94abe..db882b4d 100644 --- a/crates/resonance-collections/src/read.rs +++ b/crates/resonance-collections/src/read.rs @@ -11,44 +11,19 @@ use std::collections::HashSet; use std::fs; use std::path::{Path, PathBuf}; -use super::layout::{ - COLLECTION_JSON, COLLECTION_YAML, FOLDER_YAML, OPENAPI_YAML, VARIABLES_YAML, is_reserved_dir, -}; +use super::layout::{COLLECTION_YAML, FOLDER_YAML, OPENAPI_YAML, VARIABLES_YAML, is_reserved_dir}; use super::model::{ CollectionDoc, FORMAT_VERSION, FolderDoc, FormatProbe, RequestDoc, probe_format, }; use super::runners::{RunnerEntry, read_runners}; -/// Which on-disk format a collection directory uses. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum Layout { - /// `collection.json` plus a flat `requests/` directory. - V1, - /// `collection.yaml` plus one file per request, folders as directories. - V2, -} - -impl Layout { - /// Identifies a collection directory by its marker file. - /// - /// V2 wins when both markers are present, which is what an interrupted - /// conversion leaves behind: the v2 tree is complete by then, and the v1 - /// files are stale leftovers the next save removes. - /// - /// @param dir - Directory to inspect - /// @returns The layout, or None when this is not a collection directory - pub fn detect(dir: &Path) -> Option { - if !dir.is_dir() { - return None; - } - if dir.join(COLLECTION_YAML).exists() { - return Some(Layout::V2); - } - if dir.join(COLLECTION_JSON).exists() { - return Some(Layout::V1); - } - None - } +/// Reports whether a directory holds a collection, marked by its +/// `collection.yaml`. +/// +/// @param dir - Directory to inspect +/// @returns True when the marker file is present +pub fn is_collection_dir(dir: &Path) -> bool { + dir.is_dir() && dir.join(COLLECTION_YAML).exists() } /// One request, paired with the file it came from so a later write can rename @@ -282,7 +257,7 @@ fn read_folder(dir: &Path, at_root: bool) -> Result { } continue; } - if Layout::detect(&path).is_some() { + if is_collection_dir(&path) { return Err(format!( "{} contains a nested collection at {}; open it on its own instead", dir.display(), @@ -457,34 +432,18 @@ mod tests { } #[test] - fn detects_a_v2_directory_by_its_marker() { + fn detects_a_collection_directory_by_its_marker() { let temp = TempDir::new().unwrap(); minimal_collection(temp.path()); - assert_eq!(Layout::detect(temp.path()), Some(Layout::V2)); - } - - #[test] - fn detects_a_v1_directory_by_its_marker() { - let temp = TempDir::new().unwrap(); - write(temp.path(), COLLECTION_JSON, "{}"); - assert_eq!(Layout::detect(temp.path()), Some(Layout::V1)); - } - - /// An interrupted conversion leaves both markers. The v2 tree is complete - /// by that point, so it wins and the next save clears the v1 leftovers. - #[test] - fn v2_wins_when_both_markers_are_present() { - let temp = TempDir::new().unwrap(); - minimal_collection(temp.path()); - write(temp.path(), COLLECTION_JSON, "{}"); - assert_eq!(Layout::detect(temp.path()), Some(Layout::V2)); + assert!(is_collection_dir(temp.path())); } #[test] fn an_ordinary_directory_is_not_a_collection() { let temp = TempDir::new().unwrap(); write(temp.path(), "notes.txt", "hello"); - assert_eq!(Layout::detect(temp.path()), None); + write(temp.path(), "collection.json", "{}"); + assert!(!is_collection_dir(temp.path())); } #[test] @@ -935,19 +894,6 @@ mod tests { assert!(error.contains("nested collection"), "{}", error); } - #[test] - fn a_nested_v1_collection_is_also_refused() { - let temp = TempDir::new().unwrap(); - minimal_collection(temp.path()); - write(temp.path(), "inner/collection.json", "{}"); - - assert!( - read_collection_dir(temp.path()) - .unwrap_err() - .contains("nested collection") - ); - } - /// Same seq and same name used to fall back to fs::read_dir order, which /// differs by filesystem, so two people saw different orders for one commit. #[test] diff --git a/crates/resonance-collections/src/snapshot.rs b/crates/resonance-collections/src/snapshot.rs index 2dceef27..efeb5bf0 100644 --- a/crates/resonance-collections/src/snapshot.rs +++ b/crates/resonance-collections/src/snapshot.rs @@ -4,16 +4,10 @@ use resonance_core::scripts::ScriptData; use serde::{Deserialize, Serialize}; use serde_json::{Value, json}; -use std::{ - collections::{HashMap, HashSet}, - fs, - path::Path, -}; +use std::collections::{HashMap, HashSet}; use crate::{ - ipc::{self, Collection, EndpointData}, - layout, - legacy::read_json_file, + ipc::{self, EndpointData}, read::{FolderNode, LoadedCollection, folder_display_name}, }; @@ -59,7 +53,7 @@ impl CollectionSnapshot { variables: Vec::new(), targets: endpoint_ids .iter() - .map(|id| target(id, None, EndpointData::default(), Vec::new(), None)) + .map(|id| target(id, None, EndpointData::default(), Vec::new())) .collect(), } } @@ -95,13 +89,11 @@ fn target( endpoint: Option, mut persisted: EndpointData, inherited_auth: Vec, - fallback: Option<&ScriptData>, ) -> TargetSnapshot { let scripts = persisted .scripts .take() .and_then(|value| serde_json::from_value(value).ok()) - .or_else(|| fallback.cloned()) .unwrap_or_else(|| ScriptData { pre_request_script: String::new(), test_script: String::new(), @@ -120,12 +112,7 @@ fn auth(source: Value, config: Option<&Value>, default: &str) -> Value { .unwrap_or_else(|| json!({"type": default, "config": {}})) }) } -pub fn from_loaded( - id: &str, - loaded: &LoadedCollection, - ids: &[String], - scripts: &HashMap, -) -> CollectionSnapshot { +pub fn from_loaded(id: &str, loaded: &LoadedCollection, ids: &[String]) -> CollectionSnapshot { let wanted: HashSet<&str> = ids.iter().map(String::as_str).collect(); let mut found = HashMap::new(); let collection_auth = auth( @@ -158,13 +145,7 @@ pub fn from_loaded( let (endpoint, persisted, mut parents) = found.remove(endpoint_id).unwrap_or_default(); parents.push(collection_auth.clone()); - target( - endpoint_id, - endpoint, - persisted, - parents, - scripts.get(&format!("{id}_{endpoint_id}")), - ) + target(endpoint_id, endpoint, persisted, parents) }) .collect(), } @@ -206,126 +187,3 @@ fn visit( parents.pop(); } } - -/// The legacy path preserves metadata verbatim rather than converting through -/// v2, which would lose legacy endpoint headers/security. Enumerate sidecars -/// once, retaining the old exact-name-before-suffix lookup precedence. -pub fn read_v1( - id: &str, - dir: &Path, - ids: &[String], - scripts: &HashMap, -) -> Result { - let collection: Collection = read_json_file(&dir.join(layout::COLLECTION_JSON))?; - let mut found = HashMap::new(); - legacy_folders(&collection.folders, &mut Vec::new(), &mut found); - for endpoint in &collection.endpoints { - if let Some(id) = endpoint["id"].as_str() { - found - .entry(id.into()) - .or_insert_with(|| (Some(endpoint.clone()), EndpointData::default(), Vec::new())); - } - } - let requests = dir.join("requests"); - let files = if requests.exists() { - fs::read_dir(&requests) - .map_err(|e| e.to_string()) - .and_then(|entries| { - entries - .map(|entry| { - entry - .map(|e| { - let path = e.path(); - let is_file = path.is_file(); - (path, is_file) - }) - .map_err(|e| e.to_string()) - }) - .collect::, _>>() - }) - } else { - Ok(Vec::new()) - }; - let collection_auth = auth( - json!({"kind":"collection"}), - collection.auth_config.as_ref(), - "none", - ); - Ok(CollectionSnapshot { - collection_id: id.into(), - collection: Some(CollectionMetadata { - id: collection.id, - base_url: collection.base_url, - default_headers: collection.default_headers, - }), - variables: read_json_file(&dir.join("variables.json")).unwrap_or_default(), - targets: ids - .iter() - .map(|endpoint_id| { - let exact = format!("{endpoint_id}.json"); - let suffix = format!("--{}.json", layout::sanitize_file_component(endpoint_id)); - let read = match &files { - Ok(files) => { - let file = files - .iter() - .find(|(p, _)| p.file_name().and_then(|s| s.to_str()) == Some(&exact)) - .or_else(|| { - files.iter().find(|(p, is_file)| { - *is_file - && p.file_name() - .and_then(|s| s.to_str()) - .is_some_and(|s| s.ends_with(&suffix)) - }) - }); - file.map(|(path, _)| read_json_file::(path)) - .transpose() - } - // A failed inventory is a per-request data read failure, - // not a missing collection. Exact legacy paths can still - // be readable when directory enumeration is denied. - Err(_) => layout::find_endpoint_data_file(&requests, endpoint_id) - .and_then(|file| file.map(|path| read_json_file(&path)).transpose()), - }; - let fallback = read - .is_ok() - .then(|| scripts.get(&format!("{id}_{endpoint_id}"))) - .flatten(); - let persisted = read.ok().flatten().unwrap_or_default(); - let (endpoint, _, mut parents) = found.remove(endpoint_id).unwrap_or_default(); - parents.push(collection_auth.clone()); - target(endpoint_id, endpoint, persisted, parents, fallback) - }) - .collect(), - }) -} - -fn legacy_folders( - folders: &[Value], - parents: &mut Vec, - found: &mut HashMap, -) { - for folder in folders { - parents.push(auth( - json!({"kind":"folder", "folderId":folder["id"]}), - folder.get("authConfig"), - "inherit", - )); - if let Some(endpoints) = folder["endpoints"].as_array() { - for endpoint in endpoints { - if let Some(id) = endpoint["id"].as_str() { - found.entry(id.into()).or_insert_with(|| { - ( - Some(endpoint.clone()), - EndpointData::default(), - parents.iter().rev().cloned().collect(), - ) - }); - } - } - } - if let Some(children) = folder["folders"].as_array() { - legacy_folders(children, parents, found); - } - parents.pop(); - } -} diff --git a/crates/resonance-collections/src/startup.rs b/crates/resonance-collections/src/startup.rs deleted file mode 100644 index f96244b7..00000000 --- a/crates/resonance-collections/src/startup.rs +++ /dev/null @@ -1,192 +0,0 @@ -//! Collection conversions run by the startup migration chain. -//! -//! Both work on one app-owned collection directory at a time and need no -//! AppHandle, so they can run in `setup` before the store plugin is touched. - -use serde_json::{Map, Value}; -use std::path::Path; - -use super::ipc::scripts_from_endpoint_data; -use super::legacy::load_v1_dir; -use super::read::{FolderNode, Layout, read_collection_dir}; -use super::write::write_collection_dir; - -/// Rewrites a v1 collection directory as a v2 tree. -/// -/// @param dir - The collection directory -/// @returns True when the directory was converted, false when it was not v1 -pub fn convert_v1_dir(dir: &Path) -> Result { - if Layout::detect(dir) != Some(Layout::V1) { - return Ok(false); - } - let mut loaded = load_v1_dir(dir)?; - write_collection_dir(dir, &mut loaded)?; - Ok(true) -} - -/// Moves the global-store scripts of one v2 collection into its request files. -/// -/// Only entries naming a request of this collection are consumed: a request -/// without scripts takes the stored ones, a request that already has scripts -/// keeps its own. Entries for unknown requests are left alone, since an id -/// such as `c_1` would otherwise claim the entries of a collection `c`'s -/// neighbour `c_1`. -/// -/// @param dir - The collection directory -/// @param scripts - The `persistedScripts` map; consumed entries are removed -/// @returns How many requests received scripts -pub fn absorb_store_scripts(dir: &Path, scripts: &mut Map) -> Result { - if Layout::detect(dir) != Some(Layout::V2) { - return Ok(0); - } - let mut loaded = read_collection_dir(dir)?; - let prefix = format!("{}_", loaded.meta.id); - let keys: Vec = scripts - .keys() - .filter(|key| key.starts_with(&prefix)) - .cloned() - .collect(); - if keys.is_empty() { - return Ok(0); - } - - let mut absorbed = 0; - for key in &keys { - let Some(doc) = find_request_mut(&mut loaded.root, &key[prefix.len()..]) else { - continue; - }; - let stored = scripts.remove(key); - if doc.scripts.is_empty() { - doc.scripts = scripts_from_endpoint_data(stored.as_ref()); - if !doc.scripts.is_empty() { - absorbed += 1; - } - } - } - - if absorbed > 0 { - write_collection_dir(dir, &mut loaded)?; - } - Ok(absorbed) -} - -fn find_request_mut<'a>( - node: &'a mut FolderNode, - request_id: &str, -) -> Option<&'a mut super::model::RequestDoc> { - if let Some(entry) = node - .requests - .iter_mut() - .find(|entry| entry.doc.id == request_id) - { - return Some(&mut entry.doc); - } - node.folders - .iter_mut() - .find_map(|folder| find_request_mut(folder, request_id)) -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::read::read_collection_dir; - use serde_json::json; - use std::fs; - use tempfile::TempDir; - - fn write_v1(dir: &Path) { - fs::write( - dir.join("collection.json"), - json!({ - "id": "c_1", - "name": "Pets", - "baseUrl": "https://api.example.com", - "endpoints": [ - {"id": "r1", "name": "List", "method": "GET", "path": "/pets", "protocol": "http"}, - {"id": "r2", "name": "Create", "method": "POST", "path": "/pets", "protocol": "http"} - ], - "folders": [] - }) - .to_string(), - ) - .unwrap(); - fs::create_dir_all(dir.join("requests")).unwrap(); - fs::write( - dir.join("requests/create--r2.json"), - json!({"scripts": {"preRequestScript": "", "testScript": "kept();"}}).to_string(), - ) - .unwrap(); - } - - fn script_test(dir: &Path, request_id: &str) -> Option { - read_collection_dir(dir) - .unwrap() - .requests() - .into_iter() - .find(|entry| entry.doc.id == request_id) - .and_then(|entry| entry.doc.scripts.test.clone()) - } - - #[test] - fn a_v1_dir_is_rewritten_as_v2_without_leftovers() { - let temp = TempDir::new().unwrap(); - write_v1(temp.path()); - - assert!(convert_v1_dir(temp.path()).unwrap()); - - assert_eq!(Layout::detect(temp.path()), Some(Layout::V2)); - assert!(!temp.path().join("collection.json").exists()); - assert!(!temp.path().join("requests").exists()); - assert_eq!(script_test(temp.path(), "r2").as_deref(), Some("kept();")); - } - - #[test] - fn converting_is_a_no_op_for_v2_and_non_collection_dirs() { - let temp = TempDir::new().unwrap(); - assert!(!convert_v1_dir(temp.path()).unwrap()); - - write_v1(temp.path()); - convert_v1_dir(temp.path()).unwrap(); - assert!(!convert_v1_dir(temp.path()).unwrap()); - } - - #[test] - fn store_scripts_fill_empty_requests_and_never_override_file_scripts() { - let temp = TempDir::new().unwrap(); - write_v1(temp.path()); - convert_v1_dir(temp.path()).unwrap(); - - let mut store = Map::new(); - store.insert( - "c_1_r1".into(), - json!({"preRequestScript": "pre();", "testScript": "stored();"}), - ); - store.insert("c_1_r2".into(), json!({"testScript": "stale();"})); - store.insert("c_1_gone".into(), json!({"testScript": "orphan();"})); - store.insert("c_10_r1".into(), json!({"testScript": "other();"})); - - assert_eq!(absorb_store_scripts(temp.path(), &mut store).unwrap(), 1); - - assert_eq!(script_test(temp.path(), "r1").as_deref(), Some("stored();")); - assert_eq!(script_test(temp.path(), "r2").as_deref(), Some("kept();")); - let mut left: Vec<_> = store.keys().cloned().collect(); - left.sort(); - assert_eq!(left, vec!["c_10_r1", "c_1_gone"]); - } - - #[test] - fn a_shorter_collection_id_never_claims_a_longer_ids_entries() { - let temp = TempDir::new().unwrap(); - write_v1(temp.path()); - convert_v1_dir(temp.path()).unwrap(); - - let mut store = Map::new(); - store.insert("c_1_r1".into(), json!({"testScript": "mine();"})); - store.insert("c_1_x_r9".into(), json!({"testScript": "neighbour();"})); - - absorb_store_scripts(temp.path(), &mut store).unwrap(); - - assert_eq!(store.keys().collect::>(), vec!["c_1_x_r9"]); - assert_eq!(script_test(temp.path(), "r1").as_deref(), Some("mine();")); - } -} diff --git a/crates/resonance-collections/src/write.rs b/crates/resonance-collections/src/write.rs index 40c4aade..6f4c2009 100644 --- a/crates/resonance-collections/src/write.rs +++ b/crates/resonance-collections/src/write.rs @@ -17,8 +17,8 @@ use std::io::Write; use std::path::{Path, PathBuf}; use super::layout::{ - COLLECTION_JSON, COLLECTION_YAML, FOLDER_YAML, OPENAPI_YAML, VARIABLES_YAML, - find_available_folder_dir, find_available_path, is_reserved_dir, request_stem, slugify, + COLLECTION_YAML, FOLDER_YAML, OPENAPI_YAML, VARIABLES_YAML, find_available_folder_dir, + find_available_path, is_reserved_dir, request_stem, slugify, }; use super::read::{FolderNode, LoadedCollection, RequestEntry, folder_display_name}; use crate::fs_secure::{restrict_dir, restrict_file}; @@ -155,6 +155,24 @@ pub fn relocate(current: &Path, desired: &Path) -> Result<(), String> { }) } +/// Points every descendant's `source` at the directory its folder moved to, +/// so the recursive write sees each file where it now lives instead of +/// treating it as a stranger that has claimed the name. +fn rebase_sources(node: &mut FolderNode, from: &Path, to: &Path) { + let rebase = |source: &mut Option| { + if let Some(rest) = source.as_deref().and_then(|p| p.strip_prefix(from).ok()) { + *source = Some(to.join(rest)); + } + }; + for entry in &mut node.requests { + rebase(&mut entry.source); + } + for child in &mut node.folders { + rebase(&mut child.source); + rebase_sources(child, from, to); + } +} + /// Writes one folder's requests and recurses into its subfolders. /// /// Returns every path it wrote or kept, so the caller can tell which files in @@ -205,6 +223,7 @@ fn write_folder(dir: &Path, node: &mut FolderNode, at_root: bool) -> Result Result Result<(), String> { - let collection_json = dir.join(COLLECTION_JSON); - if collection_json.exists() { - fs::remove_file(&collection_json) - .map_err(|e| format!("Failed to remove {}: {}", collection_json.display(), e))?; - } - - let variables_json = dir.join("variables.json"); - if variables_json.exists() { - fs::remove_file(&variables_json) - .map_err(|e| format!("Failed to remove {}: {}", variables_json.display(), e))?; - } - - let requests = dir.join("requests"); - if requests.is_dir() { - fs::remove_dir_all(&requests) - .map_err(|e| format!("Failed to remove {}: {}", requests.display(), e))?; - } - - Ok(()) -} - /// Writes a whole collection to a directory. /// /// `collection.yaml` is written last on purpose: it is the marker that makes a @@ -267,8 +260,6 @@ pub fn write_collection_dir(dir: &Path, collection: &mut LoadedCollection) -> Re write_if_changed(&dir.join(COLLECTION_YAML), &to_yaml(&collection.meta)?)?; - remove_v1_files(dir)?; - Ok(()) } @@ -353,6 +344,31 @@ mod tests { } } + #[test] + fn renaming_a_folder_moves_its_subtree_without_copies() { + let temp = TempDir::new().unwrap(); + let mut written = collection(vec![]); + let mut pets = folder("f1", "Pets", vec![request("r1", "List")]); + pets.folders + .push(folder("f2", "Sub", vec![request("r2", "Item")])); + written.root.folders.push(pets); + write_collection_dir(temp.path(), &mut written).unwrap(); + + let mut read = read_collection_dir(temp.path()).unwrap(); + read.root.folders[0].meta.as_mut().unwrap().name = "Animals".into(); + write_collection_dir(temp.path(), &mut read).unwrap(); + + assert!(temp.path().join("animals/list.yaml").exists()); + assert!(temp.path().join("animals/sub/item.yaml").exists()); + assert!(!temp.path().join("animals/list-2.yaml").exists()); + assert!(!temp.path().join("animals/sub-2").exists()); + assert!(!temp.path().join("pets").exists()); + let reread = read_collection_dir(temp.path()).unwrap(); + assert_eq!(reread.requests().len(), 2); + assert_eq!(reread.root.folders.len(), 1); + assert_eq!(reread.root.folders[0].folders.len(), 1); + } + #[test] fn a_root_folder_named_runners_gets_a_suffix() { let temp = TempDir::new().unwrap(); @@ -857,29 +873,6 @@ mod tests { assert_eq!(read.open_api_spec.unwrap()["openapi"], "3.0.0"); } - #[test] - fn a_v2_write_removes_leftover_v1_files() { - let temp = TempDir::new().unwrap(); - fs::write(temp.path().join(COLLECTION_JSON), "{}").unwrap(); - fs::write(temp.path().join("variables.json"), "[]").unwrap(); - fs::create_dir(temp.path().join("requests")).unwrap(); - fs::write(temp.path().join("requests/a--b.json"), "{}").unwrap(); - - let mut c = collection(vec![request("r1", "Health")]); - write_collection_dir(temp.path(), &mut c).unwrap(); - - assert!(!temp.path().join(COLLECTION_JSON).exists()); - assert!(!temp.path().join("variables.json").exists()); - assert!(!temp.path().join("requests").exists()); - } - - #[test] - fn removing_v1_files_is_safe_to_repeat() { - let temp = TempDir::new().unwrap(); - remove_v1_files(temp.path()).unwrap(); - remove_v1_files(temp.path()).unwrap(); - } - #[cfg(unix)] #[test] fn every_written_file_is_owner_only() { diff --git a/crates/resonance-collections/tests/snapshot.rs b/crates/resonance-collections/tests/snapshot.rs index 78e0156a..90e7a677 100644 --- a/crates/resonance-collections/tests/snapshot.rs +++ b/crates/resonance-collections/tests/snapshot.rs @@ -1,25 +1,8 @@ use resonance_collections::{read::read_collection_dir, snapshot}; use serde_json::json; -use std::{collections::HashMap, fs}; +use std::fs; use tempfile::TempDir; -#[test] -fn an_unreadable_sidecar_directory_does_not_make_the_collection_missing() { - let dir = TempDir::new().unwrap(); - fs::write( - dir.path().join("collection.json"), - json!({"id":"c", "name":"C", "endpoints":[{"id":"e"}]}).to_string(), - ) - .unwrap(); - // A file where the directory should be reproduces a sidecar read error - // independently of the current user's filesystem privileges. - fs::write(dir.path().join("requests"), "not a directory").unwrap(); - let snapshot = snapshot::read_v1("c", dir.path(), &["e".into()], &HashMap::new()).unwrap(); - assert!(snapshot.collection.is_some()); - assert_eq!(snapshot.targets[0].endpoint.as_ref().unwrap()["id"], "e"); - assert!(snapshot.targets[0].scripts.test_script.is_empty()); -} - #[test] fn a_new_capture_revalidates_disk_while_the_previous_snapshot_stays_owned() { use resonance_collections::cache::{CollectionCache, read_collection_dir_cached}; @@ -41,7 +24,6 @@ fn a_new_capture_revalidates_disk_while_the_previous_snapshot_stays_owned() { "c", &read_collection_dir_cached(&cache, dir.path()).unwrap(), &["e".into()], - &HashMap::new(), ) }; let before = capture(); @@ -117,12 +99,7 @@ fn snapshot_selects_requests_with_their_folder_auth_and_live_data() { fs::write(dir.path().join(name), content).unwrap(); } let loaded = read_collection_dir(dir.path()).unwrap(); - let result = snapshot::from_loaded( - "c", - &loaded, - &["e".into(), "missing".into()], - &HashMap::new(), - ); + let result = snapshot::from_loaded("c", &loaded, &["e".into(), "missing".into()]); let value = serde_json::to_value(result).unwrap(); assert_eq!( value["collection"], @@ -150,54 +127,3 @@ fn snapshot_selects_requests_with_their_folder_auth_and_live_data() { ); assert!(value["targets"][1]["endpoint"].is_null()); } - -#[test] -fn legacy_snapshot_keeps_metadata_sidecar_precedence_and_script_fallback() { - let dir = TempDir::new().unwrap(); - fs::create_dir(dir.path().join("requests")).unwrap(); - fs::write(dir.path().join("collection.json"), json!({ - "id":"c", "name":"C", "endpoints":[{"id":"e", "headers":{"X-Legacy":"kept"}, "security":{"type":"bearer","config":{"token":"legacy"}}}] - }).to_string()).unwrap(); - fs::write( - dir.path().join("requests/e.json"), - json!({"modifiedBody":"preferred"}).to_string(), - ) - .unwrap(); - fs::write( - dir.path().join("requests/name--e.json"), - json!({"modifiedBody":"wrong"}).to_string(), - ) - .unwrap(); - let scripts = HashMap::from([( - "c_e".into(), - resonance_core::scripts::ScriptData { - pre_request_script: "pre()".into(), - test_script: "test()".into(), - }, - )]); - let result = snapshot::read_v1("c", dir.path(), &["e".into()], &scripts).unwrap(); - let value = serde_json::to_value(result).unwrap(); - assert_eq!( - value["targets"][0]["endpoint"]["headers"]["X-Legacy"], - "kept" - ); - assert_eq!( - value["targets"][0]["persisted"]["modifiedBody"], - "preferred" - ); - assert_eq!(value["targets"][0]["scripts"]["preRequestScript"], "pre()"); - assert!(dir.path().join("collection.json").exists()); - assert!(!dir.path().join("collection.yaml").exists()); - - fs::write( - dir.path().join("requests/e.json"), - json!({"scripts":{"preRequestScript":"", "testScript":""}}).to_string(), - ) - .unwrap(); - let cleared = snapshot::read_v1("c", dir.path(), &["e".into()], &scripts).unwrap(); - assert!(cleared.targets[0].scripts.pre_request_script.is_empty()); - - fs::write(dir.path().join("requests/e.json"), "invalid json").unwrap(); - let broken = snapshot::read_v1("c", dir.path(), &["e".into()], &scripts).unwrap(); - assert!(broken.targets[0].scripts.pre_request_script.is_empty()); -} diff --git a/crates/resonance-core/Cargo.toml b/crates/resonance-core/Cargo.toml index cb094f3a..2377d589 100644 --- a/crates/resonance-core/Cargo.toml +++ b/crates/resonance-core/Cargo.toml @@ -23,6 +23,7 @@ tokio-rustls.workspace = true rustls.workspace = true webpki-roots.workspace = true uuid.workspace = true +getrandom.workspace = true chrono.workspace = true encoding_rs = "0.8" regex.workspace = true diff --git a/crates/resonance-core/src/cookies.rs b/crates/resonance-core/src/cookies.rs index dd5a610c..a17ad29d 100644 --- a/crates/resonance-core/src/cookies.rs +++ b/crates/resonance-core/src/cookies.rs @@ -20,7 +20,10 @@ pub fn header(cookies: &[Cookie], url: &str) -> Option { .filter(|c| domain_matches(&c.domain, &host, c.host_only)) .filter(|c| { let path = c.path.as_deref().unwrap_or("/"); - path == "/" || url.path() == path || url.path().starts_with(&format!("{path}/")) + path == "/" + || url.path() == path + || (path.ends_with('/') && url.path().starts_with(path)) + || url.path().starts_with(&format!("{path}/")) }) .map(|c| format!("{}={}", c.name, c.value)) .collect(); @@ -161,6 +164,16 @@ mod tests { ); assert_eq!(header(&jar, "http://example.test/api"), None); assert_eq!(header(&jar, "https://example.test/apix"), None); + let mut slash = vec![]; + apply( + &mut slash, + &response_changes(&["s=1; Path=/api/".into()], "https://example.test/api/"), + ); + assert_eq!( + header(&slash, "https://example.test/api/users"), + Some("s=1".into()) + ); + assert_eq!(header(&slash, "https://example.test/api"), None); apply( &mut jar, &response_changes( diff --git a/crates/resonance-core/src/execution.rs b/crates/resonance-core/src/execution.rs index b9357dc1..c4104a5b 100644 --- a/crates/resonance-core/src/execution.rs +++ b/crates/resonance-core/src/execution.rs @@ -128,9 +128,7 @@ impl Session { "" } ); - if let Some(auth) = self.refreshed.get(&key) { - return auth.clone(); - } + let raw = self.refreshed.get(&key).cloned().unwrap_or(raw); let resolved = processor.value(&raw, vars); let config = &resolved["config"]; if resolved["type"] != "oauth2" diff --git a/crates/resonance-core/src/scripts.rs b/crates/resonance-core/src/scripts.rs index aecbb4a9..3092db6a 100644 --- a/crates/resonance-core/src/scripts.rs +++ b/crates/resonance-core/src/scripts.rs @@ -250,8 +250,12 @@ fn execute_script( } Ok(()) } - Err(ScriptRunError::Js(_)) if ctx.borrow().execution.skipped => Ok(()), + Err(ScriptRunError::Js(_)) if ctx.borrow().execution.skipped => { + collect_test_results(&mut context, &ctx); + Ok(()) + } Err(ScriptRunError::Js(e)) => { + collect_test_results(&mut context, &ctx); ctx.borrow_mut().execution.next = NextRequest::Unchanged; if capture_request { let _ = capture_request_mutations(&mut context, &ctx, baseline.as_deref()); @@ -2473,6 +2477,20 @@ mod tests { .collect() } + #[test] + fn tests_that_ran_before_a_throw_are_reported() { + let script = r#" + pm.test("status", function () { pm.expect(pm.response.code).to.equal(200); }); + JSON.parse("not json"); + "#; + let result = run_test_script(script, ok_response(), &[]); + assert!(!result.success); + assert_eq!( + results(&result), + vec![(true, results(&result)[0].1.clone())] + ); + } + #[test] fn crypto_matches_known_vectors() { let script = r#" diff --git a/crates/resonance-core/src/variables.rs b/crates/resonance-core/src/variables.rs index 04268d92..193252bc 100644 --- a/crates/resonance-core/src/variables.rs +++ b/crates/resonance-core/src/variables.rs @@ -107,7 +107,9 @@ impl VariableProcessor { } } fn random() -> f64 { - (uuid::Uuid::new_v4().as_u128() as u64 >> 11) as f64 / ((1u64 << 53) as f64) + let mut bytes = [0u8; 8]; + getrandom::fill(&mut bytes).expect("OS random number generator unavailable"); + (u64::from_le_bytes(bytes) >> 11) as f64 / ((1u64 << 53) as f64) } fn int(min: i64, max: i64) -> i64 { min + (random() * (max as f64 - min as f64 + 1.0)).floor() as i64 @@ -328,3 +330,16 @@ const LOREM_WORDS: &[&str] = &[ "nisi", "aliquip", ]; + +#[cfg(test)] +mod random_tests { + use super::random; + + #[test] + fn random_covers_the_whole_unit_interval() { + let samples: Vec = (0..10_000).map(|_| random()).collect(); + assert!(samples.iter().all(|v| (0.0..1.0).contains(v))); + assert!(samples.iter().any(|v| *v < 0.25)); + assert!(samples.iter().any(|v| *v >= 0.75)); + } +} diff --git a/crates/resonance-core/tests/lifecycle.rs b/crates/resonance-core/tests/lifecycle.rs index 6e621ef2..6d18bdb5 100644 --- a/crates/resonance-core/tests/lifecycle.rs +++ b/crates/resonance-core/tests/lifecycle.rs @@ -202,3 +202,46 @@ async fn inherited_oauth_renews_once_and_reuses_credentials_without_frontend_cal assert!(effects[0]["changes"].get("clientId").is_none()); server.abort(); } + +#[tokio::test] +async fn a_renewed_token_that_expires_mid_run_is_renewed_again() { + let tokens = Arc::new(std::sync::atomic::AtomicUsize::new(0)); + let counter = tokens.clone(); + let (url, server) = serve(Router::new().fallback(any( + move |request: axum::extract::Request| { + let counter = counter.clone(); + async move { + if request.uri().path() == "/token" { + let n = counter.fetch_add(1, std::sync::atomic::Ordering::SeqCst) + 1; + let body = axum::body::to_bytes(request.into_body(), 8192) + .await + .unwrap(); + let body = String::from_utf8(body.to_vec()).unwrap(); + let expected = if n == 1 { "refresh-0" } else { "refresh-1" }; + assert!(body.contains(&format!("refresh_token={expected}")), "{body}"); + json!({"access_token":format!("fresh-{n}"),"refresh_token":format!("refresh-{n}"),"expires_in":1}) + .to_string() + } else { + request.headers()["authorization"] + .to_str() + .unwrap() + .to_string() + } + } + }, + ))) + .await; + let auth = json!({"type":"oauth2","config":{"token":"expired","expiresAt":1,"grantType":"authorization_code","refreshToken":"refresh-0","clientId":"id","tokenUrl":format!("{url}/token")}}); + let request = + json!({"collectionId":"c","endpointId":"e","url":format!("{url}/api"),"auth":auth}); + let input: RunInput = serde_json::from_value(json!({"executionId":"oauth-expiry","runnerName":"oauth","context":{},"requests":[request,request]})).unwrap(); + let result = execute_run(input, Cancellation::default(), |_| {}).await; + let result = serde_json::to_value(result).unwrap(); + assert_eq!(result["passed"], 2, "{result}"); + assert_eq!(tokens.load(std::sync::atomic::Ordering::SeqCst), 2); + assert_eq!( + result["requests"][1]["apiResponse"]["data"], + "Bearer fresh-2" + ); + server.abort(); +} diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index f5851b4f..16b9355b 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -161,9 +161,7 @@ Runner startup uses `backendAPI.collections.captureExecutionSnapshot(targets)` native blocking pool and loads each collection once. `snapshot.rs` projects selected endpoints, persisted data, scripts and closest-folder-first auth sources alongside one compact collection metadata object and its variable entries. It -never sends the collection tree or sibling OpenAPI document per request. Legacy -collections retain their metadata and sidecar precedence without conversion; -legacy global scripts are a read-only fallback. +never sends the collection tree or sibling OpenAPI document per request. Repositories hydrate keychain values from the supplied data. Repeated auth secret scopes share an in-flight read only for this capture; collection variables bypass @@ -193,10 +191,7 @@ in-memory runner (`storage/runnerDoc.js`), re-attaching `collectionId` and `endpointId` so execution capture is unchanged. Desktop-local state stays in the store: `runnerLastRuns` (last-run timestamps), `runnerRunHistory` and the `lastRunnerId` setting. `runner.feature.js` wires the shared repositories and -`RunnerBookkeeping`, which runs the resumable migration from the legacy -`collectionRunners` store key on every collections load (markers in -`runnersMigratedCollections`, backup in `collectionRunnersLegacyBackup`) and -clears local state when a collection is deleted. The runner tab binds to the +`RunnerBookkeeping`, which clears local state when a collection is deleted. The runner tab binds to the collection of its first queued request; the palette then offers only that collection and the saved-runner dropdown groups runners by collection. diff --git a/package-lock.json b/package-lock.json index a9daadd3..48912939 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "resonance", - "version": "3.6.0", + "version": "3.6.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "resonance", - "version": "3.6.0", + "version": "3.6.1", "license": "MIT", "dependencies": { "@codemirror/autocomplete": "^6.20.4", @@ -20,7 +20,7 @@ "@codemirror/state": "^6.5.2", "@codemirror/view": "^6.38.6", "@lezer/highlight": "^1.2.3", - "cm6-graphql": "^0.2.1", + "cm6-graphql": "^1.0.0", "graphql": "^17.0.2" }, "devDependencies": { @@ -5241,9 +5241,9 @@ } }, "node_modules/ci-info": { - "version": "4.4.0", - "resolved": "https://registry.npmjs.org/ci-info/-/ci-info-4.4.0.tgz", - "integrity": "sha512-77PSwercCZU2Fc4sX94eF8k8Pxte6JAwL4/ICZLFjJLqegs7kCuAsqqj/70NQF6TvDpgFjkubQB2FW2ZZddvQg==", + "version": "4.5.0", + "resolved": "https://registry.npmjs.org/ci-info/-/ci-info-4.5.0.tgz", + "integrity": "sha512-d1uM8k5UZEyPza1n/MG3ko8nKupKN0KJpFsipgPcgaCCvV+IqIfJ+NzTjetlLM+UcXZx3+cLrNr76+eAO7G1Mw==", "dev": true, "funding": [ { @@ -5279,12 +5279,12 @@ } }, "node_modules/cm6-graphql": { - "version": "0.2.2", - "resolved": "https://registry.npmjs.org/cm6-graphql/-/cm6-graphql-0.2.2.tgz", - "integrity": "sha512-z9DQonlhuCQZotqSxKUMdZpZKZO8xaxx9BQNf2EBr0auunWaS9FJmUoeiBECOn24aZq2UZqcPIa9OwYZolk8Tg==", + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/cm6-graphql/-/cm6-graphql-1.0.0.tgz", + "integrity": "sha512-nOiWqjXN2J9qv32NjCOd8RN5kjIOSySjU6Y02xTGqAPboDyn86hfaAwvTwY2w3p9OA6klQW2jhc8M7+/Pxblvg==", "license": "MIT", "dependencies": { - "graphql-language-service": "^5.7.0" + "graphql-language-service": "^6.0.0" }, "peerDependencies": { "@codemirror/autocomplete": "^6.0.0", @@ -5293,7 +5293,7 @@ "@codemirror/state": "^6.0.0", "@codemirror/view": "^6.0.0", "@lezer/highlight": "^1.0.0", - "graphql": "^15.5.0 || ^16.0.0 || ^17.0.0" + "graphql": "^16.11.0 || ^17.0.0" } }, "node_modules/co": { @@ -5498,9 +5498,9 @@ "license": "MIT" }, "node_modules/electron-to-chromium": { - "version": "1.5.478", - "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.5.478.tgz", - "integrity": "sha512-pftG+ma5Lm8O6/p45bhYT6StBN5ziQymaRzmCZwFl8DwWHKPPD93FdOmP1aqftcepolQYvrcyTLa17kE8Yf+jg==", + "version": "1.5.479", + "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.5.479.tgz", + "integrity": "sha512-uAJdJQ7vHa4TvPfzt1TOqoRgHmav5gwmqHMPLxBH6m8Cbj5R9/z/rygTOQiZ8pe5g0ZAEogga1bP6HoKcOzx/Q==", "dev": true, "license": "ISC" }, @@ -6135,20 +6135,16 @@ } }, "node_modules/graphql-language-service": { - "version": "5.7.0", - "resolved": "https://registry.npmjs.org/graphql-language-service/-/graphql-language-service-5.7.0.tgz", - "integrity": "sha512-5mwDb9HyHxMcO9RCLLieg6rC1u9XKJKWhFuvaZFJi3broQZDz1fcFNlm96UCMrIgI6MSsb9RwwqKKp2sZOZ9Gw==", + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/graphql-language-service/-/graphql-language-service-6.0.1.tgz", + "integrity": "sha512-f+/kcxErsGlv/wbmm1PbaNWEL6nTNyUMpkZffQpiSWisuD07AUGQKE/6t3GtYuPhB/mbbWTuaJXkyE0zv+qHnQ==", "license": "MIT", "dependencies": { "debounce-promise": "^3.1.2", - "nullthrows": "^1.0.0", - "vscode-languageserver-types": "^3.17.1" - }, - "bin": { - "graphql": "dist/temp-bin.js" + "vscode-languageserver-types": "^3.18.5" }, "peerDependencies": { - "graphql": "^15.5.0 || ^16.0.0 || ^17.0.0" + "graphql": "^16.11.0 || ^17.0.0" } }, "node_modules/has-flag": { @@ -8294,12 +8290,6 @@ "node": ">=8" } }, - "node_modules/nullthrows": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/nullthrows/-/nullthrows-1.1.1.tgz", - "integrity": "sha512-2vPPEi+Z7WqML2jZYddDIfy5Dqb0r2fze2zTxNNknZaFpVHU3mFB3R+DWeJWGVx0ecvttSGlJTI+WG+8Z4cDWw==", - "license": "MIT" - }, "node_modules/nwsapi": { "version": "2.2.28", "resolved": "https://registry.npmjs.org/nwsapi/-/nwsapi-2.2.28.tgz", diff --git a/package.json b/package.json index 58ef6b2c..b23ad3e6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "resonance", - "version": "3.6.0", + "version": "3.6.1", "description": "Resonance - A local-first, zero-account API client with excellent user experience", "license": "MIT", "engines": { @@ -50,7 +50,7 @@ "@codemirror/state": "^6.5.2", "@codemirror/view": "^6.38.6", "@lezer/highlight": "^1.2.3", - "cm6-graphql": "^0.2.1", + "cm6-graphql": "^1.0.0", "graphql": "^17.0.2" } } diff --git a/packaging/flatpak/io.github.db_mobile.resonance.metainfo.xml b/packaging/flatpak/io.github.db_mobile.resonance.metainfo.xml index 3615815f..6e0b83e8 100644 --- a/packaging/flatpak/io.github.db_mobile.resonance.metainfo.xml +++ b/packaging/flatpak/io.github.db_mobile.resonance.metainfo.xml @@ -117,6 +117,20 @@ + + +

Feature stabilization release

+
    +
  • Renaming a folder no longer duplicates its requests and subfolders
  • +
  • Store authentication secrets of open tabs in the system keyring instead of plain text
  • +
  • Saving an inactive tab from the tab bar saves that tab instead of the active one
  • +
  • Remove a collection's variables file when all its variables are cleared
  • +
  • Disconnect from MQTT brokers cleanly so the Last Will message is not published
  • +
  • Fix gRPC message skeletons for recursive message types, cURL imports with -u credentials and script test results when a script throws
  • +
  • Data from before Resonance 3.3.1 is no longer converted; install 3.6.0 once first to upgrade it
  • +
+
+

New features introduced

diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index 5b1ecb15..9ff79d77 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -1,6 +1,6 @@ name: db-mobile-resonance base: core22 -version: '3.6.0' +version: '3.6.1' title: Resonance summary: Test and explore web services description: | diff --git a/src-tauri/src/commands/collections/execution_snapshot.rs b/src-tauri/src/commands/collections/execution_snapshot.rs index f6fd21a5..0f63d882 100644 --- a/src-tauri/src/commands/collections/execution_snapshot.rs +++ b/src-tauri/src/commands/collections/execution_snapshot.rs @@ -8,21 +8,12 @@ pub async fn collections_capture_execution_snapshot( targets: Vec, ) -> Result, String> { tauri::async_runtime::spawn_blocking(move || { - let scripts = super::super::scripts::read_legacy_store_scripts(&app); snapshot::capture_collections(&targets, |id, endpoints| { let capture = || -> Result { let paths = CollectionPaths::resolve(&app, id)?; - match Layout::detect(&paths.dir) { - Some(Layout::V2) => { - let loaded = read_collection_dir_cached(collection_cache(), &paths.dir)?; - Ok(snapshot::from_loaded(id, &loaded, endpoints, &scripts)) - } - Some(Layout::V1) => snapshot::read_v1(id, &paths.dir, endpoints, &scripts), - None => Err(format!("Collection {id} not found")), - } + let loaded = read_collection_dir_cached(collection_cache(), &paths.dir)?; + Ok(snapshot::from_loaded(id, &loaded, endpoints)) }; - // Match the repositories' existing read failures: a missing or - // unreadable collection becomes a per-visit missingCollection error. capture().unwrap_or_else(|_| CollectionSnapshot::missing(id, endpoints)) }) }) diff --git a/src-tauri/src/commands/collections/mod.rs b/src-tauri/src/commands/collections/mod.rs index 42535c9e..3aee549c 100644 --- a/src-tauri/src/commands/collections/mod.rs +++ b/src-tauri/src/commands/collections/mod.rs @@ -10,22 +10,17 @@ use tauri_plugin_dialog::{DialogExt, FilePath}; use tauri_plugin_store::StoreExt; use tokio::sync::oneshot; -use legacy::{list_collection_endpoints, read_json_file}; -use resonance_collections::fs_secure::{restrict_dir, restrict_file}; +use resonance_collections::fs_secure::restrict_dir; pub use resonance_collections::ipc::{Collection, EndpointData}; use resonance_collections::model::ExampleDoc; -use resonance_collections::{ - cache, git, ipc, layout, legacy, link, model, read, runners, secrets, write, -}; +use resonance_collections::{cache, git, ipc, layout, link, model, read, runners, secrets, write}; use cache::{CollectionCache, read_collection_dir_cached}; use ipc::to_ipc_collection; -use read::Layout; +use read::is_collection_dir; -pub(crate) use layout::{desired_endpoint_file_name, find_endpoint_data_file}; use layout::{find_available_dir, slugify}; -pub(crate) use resonance_collections::startup::{absorb_store_scripts, convert_v1_dir}; use secrets::redact_auth_secrets; /// The process-wide parsed-collection cache. @@ -42,7 +37,7 @@ fn collection_cache() -> &'static CollectionCache { use super::store_files::MAIN_STORE as STORE_FILE; pub(crate) const COLLECTIONS_DIR: &str = "collections"; -const COLLECTION_INDEX_KEY: &str = "collectionIndex"; +pub(crate) const COLLECTION_INDEX_KEY: &str = "collectionIndex"; const LAST_COLLECTION_DIR_KEY: &str = "lastCollectionDirectory"; fn get_default_collections_dir(app: &AppHandle) -> Result { @@ -62,8 +57,7 @@ pub(crate) fn ensure_default_collections_dir(app: &AppHandle) -> Result Self { - Self { dir } - } - - fn of(dir: &Path) -> Self { - Self::from_dir(dir.to_path_buf()) - } - - pub(crate) fn collection_json(&self) -> PathBuf { - self.dir.join("collection.json") - } - - pub(crate) fn requests(&self) -> PathBuf { - self.dir.join("requests") - } - - pub(crate) fn variables_json(&self) -> PathBuf { - self.dir.join("variables.json") - } - - /// Create the requests directory if it is missing, and restrict it either - /// way. Restricting unconditionally is deliberate: a directory that already - /// exists may predate the hardening, or have been created by an older - /// version under a looser umask. - pub(crate) fn ensure_requests(&self) -> Result { - let requests = self.requests(); - if !requests.exists() { - fs::create_dir_all(&requests) - .map_err(|e| format!("Failed to create requests dir: {}", e))?; - } - restrict_dir(&requests); - Ok(requests) - } } fn get_collection_index(app: &AppHandle) -> Result, String> { @@ -182,36 +142,14 @@ fn save_last_collection_directory(app: &AppHandle, dir: &Path) { } } -pub(crate) fn write_json_file(path: &PathBuf, data: &T) -> Result<(), String> { - let json = serde_json::to_string_pretty(data) - .map_err(|e| format!("Failed to serialize JSON: {}", e))?; - fs::write(path, json).map_err(|e| format!("Failed to write file: {}", e))?; - restrict_file(path); - Ok(()) -} - -fn is_collection_dir(path: &Path) -> bool { - Layout::detect(path).is_some() -} - -/// Reads a collection directory in whichever layout it uses. -/// -/// A v2 tree is projected back into the shape the frontend expects, so reading -/// the new format is invisible to the renderer. +/// Reads a collection directory, projected back into the shape the frontend +/// expects. fn read_collection_from_dir(path: &Path) -> Result { - let mut collection = match Layout::detect(path) { - Some(Layout::V2) => { - let loaded = read_collection_dir_cached(collection_cache(), path)?; - to_ipc_collection(&loaded, &path.to_string_lossy(), false) - } - Some(Layout::V1) => { - let mut collection: Collection = - read_json_file(&CollectionPaths::of(path).collection_json())?; - collection.storage_path = Some(path.to_string_lossy().to_string()); - collection - } - None => return Err(format!("{} is not a collection directory", path.display())), - }; + if !is_collection_dir(path) { + return Err(format!("{} is not a collection directory", path.display())); + } + let loaded = read_collection_dir_cached(collection_cache(), path)?; + let mut collection = to_ipc_collection(&loaded, &path.to_string_lossy(), false); // Every read of a collection off disk funnels through here, and the branch // is as much a fn(&Path) as the rest of the read, so one call covers list, @@ -220,12 +158,6 @@ fn read_collection_from_dir(path: &Path) -> Result { Ok(collection) } -fn find_endpoint_name_in_collection(collection: &Collection, endpoint_id: &str) -> Option { - list_collection_endpoints(collection) - .into_iter() - .find_map(|(id, name)| if id == endpoint_id { Some(name) } else { None }) -} - pub(crate) fn resolve_collection_dir( app: &AppHandle, collection_id: &str, @@ -438,23 +370,18 @@ fn folder_request_ids(folder: &read::FolderNode, out: &mut Vec) { } } -/// Loads whatever is already stored for a collection, in either layout. -/// -/// A v1 directory is converted on the way in, so a save always merges onto the -/// v2 model regardless of what is on disk. +/// Loads whatever is already stored for a collection. /// /// @param dir - The collection directory /// @returns The stored collection, or None for a directory with nothing in it fn load_existing(dir: &Path) -> Result, String> { - match Layout::detect(dir) { - Some(Layout::V2) => Ok(Some(read_collection_dir_cached(collection_cache(), dir)?)), - Some(Layout::V1) => Ok(Some(legacy::load_v1_dir(dir)?)), - None => Ok(None), + if !is_collection_dir(dir) { + return Ok(None); } + Ok(Some(read_collection_dir_cached(collection_cache(), dir)?)) } -/// Writes a collection as a v2 tree, converting it first when the directory -/// still holds v1 files. +/// Writes a collection as a v2 tree. /// /// The per-request state already on disk is carried over: `collection_save` /// only ever carries structure, so a folder rename must not blank the bodies @@ -468,8 +395,7 @@ fn write_v2_collection(dir: &Path, incoming: &Collection) -> Result<(), String> /// not carried by the IPC collection. /// /// Import needs this: it produces a whole collection plus its variables and -/// per-endpoint payloads in one go, and writing those as separate v1 files -/// beside a v2 tree would leave artifacts the next save deletes. +/// per-endpoint payloads in one go. /// /// @param dir - The collection directory /// @param incoming - The collection structure @@ -749,43 +675,18 @@ pub(crate) fn load_for_export( let collection = read_collection_from_dir(&dir)?; - match Layout::detect(&dir) { - Some(Layout::V2) => { - let loaded = read_collection_dir_cached(collection_cache(), &dir)?; - let data = loaded - .requests() - .into_iter() - .map(|entry| { - ( - entry.doc.id.clone(), - ipc::request_to_endpoint_data(&entry.doc), - ) - }) - .collect(); - Ok((collection, loaded.variables, data)) - } - _ => { - let paths = CollectionPaths::of(&dir); - - let variables = if paths.variables_json().exists() { - read_json_file(&paths.variables_json()).unwrap_or_default() - } else { - Vec::new() - }; - - let requests_dir = paths.requests(); - let mut data = HashMap::new(); - for (endpoint_id, _) in list_collection_endpoints(&collection) { - if let Some(file) = find_endpoint_data_file(&requests_dir, &endpoint_id)? - && let Ok(endpoint_data) = read_json_file::(&file) - { - data.insert(endpoint_id, endpoint_data); - } - } - - Ok((collection, variables, data)) - } - } + let loaded = read_collection_dir_cached(collection_cache(), &dir)?; + let data = loaded + .requests() + .into_iter() + .map(|entry| { + ( + entry.doc.id.clone(), + ipc::request_to_endpoint_data(&entry.doc), + ) + }) + .collect(); + Ok((collection, loaded.variables, data)) } /// One collection that could not be opened, and why. @@ -1295,7 +1196,6 @@ pub async fn collection_delete(app: AppHandle, collection_id: String) -> Result< } unregister_collection_path(&app, &collection_id)?; - super::scripts::purge_store_scripts_for_collection(&app, &collection_id); Ok(()) } @@ -1307,20 +1207,10 @@ pub async fn collection_get_endpoint_data( ) -> Result { let paths = CollectionPaths::resolve(&app, &collection_id)?; - if Layout::detect(&paths.dir) == Some(Layout::V2) { - let loaded = read_collection_dir_cached(collection_cache(), &paths.dir)?; - return Ok(ipc::find_request(&loaded, &endpoint_id) - .map(ipc::request_to_endpoint_data) - .unwrap_or_default()); - } - - let requests_dir = paths.requests(); - - let Some(endpoint_file) = find_endpoint_data_file(&requests_dir, &endpoint_id)? else { - return Ok(EndpointData::default()); - }; - - read_json_file(&endpoint_file) + let loaded = read_collection_dir_cached(collection_cache(), &paths.dir)?; + Ok(ipc::find_request(&loaded, &endpoint_id) + .map(ipc::request_to_endpoint_data) + .unwrap_or_default()) } #[tauri::command] @@ -1336,31 +1226,9 @@ pub async fn collection_save_endpoint_data( } let paths = CollectionPaths::resolve(&app, &collection_id)?; - if Layout::detect(&paths.dir) == Some(Layout::V2) { - let mut loaded = read_collection_dir_cached(collection_cache(), &paths.dir)?; - save_endpoint_data_v2(&paths.dir, &mut loaded, &endpoint_id, &data)?; - collection_cache().refresh(&paths.dir, &loaded); - return Ok(()); - } - - let collection = read_collection_from_dir(&paths.dir)?; - let requests_dir = paths.ensure_requests()?; - - let endpoint_name = find_endpoint_name_in_collection(&collection, &endpoint_id) - .unwrap_or_else(|| endpoint_id.clone()); - let desired_file = requests_dir.join(desired_endpoint_file_name(&endpoint_name, &endpoint_id)); - - if let Some(current_file) = find_endpoint_data_file(&requests_dir, &endpoint_id)? { - if current_file != desired_file && !desired_file.exists() { - fs::rename(¤t_file, &desired_file) - .map_err(|e| format!("Failed to rename endpoint data file: {}", e))?; - } else if current_file != desired_file && desired_file.exists() { - fs::remove_file(¤t_file) - .map_err(|e| format!("Failed to remove old endpoint data file: {}", e))?; - } - } - - write_json_file(&desired_file, &data)?; + let mut loaded = read_collection_dir_cached(collection_cache(), &paths.dir)?; + save_endpoint_data_v2(&paths.dir, &mut loaded, &endpoint_id, &data)?; + collection_cache().refresh(&paths.dir, &loaded); Ok(()) } @@ -1388,9 +1256,6 @@ pub async fn collection_save_examples( ) -> Result<(), String> { validate_examples(&examples)?; let paths = CollectionPaths::resolve(&app, &collection_id)?; - if Layout::detect(&paths.dir) != Some(Layout::V2) { - return Err("Saving examples requires the current collection format".into()); - } let mut loaded = read_collection_dir_cached(collection_cache(), &paths.dir)?; save_examples_v2(&paths.dir, &mut loaded, &endpoint_id, examples)?; collection_cache().refresh(&paths.dir, &loaded); @@ -1405,32 +1270,20 @@ pub async fn collection_delete_endpoint_data( ) -> Result<(), String> { let paths = CollectionPaths::resolve(&app, &collection_id)?; - if Layout::detect(&paths.dir) == Some(Layout::V2) { - let mut loaded = read_collection_dir_cached(collection_cache(), &paths.dir)?; - let request_file = loaded - .requests() - .into_iter() - .find(|entry| entry.doc.id == endpoint_id) - .and_then(|entry| entry.source.clone()); + let mut loaded = read_collection_dir_cached(collection_cache(), &paths.dir)?; + let request_file = loaded + .requests() + .into_iter() + .find(|entry| entry.doc.id == endpoint_id) + .and_then(|entry| entry.source.clone()); - if remove_request_from_tree(&mut loaded.root, &endpoint_id) { - write::write_collection_dir(&paths.dir, &mut loaded)?; - if let Some(file) = request_file { - let _ = fs::remove_file(file); - } - // After the removal, so the fingerprint matches what is now on disk. - collection_cache().refresh(&paths.dir, &loaded); + if remove_request_from_tree(&mut loaded.root, &endpoint_id) { + write::write_collection_dir(&paths.dir, &mut loaded)?; + if let Some(file) = request_file { + let _ = fs::remove_file(file); } - return Ok(()); - } - - let requests_dir = paths.requests(); - - if let Some(endpoint_file) = find_endpoint_data_file(&requests_dir, &endpoint_id)? { - fs::remove_file(&endpoint_file) - .map_err(|e| format!("Failed to delete endpoint data: {}", e))?; + collection_cache().refresh(&paths.dir, &loaded); } - Ok(()) } @@ -1444,13 +1297,6 @@ pub async fn collection_delete_folder( folder_id: String, ) -> Result, String> { let paths = CollectionPaths::resolve(&app, &collection_id)?; - if Layout::detect(&paths.dir) != Some(Layout::V2) { - return Err( - "This collection is still in the legacy format. Make any change to it first so it is converted, then delete the folder again." - .to_string(), - ); - } - let mut loaded = read_collection_dir_cached(collection_cache(), &paths.dir)?; let folder = take_folder_from_tree(&mut loaded.root, &folder_id) .ok_or_else(|| format!("Folder {} not found in collection", folder_id))?; @@ -1471,17 +1317,7 @@ pub async fn collection_get_variables( ) -> Result, String> { let paths = CollectionPaths::resolve(&app, &collection_id)?; - if Layout::detect(&paths.dir) == Some(Layout::V2) { - return Ok(read_collection_dir_cached(collection_cache(), &paths.dir)?.variables); - } - - let variables_file = paths.variables_json(); - - if !variables_file.exists() { - return Ok(vec![]); - } - - read_json_file(&variables_file) + Ok(read_collection_dir_cached(collection_cache(), &paths.dir)?.variables) } #[tauri::command] @@ -1502,29 +1338,32 @@ pub async fn collection_save_variables( } } - if Layout::detect(&paths.dir) == Some(Layout::V2) { - let mut loaded = read_collection_dir_cached(collection_cache(), &paths.dir)?; - loaded.variables = variables; - write::write_collection_dir(&paths.dir, &mut loaded)?; - collection_cache().refresh(&paths.dir, &loaded); - return Ok(()); - } - - write_json_file(&paths.variables_json(), &variables)?; + save_variables_in(&paths.dir, variables) +} +/// Replaces a collection's variables on disk. The directory writer skips an +/// empty list, so clearing the last variable removes the file here; otherwise +/// the old values would come back on the next uncached read. +fn save_variables_in(dir: &Path, variables: Vec) -> Result<(), String> { + let mut loaded = read_collection_dir_cached(collection_cache(), dir)?; + loaded.variables = variables; + write::write_collection_dir(dir, &mut loaded)?; + if loaded.variables.is_empty() { + let file = dir.join(layout::VARIABLES_YAML); + if file.exists() { + fs::remove_file(&file) + .map_err(|e| format!("Failed to remove {}: {}", file.display(), e))?; + } + } + collection_cache().refresh(dir, &loaded); Ok(()) } -const LEGACY_RUNNER_LAYOUT: &str = "This collection is still in the legacy format. Make any change to it first so it is converted, then save the runner again."; - /// Lists a collection's runners, persisting any id the reader had to re-mint. /// /// Without the write-back a merge-duplicated id would get a different fresh id /// on every read, and nothing stored against it could ever resolve. fn list_runners_at(dir: &Path) -> Result, String> { - if Layout::detect(dir) != Some(Layout::V2) { - return Ok(Vec::new()); - } let mut loaded = read_collection_dir_cached(collection_cache(), dir)?; if !loaded.reassigned_runner_ids.is_empty() { let fresh: HashSet = loaded @@ -1554,9 +1393,6 @@ fn save_runner_at( collection_id: &str, runner: model::RunnerDoc, ) -> Result { - if Layout::detect(dir) != Some(Layout::V2) { - return Err(LEGACY_RUNNER_LAYOUT.to_string()); - } let mut loaded = read_collection_dir_cached(collection_cache(), dir)?; let stored = runners::upsert_runner(dir, &mut loaded.runners, runner, collection_id)?; collection_cache().refresh(dir, &loaded); @@ -1564,9 +1400,6 @@ fn save_runner_at( } fn delete_runner_at(dir: &Path, runner_id: &str) -> Result { - if Layout::detect(dir) != Some(Layout::V2) { - return Err(LEGACY_RUNNER_LAYOUT.to_string()); - } let mut loaded = read_collection_dir_cached(collection_cache(), dir)?; let removed = runners::delete_runner(dir, &mut loaded.runners, runner_id)?; collection_cache().refresh(dir, &loaded); @@ -1692,7 +1525,7 @@ pub async fn collections_git_status( if status.changes.is_empty() { continue; } - let loaded = (Layout::detect(dir) == Some(Layout::V2)) + let loaded = is_collection_dir(dir) .then(|| read_collection_dir_cached(collection_cache(), dir).ok()) .flatten(); let entry = match &loaded { @@ -1750,47 +1583,50 @@ pub async fn collections_pick_directory( } #[cfg(test)] -mod convert_on_save { +mod saves_on_disk { use super::read::read_collection_dir; use super::*; use serde_json::json; use tempfile::TempDir; - fn v1_on_disk(dir: &Path) { - let collection = json!({ - "id": "c1", - "name": "Petstore", - "baseUrl": "https://api.example.com", - "endpoints": [ - {"id": "custom_1", "name": "Health", "method": "GET", "path": "/health"}, - {"id": "custom_2", "name": "Create Pet", "method": "POST", "path": "/pets"} + fn seeded_on_disk(dir: &Path) { + let incoming = Collection { + id: "c1".into(), + name: "Petstore".into(), + base_url: "https://api.example.com".into(), + endpoints: vec![ + json!({"id": "custom_1", "name": "Health", "method": "GET", "path": "/health"}), + json!({"id": "custom_2", "name": "Create Pet", "method": "POST", "path": "/pets"}), ], - "folders": [{ + folders: vec![json!({ "id": "folder_pets", "name": "pets", - "endpoints": [ - {"id": "custom_2", "name": "Create Pet", "method": "POST", "path": "/pets"} - ] - }], - "defaultHeaders": {"Accept": "application/json"} - }); - - fs::write( - dir.join("collection.json"), - serde_json::to_string_pretty(&collection).unwrap(), - ) - .unwrap(); - - fs::write( - dir.join("variables.json"), - r#"[{"key":"baseUrl","value":"x"}]"#, - ) - .unwrap(); - - fs::create_dir(dir.join("requests")).unwrap(); - fs::write( - dir.join("requests/create-pet--custom_2.json"), - r#"{"modifiedBody":"{\"name\":\"Rex\"}","headers":[{"key":"Accept","value":"*/*"}],"pathParams":[],"queryParams":[]}"#, + "endpoints": [{"id": "custom_2", "name": "Create Pet", "method": "POST", "path": "/pets"}] + })], + default_headers: json!({"Accept": "application/json"}), + auth_config: None, + open_api_spec: None, + storage_path: None, + storage_parent_path: None, + linked: false, + can_close: false, + git_branch: None, + }; + let mut seed = HashMap::new(); + seed.insert( + "custom_2".to_string(), + EndpointData { + modified_body: Some("{\"name\":\"Rex\"}".into()), + headers: vec![json!({"key": "Accept", "value": "*/*"})], + ..Default::default() + }, + ); + write_v2_collection_seeded( + dir, + &incoming, + Some(vec![json!({"key": "baseUrl", "value": "x"})]), + &seed, + None, ) .unwrap(); } @@ -1799,119 +1635,13 @@ mod convert_on_save { read_collection_from_dir(dir).unwrap() } - #[test] - fn a_v1_directory_is_still_read_as_v1_before_any_save() { - let temp = TempDir::new().unwrap(); - v1_on_disk(temp.path()); - - assert_eq!(Layout::detect(temp.path()), Some(Layout::V1)); - assert_eq!(ipc_from_disk(temp.path()).endpoints.len(), 2); - } - - #[test] - fn saving_converts_the_directory_to_v2() { - let temp = TempDir::new().unwrap(); - v1_on_disk(temp.path()); - - let incoming = ipc_from_disk(temp.path()); - write_v2_collection(temp.path(), &incoming).unwrap(); - - assert_eq!(Layout::detect(temp.path()), Some(Layout::V2)); - assert!(temp.path().join("collection.yaml").exists()); - assert!(temp.path().join("health.yaml").exists()); - assert!(temp.path().join("pets/create-pet.yaml").exists()); - } - - #[test] - fn the_v1_files_are_removed_once_the_v2_tree_is_complete() { - let temp = TempDir::new().unwrap(); - v1_on_disk(temp.path()); - - let incoming = ipc_from_disk(temp.path()); - write_v2_collection(temp.path(), &incoming).unwrap(); - - assert!(!temp.path().join("collection.json").exists()); - assert!(!temp.path().join("variables.json").exists()); - assert!(!temp.path().join("requests").exists()); - } - - #[test] - fn conversion_carries_the_per_request_state_across() { - let temp = TempDir::new().unwrap(); - v1_on_disk(temp.path()); - - let incoming = ipc_from_disk(temp.path()); - write_v2_collection(temp.path(), &incoming).unwrap(); - - let loaded = read_collection_dir(temp.path()).unwrap(); - let created = ipc::find_request(&loaded, "custom_2").unwrap(); - let data = ipc::request_to_endpoint_data(created); - - assert_eq!(data.modified_body.as_deref(), Some("{\"name\":\"Rex\"}")); - assert_eq!(data.headers.len(), 1); - } - - #[test] - fn conversion_carries_the_variables_across() { - let temp = TempDir::new().unwrap(); - v1_on_disk(temp.path()); - - let incoming = ipc_from_disk(temp.path()); - write_v2_collection(temp.path(), &incoming).unwrap(); - - let loaded = read_collection_dir(temp.path()).unwrap(); - assert_eq!(loaded.variables.len(), 1); - assert_eq!(loaded.variables[0]["key"], "baseUrl"); - } - - #[test] - fn conversion_preserves_endpoint_ids() { - let temp = TempDir::new().unwrap(); - v1_on_disk(temp.path()); - - let incoming = ipc_from_disk(temp.path()); - write_v2_collection(temp.path(), &incoming).unwrap(); - - let loaded = read_collection_dir(temp.path()).unwrap(); - let ids: HashSet<_> = loaded - .requests() - .iter() - .map(|entry| entry.doc.id.clone()) - .collect(); - - assert!(ids.contains("custom_1")); - assert!(ids.contains("custom_2")); - } - - #[test] - fn the_collection_still_reads_the_same_over_ipc_after_converting() { - let temp = TempDir::new().unwrap(); - v1_on_disk(temp.path()); - - let before = ipc_from_disk(temp.path()); - write_v2_collection(temp.path(), &before).unwrap(); - let after = ipc_from_disk(temp.path()); - - assert_eq!(before.id, after.id); - assert_eq!(before.name, after.name); - assert_eq!(before.endpoints.len(), after.endpoints.len()); - assert_eq!(before.folders.len(), after.folders.len()); - assert_eq!( - before.folders[0]["endpoints"].as_array().unwrap().len(), - after.folders[0]["endpoints"].as_array().unwrap().len() - ); - } - /// A name already taken by a directory must not fail the write: the /// writer suffixes past it rather than giving up or deleting anything. #[test] fn an_obstructed_file_name_is_routed_around() { let temp = TempDir::new().unwrap(); - v1_on_disk(temp.path()); fs::create_dir(temp.path().join("health.yaml")).unwrap(); - - let incoming = ipc_from_disk(temp.path()); - write_v2_collection(temp.path(), &incoming).unwrap(); + seeded_on_disk(temp.path()); assert!(temp.path().join("health-2.yaml").is_file()); assert!(temp.path().join("health.yaml").is_dir()); @@ -1921,75 +1651,43 @@ mod convert_on_save { ); } - /// The guarantee behind convert-on-save: until `collection.yaml` exists, - /// the directory still reads as a complete v1 collection. Stray v2 files - /// from an interrupted write are inert, so a crash mid-conversion costs - /// nothing. - /// - /// (A simulated write failure is not used here: the writer suffixes past - /// an obstructed name, and `restrict_dir` resets a directory's mode on - /// every write, so neither is a reachable failure.) - #[test] - fn a_partial_conversion_still_reads_as_a_complete_v1_collection() { - let temp = TempDir::new().unwrap(); - v1_on_disk(temp.path()); - - // Some v2 files landed, but the marker never did. - fs::write( - temp.path().join("health.yaml"), - "resonanceFormat: 2\nid: custom_1\nname: Health\n", - ) - .unwrap(); - fs::create_dir(temp.path().join("pets")).unwrap(); - fs::write( - temp.path().join("pets/create-pet.yaml"), - "resonanceFormat: 2\nid: custom_2\nname: Create Pet\n", - ) - .unwrap(); - - assert_eq!(Layout::detect(temp.path()), Some(Layout::V1)); - - let collection = ipc_from_disk(temp.path()); - assert_eq!(collection.endpoints.len(), 2); - assert_eq!(collection.folders.len(), 1); - - // And the v1 per-request state is still the source of truth. - let requests_dir = CollectionPaths::of(temp.path()).requests(); - let file = find_endpoint_data_file(&requests_dir, "custom_2") - .unwrap() - .unwrap(); - let data: EndpointData = read_json_file(&file).unwrap(); - assert_eq!(data.modified_body.as_deref(), Some("{\"name\":\"Rex\"}")); - } - - /// The leftovers of an interrupted conversion are cleared by the next - /// successful save, rather than lingering in both formats forever. + /// Import writes the tree, its variables and the per-request payloads in + /// one pass, so the imported scripts and GraphQL bodies land in the files. #[test] - fn an_interrupted_conversion_heals_on_the_next_save() { + fn clearing_all_variables_removes_the_file() { let temp = TempDir::new().unwrap(); - v1_on_disk(temp.path()); - - // Simulate a crash after some v2 files landed but before the marker. - fs::write( - temp.path().join("health.yaml"), - "resonanceFormat: 2\nid: custom_1\nname: Health\n", - ) - .unwrap(); - assert_eq!(Layout::detect(temp.path()), Some(Layout::V1)); + let incoming = Collection { + id: "c1".into(), + name: "Vars".into(), + base_url: String::new(), + endpoints: vec![], + folders: vec![], + default_headers: Value::Null, + auth_config: None, + open_api_spec: None, + storage_path: None, + storage_parent_path: None, + linked: false, + can_close: false, + git_branch: None, + }; + write_v2_collection_seeded(temp.path(), &incoming, None, &HashMap::new(), None).unwrap(); - let incoming = ipc_from_disk(temp.path()); - write_v2_collection(temp.path(), &incoming).unwrap(); + save_variables_in(temp.path(), vec![json!({"key": "a", "value": "1"})]).unwrap(); + assert!(temp.path().join(layout::VARIABLES_YAML).exists()); - assert_eq!(Layout::detect(temp.path()), Some(Layout::V2)); - assert!(!temp.path().join("collection.json").exists()); - assert!(!temp.path().join("requests").exists()); + save_variables_in(temp.path(), vec![]).unwrap(); + assert!(!temp.path().join(layout::VARIABLES_YAML).exists()); + assert!( + read::read_collection_dir(temp.path()) + .unwrap() + .variables + .is_empty() + ); } - /// Import writes a v2 tree directly. Writing its variables and per-request - /// payloads as v1 files beside it would leave artifacts the next save - /// deletes, taking the imported scripts and GraphQL bodies with them. #[test] - fn a_seeded_write_leaves_no_v1_artifacts() { + fn a_seeded_write_stores_variables_and_request_state() { let temp = TempDir::new().unwrap(); let incoming = Collection { @@ -2029,8 +1727,6 @@ mod convert_on_save { ) .unwrap(); - assert!(!temp.path().join("variables.json").exists()); - assert!(!temp.path().join("requests").exists()); assert!(temp.path().join("variables.yaml").exists()); let loaded = read_collection_dir(temp.path()).unwrap(); @@ -2043,35 +1739,10 @@ mod convert_on_save { assert_eq!(data.graphql_data.unwrap()["query"], "{ me }"); } - /// Export must read the same content whichever layout the collection uses. - #[test] - fn export_sees_the_same_content_before_and_after_conversion() { - let temp = TempDir::new().unwrap(); - v1_on_disk(temp.path()); - - let requests_dir = CollectionPaths::of(temp.path()).requests(); - let before_file = find_endpoint_data_file(&requests_dir, "custom_2") - .unwrap() - .unwrap(); - let before: EndpointData = read_json_file(&before_file).unwrap(); - - let incoming = ipc_from_disk(temp.path()); - write_v2_collection(temp.path(), &incoming).unwrap(); - - let loaded = read_collection_dir(temp.path()).unwrap(); - let after = ipc::request_to_endpoint_data(ipc::find_request(&loaded, "custom_2").unwrap()); - - assert_eq!(before.modified_body, after.modified_body); - assert_eq!(before.headers, after.headers); - } - #[test] fn a_second_save_is_a_no_op_on_disk() { let temp = TempDir::new().unwrap(); - v1_on_disk(temp.path()); - - let incoming = ipc_from_disk(temp.path()); - write_v2_collection(temp.path(), &incoming).unwrap(); + seeded_on_disk(temp.path()); let first = fs::read_to_string(temp.path().join("health.yaml")).unwrap(); let reread = ipc_from_disk(temp.path()); @@ -2082,12 +1753,9 @@ mod convert_on_save { } #[test] - fn renaming_a_request_after_conversion_moves_only_that_file() { + fn renaming_a_request_moves_only_that_file() { let temp = TempDir::new().unwrap(); - v1_on_disk(temp.path()); - - let incoming = ipc_from_disk(temp.path()); - write_v2_collection(temp.path(), &incoming).unwrap(); + seeded_on_disk(temp.path()); let created_before = fs::read_to_string(temp.path().join("pets/create-pet.yaml")).unwrap(); @@ -2110,8 +1778,7 @@ mod convert_on_save { #[test] fn saving_endpoint_data_rewrites_only_that_request() { let temp = TempDir::new().unwrap(); - v1_on_disk(temp.path()); - write_v2_collection(temp.path(), &ipc_from_disk(temp.path())).unwrap(); + seeded_on_disk(temp.path()); let mut loaded = read_collection_dir(temp.path()).unwrap(); let unrelated = temp.path().join("pets/create-pet.yaml"); @@ -2154,8 +1821,7 @@ mod convert_on_save { #[test] fn examples_round_trip_through_the_request_file() { let temp = TempDir::new().unwrap(); - v1_on_disk(temp.path()); - write_v2_collection(temp.path(), &ipc_from_disk(temp.path())).unwrap(); + seeded_on_disk(temp.path()); let mut loaded = read_collection_dir(temp.path()).unwrap(); save_examples_v2( @@ -2177,8 +1843,7 @@ mod convert_on_save { #[test] fn saving_examples_rewrites_only_that_request_file() { let temp = TempDir::new().unwrap(); - v1_on_disk(temp.path()); - write_v2_collection(temp.path(), &ipc_from_disk(temp.path())).unwrap(); + seeded_on_disk(temp.path()); let mut loaded = read_collection_dir(temp.path()).unwrap(); let unrelated = temp.path().join("pets/create-pet.yaml"); let marked = format!("{}# marker\n", fs::read_to_string(&unrelated).unwrap()); @@ -2270,8 +1935,7 @@ mod convert_on_save { #[test] fn saving_endpoint_data_for_an_unknown_request_is_an_error() { let temp = TempDir::new().unwrap(); - v1_on_disk(temp.path()); - write_v2_collection(temp.path(), &ipc_from_disk(temp.path())).unwrap(); + seeded_on_disk(temp.path()); let mut loaded = read_collection_dir(temp.path()).unwrap(); let result = save_endpoint_data_v2( @@ -2329,11 +1993,11 @@ mod load_errors { use super::*; use tempfile::TempDir; - fn v1_collection(dir: &Path, id: &str) { + fn collection_on_disk(dir: &Path, id: &str) { fs::create_dir_all(dir).unwrap(); fs::write( - dir.join("collection.json"), - format!(r#"{{"id":"{id}","name":"{id}","endpoints":[],"folders":[]}}"#), + dir.join("collection.yaml"), + format!("resonanceFormat: 2\nid: {id}\nname: {id}\n"), ) .unwrap(); } @@ -2342,7 +2006,7 @@ mod load_errors { fn an_import_in_a_custom_folder_can_be_closed_without_a_linked_record() { let root = TempDir::new().unwrap(); let managed = root.path().join("managed"); - v1_collection(&managed.join("default"), "default"); + collection_on_disk(&managed.join("default"), "default"); let imported = root.path().join("custom"); fs::create_dir(&imported).unwrap(); let metadata = imported.join("collection.yaml"); @@ -2380,12 +2044,12 @@ mod load_errors { #[test] fn unreadable_and_duplicate_collections_are_reported_without_hiding_others() { let root = TempDir::new().unwrap(); - v1_collection(&root.path().join("a"), "col-a"); - v1_collection(&root.path().join("a-copy"), "col-a"); - v1_collection(&root.path().join("b"), "col-b"); + collection_on_disk(&root.path().join("a"), "col-a"); + collection_on_disk(&root.path().join("a-copy"), "col-a"); + collection_on_disk(&root.path().join("b"), "col-b"); let broken = root.path().join("broken"); fs::create_dir_all(&broken).unwrap(); - fs::write(broken.join("collection.json"), "{ not json").unwrap(); + fs::write(broken.join("collection.yaml"), "{ not yaml").unwrap(); let mut index = HashMap::new(); let scan = scan_collections(root.path(), &mut index); @@ -2410,14 +2074,14 @@ mod load_errors { fs::create_dir_all(&managed).unwrap(); let broken_managed = managed.join("broken"); fs::create_dir_all(&broken_managed).unwrap(); - fs::write(broken_managed.join("collection.json"), "{ nope").unwrap(); + fs::write(broken_managed.join("collection.yaml"), "{ nope").unwrap(); let elsewhere = TempDir::new().unwrap(); let empty_linked = elsewhere.path().join("empty"); fs::create_dir_all(&empty_linked).unwrap(); let broken_linked = elsewhere.path().join("broken"); fs::create_dir_all(&broken_linked).unwrap(); - fs::write(broken_linked.join("collection.json"), "{ nope").unwrap(); + fs::write(broken_linked.join("collection.yaml"), "{ nope").unwrap(); let mut index = HashMap::from([ ( @@ -2470,8 +2134,8 @@ mod load_errors { #[test] fn a_moved_collection_is_found_from_its_parent_folder_by_id() { let root = TempDir::new().unwrap(); - v1_collection(&root.path().join("other"), "col-other"); - v1_collection(&root.path().join("moved"), "col-a"); + collection_on_disk(&root.path().join("other"), "col-other"); + collection_on_disk(&root.path().join("moved"), "col-a"); let (dir, collection) = find_collection_dir(root.path(), "col-a").unwrap(); assert!(dir.ends_with("moved")); @@ -2484,7 +2148,7 @@ mod load_errors { #[test] fn a_folder_holding_a_different_collection_is_rejected() { let root = TempDir::new().unwrap(); - v1_collection(&root.path().join("other"), "col-other"); + collection_on_disk(&root.path().join("other"), "col-other"); let error = find_collection_dir(&root.path().join("other"), "col-a").unwrap_err(); assert!(error.contains("col-other"), "{error}"); @@ -2655,26 +2319,6 @@ mod runner_commands { ); } - #[test] - fn saving_a_runner_into_a_legacy_collection_is_refused() { - let temp = TempDir::new().unwrap(); - fs::write( - temp.path().join("collection.json"), - "{\"id\":\"c1\",\"name\":\"P\"}", - ) - .unwrap(); - - let error = save_runner_at( - temp.path(), - "c1", - model::RunnerDoc::new(String::new(), "Smoke".into()), - ) - .unwrap_err(); - assert!(error.contains("legacy format"), "{}", error); - assert!(list_runners_at(temp.path()).unwrap().is_empty()); - assert!(!temp.path().join("runners").exists()); - } - #[test] fn a_saved_runner_is_listed_and_deleted_through_the_cache() { let temp = TempDir::new().unwrap(); diff --git a/src-tauri/src/commands/data_migrations.rs b/src-tauri/src/commands/data_migrations.rs index 9d8498b5..fd0bf28c 100644 --- a/src-tauri/src/commands/data_migrations.rs +++ b/src-tauri/src/commands/data_migrations.rs @@ -4,31 +4,37 @@ //! The main store carries a `dataVersion` stamp. Each step lifts the data to //! its `to` version and the stamp is written after every step, so a crash or //! a failed step resumes at that step on the next launch. Every step is -//! idempotent, which is what lets an install without a stamp (version 0) run -//! the whole chain safely. +//! idempotent. A fresh install has no main store yet and is stamped +//! [`CURRENT_DATA_VERSION`] outright, since there is nothing to convert. //! //! Retiring a converter means deleting its step and raising //! [`MIN_SUPPORTED_DATA_VERSION`] to that step's `to`. Data stamped below the //! minimum is left untouched and reported, so the user can upgrade through a -//! release that still converts it instead of losing it. +//! release that still converts it instead of losing it. Leftovers that were +//! converted lazily rather than by a step (v1 collection directories, store +//! scripts, store runners) are reported the same way. Steps 1–3 were retired +//! after 3.6.0, the last release carrying them. The cookie jar rekey shipped +//! first in 3.6.0 and stays until 3.5.x data has had time to pass through it. -use serde::Serialize; use serde_json::{Map, Value}; use std::collections::HashMap; -use std::fs; -use std::path::Path; -use std::sync::Mutex; +use std::path::{Path, PathBuf}; -use super::collections::{COLLECTIONS_DIR, absorb_store_scripts, convert_v1_dir}; -use super::store_files::{ - MAIN_STORE, migrate_store_split, read_store, store_file_for, write_store, -}; +use super::collections::{COLLECTION_INDEX_KEY, COLLECTIONS_DIR}; +use super::store_files::{MAIN_STORE, read_store, store_file_for, write_store}; -pub const MIN_SUPPORTED_DATA_VERSION: u32 = 0; +/// The version a fresh install is stamped with and the last step lifts to. +pub const CURRENT_DATA_VERSION: u32 = 4; +pub const MIN_SUPPORTED_DATA_VERSION: u32 = 3; +const _: () = assert!(MIN_SUPPORTED_DATA_VERSION <= CURRENT_DATA_VERSION); const DATA_VERSION_KEY: &str = "dataVersion"; -const STORE_SCRIPTS_KEY: &str = "persistedScripts"; const GLOBAL_STORE_COLLECTIONS_KEY: &str = "collections"; +const STORE_RUNNERS_KEY: &str = "collectionRunners"; +const MIGRATED_RUNNER_COLLECTIONS_KEY: &str = "runnersMigratedCollections"; +const STORE_SCRIPTS_KEY: &str = "persistedScripts"; +const V1_MARKER: &str = "collection.json"; +const V2_MARKER: &str = "collection.yaml"; const COOKIE_JAR_KEY: &str = "cookieJar"; const DEFAULT_COOKIE_ENVIRONMENT: &str = "default"; @@ -38,67 +44,42 @@ struct Step { run: fn(&Path) -> Result<(), String>, } -const STEPS: &[Step] = &[ - Step { - to: 1, - name: "store split", - run: store_split, - }, - Step { - to: 2, - name: "collection format v1 to v2", - run: convert_v1_collections, - }, - Step { - to: 3, - name: "scripts from the store into request files", - run: move_store_scripts, - }, - Step { - to: 4, - name: "cookie jar ids", - run: rekey_cookies, - }, -]; - -/// Something the user has to know about after the chain ran. -#[derive(Debug, Clone, PartialEq, Serialize)] -#[serde(tag = "kind", rename_all = "camelCase")] +const STEPS: &[Step] = &[Step { + to: 4, + name: "cookie jar ids", + run: rekey_cookies, +}]; + +/// Something the chain could not do, logged at startup. +#[derive(Debug, Clone, PartialEq)] pub enum MigrationIssue { /// A step failed; it is retried on the next launch. - #[serde(rename_all = "camelCase")] Failed { step: String, message: String }, /// The data predates every step this build still carries. - #[serde(rename_all = "camelCase")] TooOld { found: u32, min: u32 }, /// Data in a format no step converts any more, left in place. - #[serde(rename_all = "camelCase")] Unsupported { data: String }, } -/// Issues from this launch's migration run, for the frontend to show. -#[derive(Default)] -pub struct MigrationStatus(pub Mutex>); - -#[tauri::command] -pub async fn data_migration_status( - state: tauri::State<'_, MigrationStatus>, -) -> Result, String> { - Ok(state.0.lock().unwrap().clone()) -} - -/// Brings the data in `dir` up to the last step's version. +/// Brings the data in `dir` up to [`CURRENT_DATA_VERSION`]. /// /// @param dir - The app data directory -/// @returns Everything the user needs to be told about +/// @returns Everything that was left unconverted pub fn run(dir: &Path) -> Vec { - run_steps(dir, STEPS, MIN_SUPPORTED_DATA_VERSION) + run_steps(dir, STEPS, MIN_SUPPORTED_DATA_VERSION, CURRENT_DATA_VERSION) } -fn run_steps(dir: &Path, steps: &[Step], min_supported: u32) -> Vec { +fn run_steps(dir: &Path, steps: &[Step], min_supported: u32, current: u32) -> Vec { let mut issues = Vec::new(); - let found = stored_version(dir); + if !dir.join(MAIN_STORE).exists() { + if let Err(message) = stamp_version(dir, current) { + eprintln!("stamping a fresh install failed: {}", message); + } + return issues; + } + + let found = stored_version(dir); if found < min_supported { issues.push(MigrationIssue::TooOld { found, @@ -118,11 +99,13 @@ fn run_steps(dir: &Path, steps: &[Step], min_supported: u32) -> Vec Result<(), String> { write_store(&path, &main) } -/// Collections kept in the main store itself, from before collections had -/// their own directories. Their converter was retired. -fn holds_global_store_collections(dir: &Path) -> bool { - matches!( - read_store(&dir.join(MAIN_STORE)).get(GLOBAL_STORE_COLLECTIONS_KEY), - Some(Value::Array(collections)) if !collections.is_empty() - ) -} - -/// Collection directories the app owns. Linked collections live in the -/// user's own checkouts and are only rewritten when the user saves them. -fn app_collection_dirs(dir: &Path) -> Result, String> { - let root = dir.join(COLLECTIONS_DIR); - if !root.is_dir() { - return Ok(Vec::new()); +/// Data whose converter was retired, by name. +/// +/// @param dir - The app data directory +/// @returns One name per kind of leftover found +fn unsupported_leftovers(dir: &Path) -> Vec<&'static str> { + let main = read_store(&dir.join(MAIN_STORE)); + let mut found = Vec::new(); + if holds_non_empty_array(&main, GLOBAL_STORE_COLLECTIONS_KEY) { + found.push("globalStoreCollections"); + } + if holds_runners_of_registered_collections(&main) { + found.push("collectionRunners"); + } + if holds_scripts_of_registered_collections(&main) { + found.push("storeScripts"); + } + if holds_v1_collections(dir, &main) { + found.push("v1Collections"); } - let mut dirs: Vec<_> = fs::read_dir(&root) - .map_err(|e| format!("Failed to read {}: {}", root.display(), e))? - .filter_map(Result::ok) - .map(|entry| entry.path()) - .filter(|path| path.is_dir()) - .collect(); - dirs.sort(); - Ok(dirs) + found } -fn store_split(dir: &Path) -> Result<(), String> { - migrate_store_split(dir).map(|_| ()) +fn holds_non_empty_array(main: &Map, key: &str) -> bool { + matches!(main.get(key), Some(Value::Array(items)) if !items.is_empty()) } -fn convert_v1_collections(dir: &Path) -> Result<(), String> { - let failures: Vec = app_collection_dirs(dir)? +/// Store runners owned (by their first request) by a collection that is still +/// registered and was not migrated yet. Runners of deleted collections kept +/// the old migration pending forever and are orphans, not worth a notice. +fn holds_runners_of_registered_collections(main: &Map) -> bool { + let Some(Value::Array(runners)) = main.get(STORE_RUNNERS_KEY) else { + return false; + }; + let registered = registered_collection_ids(main); + let migrated: Vec<&str> = match main.get(MIGRATED_RUNNER_COLLECTIONS_KEY) { + Some(Value::Array(ids)) => ids.iter().filter_map(Value::as_str).collect(), + _ => Vec::new(), + }; + runners .iter() - .filter_map(|path| { - convert_v1_dir(path) - .err() - .map(|e| format!("{}: {}", path.display(), e)) - }) - .collect(); - if failures.is_empty() { - Ok(()) - } else { - Err(failures.join("; ")) - } + .filter_map(|runner| runner.pointer("/requests/0/collectionId")?.as_str()) + .any(|owner| registered.iter().any(|id| id == owner) && !migrated.contains(&owner)) } -fn move_store_scripts(dir: &Path) -> Result<(), String> { - let main_path = dir.join(MAIN_STORE); - let mut main = read_store(&main_path); - let Some(Value::Object(scripts)) = main.get(STORE_SCRIPTS_KEY).cloned() else { - return Ok(()); +/// Store scripts keyed `{collectionId}_{requestId}` for a collection that is +/// still registered. Entries of collections long gone are orphans and not +/// worth a notice. +fn holds_scripts_of_registered_collections(main: &Map) -> bool { + let Some(Value::Object(scripts)) = main.get(STORE_SCRIPTS_KEY) else { + return false; }; + registered_collection_ids(main) + .iter() + .any(|id| scripts.keys().any(|key| key.starts_with(&format!("{id}_")))) +} - let mut remaining: Map = scripts; - let before = remaining.len(); - let mut failures = Vec::new(); - for path in app_collection_dirs(dir)? { - if let Err(e) = absorb_store_scripts(&path, &mut remaining) { - failures.push(format!("{}: {}", path.display(), e)); - } +fn registered_collection_ids(main: &Map) -> Vec { + match main.get(COLLECTION_INDEX_KEY) { + Some(Value::Object(index)) => index.keys().cloned().collect(), + _ => Vec::new(), } +} - if remaining.len() != before { - main.insert(STORE_SCRIPTS_KEY.to_string(), Value::Object(remaining)); - write_store(&main_path, &main)?; +fn registered_collection_dirs(main: &Map) -> Vec { + match main.get(COLLECTION_INDEX_KEY) { + Some(Value::Object(index)) => index + .values() + .filter_map(Value::as_str) + .map(PathBuf::from) + .collect(), + _ => Vec::new(), } +} - if failures.is_empty() { - Ok(()) - } else { - Err(failures.join("; ")) - } +fn app_collection_dirs(dir: &Path) -> Vec { + std::fs::read_dir(dir.join(COLLECTIONS_DIR)) + .map(|entries| { + entries + .filter_map(Result::ok) + .map(|entry| entry.path()) + .collect() + }) + .unwrap_or_default() +} + +fn holds_v1_collections(dir: &Path, main: &Map) -> bool { + app_collection_dirs(dir) + .into_iter() + .chain(registered_collection_dirs(main)) + .any(|path| path.join(V1_MARKER).exists() && !path.join(V2_MARKER).exists()) } /// Rekeys every cookie jar entry to `{environment}|{domain}|{path}|{name}`, /// the id cookies have carried since 2.12.6, and keeps only the newest entry -/// per id. Mirrors `CookieRepository._migrateLegacyIds`, which the webview -/// still runs lazily until this step retires it. +/// per id. fn rekey_cookies(dir: &Path) -> Result<(), String> { let path = dir.join(store_file_for(COOKIE_JAR_KEY)); let mut store = read_store(&path); @@ -304,6 +304,7 @@ fn updated_at(cookie: &Map) -> f64 { mod tests { use super::*; use serde_json::json; + use std::fs; use tempfile::TempDir; fn write_main(dir: &Path, value: Value) { @@ -314,24 +315,6 @@ mod tests { serde_json::from_str(&fs::read_to_string(dir.join(MAIN_STORE)).unwrap()).unwrap() } - fn write_v1_collection(dir: &Path) -> std::path::PathBuf { - let collection = dir.join(COLLECTIONS_DIR).join("pets"); - fs::create_dir_all(collection.join("requests")).unwrap(); - fs::write( - collection.join("collection.json"), - json!({ - "id": "c1", - "name": "Pets", - "baseUrl": "https://api.example.com", - "endpoints": [{"id": "r1", "name": "List", "method": "GET", "path": "/pets"}], - "folders": [] - }) - .to_string(), - ) - .unwrap(); - collection - } - fn failing(_: &Path) -> Result<(), String> { Err("boom".into()) } @@ -340,19 +323,48 @@ mod tests { Ok(()) } + fn unsupported(data: &str) -> MigrationIssue { + MigrationIssue::Unsupported { data: data.into() } + } + + #[test] + fn the_last_step_lifts_to_the_current_version() { + if let Some(last) = STEPS.last() { + assert_eq!(last.to, CURRENT_DATA_VERSION); + } + } + #[test] - fn a_fresh_install_runs_every_step_and_is_stamped_current() { + fn a_fresh_install_is_stamped_current_without_running_a_step() { + let temp = TempDir::new().unwrap(); + + let issues = run_steps( + temp.path(), + &[Step { + to: 9, + name: "never", + run: failing, + }], + 0, + 9, + ); + + assert!(issues.is_empty()); + assert_eq!(read_main(temp.path()), json!({"dataVersion": 9})); + } + + #[test] + fn a_fresh_install_runs_clean_and_is_stamped_current() { let temp = TempDir::new().unwrap(); assert!(run(temp.path()).is_empty()); - assert_eq!(stored_version(temp.path()), STEPS.last().unwrap().to); + assert_eq!(stored_version(temp.path()), CURRENT_DATA_VERSION); } #[test] fn a_second_run_changes_nothing() { let temp = TempDir::new().unwrap(); - write_v1_collection(temp.path()); run(temp.path()); let after_first = read_main(temp.path()); @@ -364,6 +376,7 @@ mod tests { #[test] fn a_failed_step_keeps_the_previous_stamp_and_stops_the_chain() { let temp = TempDir::new().unwrap(); + write_main(temp.path(), json!({})); let steps = [ Step { to: 1, @@ -382,7 +395,7 @@ mod tests { }, ]; - let issues = run_steps(temp.path(), &steps, 0); + let issues = run_steps(temp.path(), &steps, 0, 3); assert_eq!( issues, @@ -397,6 +410,7 @@ mod tests { #[test] fn a_failed_step_is_retried_on_the_next_launch() { let temp = TempDir::new().unwrap(); + write_main(temp.path(), json!({})); run_steps( temp.path(), &[Step { @@ -405,6 +419,7 @@ mod tests { run: failing, }], 0, + 1, ); let issues = run_steps( @@ -415,6 +430,7 @@ mod tests { run: succeeding, }], 0, + 1, ); assert!(issues.is_empty()); @@ -437,6 +453,7 @@ mod tests { run: failing, }], 2, + 5, ); assert_eq!(issues, vec![MigrationIssue::TooOld { found: 1, min: 2 }]); @@ -447,125 +464,172 @@ mod tests { } #[test] - fn the_chain_converts_v1_collections_and_moves_store_scripts() { + fn an_unstamped_store_is_too_old_for_the_retired_chain() { let temp = TempDir::new().unwrap(); - let collection = write_v1_collection(temp.path()); write_main( temp.path(), - json!({ - "requestHistory": [], - "persistedScripts": { - "c1_r1": {"preRequestScript": "", "testScript": "check();"}, - "elsewhere_r9": {"testScript": "linked();"} - } - }), + json!({"requestHistory": [], "persistedScripts": {"c1_r1": {"testScript": "check();"}}}), ); + let before = read_main(temp.path()); - assert!(run(temp.path()).is_empty()); + let issues = run(temp.path()); - assert!(collection.join("collection.yaml").exists()); - assert!(!collection.join("collection.json").exists()); - assert!(!collection.join("runners").exists()); - let request = fs::read_to_string(collection.join("list.yaml")).unwrap(); - assert!(request.contains("check();"), "{}", request); - let main = read_main(temp.path()); assert_eq!( - main["persistedScripts"], - json!({"elsewhere_r9": {"testScript": "linked();"}}) + issues, + vec![MigrationIssue::TooOld { + found: 0, + min: MIN_SUPPORTED_DATA_VERSION + }] ); - assert!( - main.get("requestHistory").is_none(), - "store split did not run" + assert_eq!(read_main(temp.path()), before); + } + + #[test] + fn data_stamped_by_the_last_converting_release_is_accepted() { + let temp = TempDir::new().unwrap(); + write_main(temp.path(), json!({"dataVersion": 4, "theme": "dark"})); + + assert!(run(temp.path()).is_empty()); + + assert_eq!( + read_main(temp.path()), + json!({"dataVersion": 4, "theme": "dark"}) ); - assert!(temp.path().join("resonance-history.json").exists()); } #[test] - fn the_chain_rekeys_cookies_after_the_store_split_moved_them() { + fn global_store_collections_are_reported_and_kept() { let temp = TempDir::new().unwrap(); write_main( temp.path(), - json!({ - "cookieJar": [ - {"id": "old-1", "domain": "a.test", "path": "/", "name": "s", "value": "stale", "updatedAt": 1}, - {"id": "default|a.test|/|s", "environmentId": "default", "domain": "a.test", "path": "/", "name": "s", "value": "fresh", "updatedAt": 2}, - {"id": "env1|b.test|/|t", "environmentId": "env1", "domain": "b.test", "path": "/", "name": "t", "value": "kept"} - ] - }), + json!({"dataVersion": 4, "collections": [{"id": "old"}]}), ); - assert!(run(temp.path()).is_empty()); + let issues = run(temp.path()); - assert!(read_main(temp.path()).get("cookieJar").is_none()); - let cookies = read_store(&temp.path().join(store_file_for("cookieJar"))); + assert_eq!(issues, vec![unsupported("globalStoreCollections")]); assert_eq!( - cookies["cookieJar"], - json!([ - {"id": "default|a.test|/|s", "environmentId": "default", "domain": "a.test", "path": "/", "name": "s", "value": "fresh", "updatedAt": 2}, - {"id": "env1|b.test|/|t", "environmentId": "env1", "domain": "b.test", "path": "/", "name": "t", "value": "kept"} - ]) + read_main(temp.path())["collections"], + json!([{"id": "old"}]) ); - assert_eq!(stored_version(temp.path()), 4); } #[test] - fn a_jar_with_current_ids_is_not_rewritten() { - let cookies = vec![ - json!({"id": "default|a.test|/|s", "environmentId": "default", "domain": "a.test", "path": "/", "name": "s"}), - json!({"id": "env1|b.test|/|t", "environmentId": "env1", "domain": "b.test", "path": "/", "name": "t"}), - ]; + fn leftover_store_runners_are_reported_only_for_an_unmigrated_registered_collection() { + let temp = TempDir::new().unwrap(); + let runner = json!([{"id": "runner_1", "requests": [{"collectionId": "c1"}]}]); + write_main( + temp.path(), + json!({"dataVersion": 4, "collectionIndex": {"c1": "/somewhere/c1"}, "collectionRunners": runner}), + ); + assert_eq!(run(temp.path()), vec![unsupported("collectionRunners")]); - assert_eq!(rekeyed_cookies(&cookies), None); + write_main( + temp.path(), + json!({"dataVersion": 4, "collectionIndex": {"c2": "/somewhere/c2"}, "collectionRunners": runner}), + ); + assert!(run(temp.path()).is_empty()); + + write_main( + temp.path(), + json!({ + "dataVersion": 4, + "collectionIndex": {"c1": "/somewhere/c1"}, + "collectionRunners": runner, + "runnersMigratedCollections": ["c1"] + }), + ); + assert!(run(temp.path()).is_empty()); + + write_main( + temp.path(), + json!({"dataVersion": 4, "collectionRunners": null, "collectionRunnersLegacyBackup": {"runners": [1]}}), + ); + assert!(run(temp.path()).is_empty()); } #[test] - fn a_rekeyed_entry_keeps_its_position_and_the_older_duplicate_loses() { - let cookies = vec![ - json!({"id": "x", "environmentId": null, "domain": "a.test", "path": "/", "name": "s", "value": "newer", "updatedAt": 5}), - json!({"id": "y", "domain": "c.test", "path": "/", "name": "u"}), - json!({"id": "default|a.test|/|s", "environmentId": "default", "domain": "a.test", "path": "/", "name": "s", "value": "older", "updatedAt": 3}), - ]; - - let rekeyed = rekeyed_cookies(&cookies).unwrap(); + fn store_scripts_are_reported_only_for_a_registered_collection() { + let temp = TempDir::new().unwrap(); + write_main( + temp.path(), + json!({ + "dataVersion": 4, + "collectionIndex": {"c1": "/somewhere/c1"}, + "persistedScripts": {"gone_r1": {"testScript": "orphan();"}} + }), + ); + assert!(run(temp.path()).is_empty()); - assert_eq!(rekeyed.len(), 2); - assert_eq!(rekeyed[0]["id"], "default|a.test|/|s"); - assert_eq!(rekeyed[0]["value"], "newer"); - assert_eq!(rekeyed[1]["id"], "default|c.test|/|u"); - assert_eq!(rekeyed[1]["environmentId"], "default"); + write_main( + temp.path(), + json!({ + "dataVersion": 4, + "collectionIndex": {"c1": "/somewhere/c1"}, + "persistedScripts": {"c1_r1": {"testScript": "mine();"}} + }), + ); + assert_eq!(run(temp.path()), vec![unsupported("storeScripts")]); } #[test] - fn a_missing_jar_is_a_no_op() { + fn a_v1_collection_directory_is_reported_whether_app_owned_or_linked() { let temp = TempDir::new().unwrap(); + let owned = temp.path().join(COLLECTIONS_DIR).join("pets"); + fs::create_dir_all(&owned).unwrap(); + fs::write(owned.join("collection.json"), "{}").unwrap(); + write_main(temp.path(), json!({"dataVersion": 4})); + assert_eq!(run(temp.path()), vec![unsupported("v1Collections")]); - assert_eq!(rekey_cookies(temp.path()), Ok(())); + fs::write(owned.join("collection.yaml"), "resonanceFormat: 2\n").unwrap(); + assert!(run(temp.path()).is_empty()); - assert!(!temp.path().join(store_file_for("cookieJar")).exists()); + let linked = TempDir::new().unwrap(); + fs::write(linked.path().join("collection.json"), "{}").unwrap(); + write_main( + temp.path(), + json!({"dataVersion": 4, "collectionIndex": {"c9": linked.path().to_string_lossy()}}), + ); + assert_eq!(run(temp.path()), vec![unsupported("v1Collections")]); } #[test] - fn global_store_collections_are_reported_and_kept() { + fn data_stamped_by_3_5_has_its_cookies_rekeyed() { let temp = TempDir::new().unwrap(); - write_main(temp.path(), json!({"collections": [{"id": "old"}]})); + write_main(temp.path(), json!({"dataVersion": 3})); + fs::write( + temp.path().join(store_file_for("cookieJar")), + json!({ + "cookieJar": [ + {"id": "old-1", "domain": "a.test", "path": "/", "name": "s", "value": "stale", "updatedAt": 1}, + {"id": "default|a.test|/|s", "environmentId": "default", "domain": "a.test", "path": "/", "name": "s", "value": "fresh", "updatedAt": 2}, + {"id": "env1|b.test|/|t", "environmentId": "env1", "domain": "b.test", "path": "/", "name": "t", "value": "kept"} + ] + }) + .to_string(), + ) + .unwrap(); - let issues = run(temp.path()); + assert!(run(temp.path()).is_empty()); + let cookies = read_store(&temp.path().join(store_file_for("cookieJar"))); assert_eq!( - issues, - vec![MigrationIssue::Unsupported { - data: "globalStoreCollections".into() - }] - ); - assert_eq!( - read_main(temp.path())["collections"], - json!([{"id": "old"}]) + cookies["cookieJar"], + json!([ + {"id": "default|a.test|/|s", "environmentId": "default", "domain": "a.test", "path": "/", "name": "s", "value": "fresh", "updatedAt": 2}, + {"id": "env1|b.test|/|t", "environmentId": "env1", "domain": "b.test", "path": "/", "name": "t", "value": "kept"} + ]) ); + assert_eq!(stored_version(temp.path()), 4); } #[test] - fn an_issue_serializes_with_a_kind_tag() { - let value = serde_json::to_value(MigrationIssue::TooOld { found: 0, min: 2 }).unwrap(); - assert_eq!(value, json!({"kind": "tooOld", "found": 0, "min": 2})); + fn a_jar_with_current_ids_is_not_rewritten() { + let cookies = vec![ + json!({"id": "default|a.test|/|s", "environmentId": "default", "domain": "a.test", "path": "/", "name": "s"}), + json!({"id": "env1|b.test|/|t", "environmentId": "env1", "domain": "b.test", "path": "/", "name": "t"}), + ]; + + assert_eq!(rekeyed_cookies(&cookies), None); } } diff --git a/src-tauri/src/commands/grpc_proto.rs b/src-tauri/src/commands/grpc_proto.rs index 50542c54..fcaeacb1 100644 --- a/src-tauri/src/commands/grpc_proto.rs +++ b/src-tauri/src/commands/grpc_proto.rs @@ -283,6 +283,35 @@ service Billing { rpc Get (Invoice) returns (Invoice); } assert!(pool.get_message_by_name("common.Money").is_some()); } + #[test] + fn skeletons_of_recursive_messages_terminate() { + let dir = TempDir::new().unwrap(); + let service = dir.path().join("tree.proto"); + fs::write( + &service, + r#"syntax = "proto3"; +package tree; +import "google/protobuf/struct.proto"; +message Node { Node child = 1; repeated Node children = 2; map named = 3; string label = 4; } +message Input { Node root = 1; google.protobuf.Struct meta = 2; } +"#, + ) + .unwrap(); + let pool = + parse_proto_file(service.to_str().unwrap(), &[dir.path().to_path_buf()]).unwrap(); + let input = pool.get_message_by_name("tree.Input").unwrap(); + + let skeleton = crate::commands::grpc_reflection::generate_message_skeleton(&input); + + assert_eq!(skeleton["root"]["label"], ""); + assert_eq!(skeleton["root"]["child"], serde_json::json!({})); + assert_eq!( + skeleton["root"]["named"], + serde_json::json!({"key": "value"}) + ); + assert!(skeleton["meta"].is_object()); + } + #[test] fn a_missing_import_is_reported() { let dir = TempDir::new().unwrap(); diff --git a/src-tauri/src/commands/grpc_reflection.rs b/src-tauri/src/commands/grpc_reflection.rs index 41fba665..0c405dcc 100644 --- a/src-tauri/src/commands/grpc_reflection.rs +++ b/src-tauri/src/commands/grpc_reflection.rs @@ -624,27 +624,43 @@ pub async fn grpc_get_input_skeleton( } pub(crate) fn generate_message_skeleton(desc: &prost_reflect::MessageDescriptor) -> Value { + message_skeleton(desc, &mut Vec::new()) +} + +/// Builds one message's skeleton. `visiting` holds the messages on the current +/// path; a message that refers back to one of them (directly, or through the +/// mutually recursive `google.protobuf.Struct`/`Value`) becomes `{}` instead of +/// recursing until the stack overflows. +fn message_skeleton(desc: &prost_reflect::MessageDescriptor, visiting: &mut Vec) -> Value { + if visiting.iter().any(|name| name == desc.full_name()) { + return Value::Object(serde_json::Map::new()); + } + visiting.push(desc.full_name().to_string()); + let mut obj = serde_json::Map::new(); for field in desc.fields() { let field_name = field.name().to_string(); - let field_value = generate_field_skeleton(&field); - if field.is_list() { - obj.insert(field_name, Value::Array(vec![field_value])); - } else if field.is_map() { + if field.is_map() { let mut map_obj = serde_json::Map::new(); map_obj.insert("key".to_string(), Value::String("value".to_string())); obj.insert(field_name, Value::Object(map_obj)); + } else if field.is_list() { + obj.insert( + field_name, + Value::Array(vec![field_skeleton(&field, visiting)]), + ); } else { - obj.insert(field_name, field_value); + obj.insert(field_name, field_skeleton(&field, visiting)); } } + visiting.pop(); Value::Object(obj) } -fn generate_field_skeleton(field: &prost_reflect::FieldDescriptor) -> Value { +fn field_skeleton(field: &prost_reflect::FieldDescriptor, visiting: &mut Vec) -> Value { use prost_reflect::Kind; match field.kind() { @@ -656,7 +672,7 @@ fn generate_field_skeleton(field: &prost_reflect::FieldDescriptor) -> Value { Kind::Bool => Value::Bool(false), Kind::String => Value::String(String::new()), Kind::Bytes => Value::String(String::new()), - Kind::Message(msg_desc) => generate_message_skeleton(&msg_desc), + Kind::Message(msg_desc) => message_skeleton(&msg_desc, visiting), Kind::Enum(enum_desc) => { if let Some(first_value) = enum_desc.values().next() { Value::String(first_value.name().to_string()) diff --git a/src-tauri/src/commands/mqtt.rs b/src-tauri/src/commands/mqtt.rs index d4846866..581cae76 100644 --- a/src-tauri/src/commands/mqtt.rs +++ b/src-tauri/src/commands/mqtt.rs @@ -2,7 +2,7 @@ use super::api_request::ClientCertConfig; use super::tab_sessions::{CommandAck, Session, TabSessions, require_tab_id}; use base64::Engine; use base64::engine::general_purpose::STANDARD as BASE64_STANDARD; -use rumqttc::{AsyncClient, Event, LastWill, MqttOptions, Packet, QoS, Transport}; +use rumqttc::{AsyncClient, Event, LastWill, MqttOptions, Outgoing, Packet, QoS, Transport}; use serde::{Deserialize, Serialize}; use std::time::Duration; use tauri::{AppHandle, Emitter, State}; @@ -42,9 +42,17 @@ struct MqttConnection { broker: String, } +/// How long a clean disconnect may take before the event loop is aborted. +const DISCONNECT_GRACE: Duration = Duration::from_secs(2); + impl MqttConnection { - async fn shutdown(self) { - let _ = self.client.disconnect().await; + /// Queues DISCONNECT and lets the event loop write it before stopping the + /// loop. Aborting straight away dropped the socket with the packet still + /// queued, so the broker saw an unclean close and published the Last Will. + async fn shutdown(mut self) { + if self.client.disconnect().await.is_ok() { + let _ = tokio::time::timeout(DISCONNECT_GRACE, &mut self.poll_handle).await; + } self.poll_handle.abort(); } } @@ -406,7 +414,8 @@ async fn establish_connection( ), ); } - Ok(Event::Incoming(Packet::Disconnect)) => { + Ok(Event::Incoming(Packet::Disconnect)) + | Ok(Event::Outgoing(Outgoing::Disconnect)) => { break; } Ok(_) => {} diff --git a/src-tauri/src/commands/scripts.rs b/src-tauri/src/commands/scripts.rs index 4359cf90..aad5bd52 100644 --- a/src-tauri/src/commands/scripts.rs +++ b/src-tauri/src/commands/scripts.rs @@ -1,112 +1,31 @@ -use super::store_files::MAIN_STORE as STORE_FILE; pub use resonance_core::scripts::*; -use serde_json::Value; -use std::collections::HashMap; use tauri::AppHandle; -use tauri_plugin_store::StoreExt; -const SCRIPTS_KEY: &str = "persistedScripts"; fn scripts_are_empty(scripts: &ScriptData) -> bool { scripts.pre_request_script.is_empty() && scripts.test_script.is_empty() } -/// Read a one-off script entry from the legacy global store. -fn read_legacy_store_script(app: &AppHandle, key: &str) -> Option { - read_legacy_store_scripts(app).get(key).cloned() -} - -/// Read once for a batch capture, without migrating or rewriting script data. -pub(crate) fn read_legacy_store_scripts(app: &AppHandle) -> HashMap { - let Ok(store) = app.store(STORE_FILE) else { - return HashMap::new(); - }; - store - .get(SCRIPTS_KEY) - .and_then(|v| serde_json::from_value(v).ok()) - .unwrap_or_default() -} - -/// Remove a single `"{collectionId}_{endpointId}"` entry from the legacy global -/// store. Best-effort: errors are swallowed because the authoritative copy -/// already lives in the per-endpoint file. -fn remove_store_script_entry(app: &AppHandle, collection_id: &str, endpoint_id: &str) { - let Ok(store) = app.store(STORE_FILE) else { - return; - }; - let mut map: HashMap = store - .get(SCRIPTS_KEY) - .and_then(|v| serde_json::from_value(v).ok()) - .unwrap_or_default(); - let key = format!("{}_{}", collection_id, endpoint_id); - if map.remove(&key).is_some() { - store.set(SCRIPTS_KEY.to_string(), serde_json::to_value(map).unwrap()); - let _ = store.save(); - } -} - -/// Drop every script entry keyed by the given collection from the legacy store. -/// Called by `collection_delete` to keep the store from accumulating orphans. -pub(crate) fn purge_store_scripts_for_collection(app: &AppHandle, collection_id: &str) { - let Ok(store) = app.store(STORE_FILE) else { - return; - }; - let mut map: HashMap = store - .get(SCRIPTS_KEY) - .and_then(|v| serde_json::from_value(v).ok()) - .unwrap_or_default(); - let prefix = format!("{}_", collection_id); - let before = map.len(); - map.retain(|k, _| !k.starts_with(&prefix)); - if map.len() != before { - store.set(SCRIPTS_KEY.to_string(), serde_json::to_value(map).unwrap()); - let _ = store.save(); - } -} - #[tauri::command] pub async fn script_get( app: AppHandle, collection_id: String, endpoint_id: String, ) -> Result { - let endpoint_data = super::collections::collection_get_endpoint_data( - app.clone(), - collection_id.clone(), - endpoint_id.clone(), - ) - .await?; + let endpoint_data = + super::collections::collection_get_endpoint_data(app, collection_id, endpoint_id).await?; - if let Some(value) = endpoint_data.scripts.clone() - && let Ok(scripts) = serde_json::from_value::(value) - { - return Ok(scripts); - } - - // Fallback: legacy global-store entry. Migrate into the per-endpoint file - // on hit so the next read goes through the fast path above. - let legacy_key = format!("{}_{}", collection_id, endpoint_id); - if let Some(scripts) = read_legacy_store_script(&app, &legacy_key) { - if !scripts_are_empty(&scripts) { - let _ = write_scripts_to_endpoint_file( - &app, - collection_id.clone(), - endpoint_id.clone(), - scripts.clone(), - ) - .await; - remove_store_script_entry(&app, &collection_id, &endpoint_id); - } - return Ok(scripts); - } - - Ok(ScriptData { - pre_request_script: String::new(), - test_script: String::new(), - }) + Ok(endpoint_data + .scripts + .and_then(|value| serde_json::from_value::(value).ok()) + .unwrap_or_else(|| ScriptData { + pre_request_script: String::new(), + test_script: String::new(), + })) } -async fn write_scripts_to_endpoint_file( - app: &AppHandle, +#[tauri::command] +pub async fn script_save( + app: AppHandle, collection_id: String, endpoint_id: String, scripts: ScriptData, @@ -125,23 +44,10 @@ async fn write_scripts_to_endpoint_file( }; super::collections::collection_save_endpoint_data( - app.clone(), + app, collection_id, endpoint_id, endpoint_data, ) .await } - -#[tauri::command] -pub async fn script_save( - app: AppHandle, - collection_id: String, - endpoint_id: String, - scripts: ScriptData, -) -> Result<(), String> { - write_scripts_to_endpoint_file(&app, collection_id.clone(), endpoint_id.clone(), scripts) - .await?; - remove_store_script_entry(&app, &collection_id, &endpoint_id); - Ok(()) -} diff --git a/src-tauri/src/commands/sse.rs b/src-tauri/src/commands/sse.rs index 797ec440..74b44f9c 100644 --- a/src-tauri/src/commands/sse.rs +++ b/src-tauri/src/commands/sse.rs @@ -133,6 +133,7 @@ struct PartialEvent { } impl PartialEvent { + #[cfg(test)] fn is_empty(&self) -> bool { self.event.is_none() && self.data.is_empty() && self.id.is_none() && self.retry.is_none() } @@ -481,10 +482,9 @@ async fn stream_loop( silent_reconnects += 1; } - // Flush any trailing partial event if the stream ended on a non-empty buffer. - if !partial.is_empty() { - dispatch(app, tab_id, url, &mut partial, &mut last_event_id); - } + // An event cut off by the end of the stream is dropped with `partial`, + // as the spec requires: dispatching it would show a truncated message + // and advance Last-Event-ID past an event the server never finished. if silent_reconnects >= MAX_SILENT_RECONNECTS { emit_error(app, tab_id, url, None, give_up_message()); diff --git a/src-tauri/src/commands/sse_body_tests.rs b/src-tauri/src/commands/sse_body_tests.rs index c540a34d..e4566263 100644 --- a/src-tauri/src/commands/sse_body_tests.rs +++ b/src-tauri/src/commands/sse_body_tests.rs @@ -70,3 +70,54 @@ async fn get_replays_json_and_text_bodies_when_the_stream_reconnects() { server.abort(); } } + +#[tokio::test] +async fn an_event_cut_off_by_the_end_of_stream_does_not_advance_last_event_id() { + let (tx, mut received) = mpsc::unbounded_channel(); + let router = Router::new().fallback(any(move |request: Request| { + let tx = tx.clone(); + async move { + tx.send( + request + .headers() + .get("last-event-id") + .map(|v| v.to_str().unwrap().to_string()), + ) + .unwrap(); + ( + [("content-type", "text/event-stream")], + "id: 1\nretry: 250\ndata: whole\n\nid: 2\ndata: part-1\n", + ) + } + })); + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let target = StreamTarget { + tab_id: "partial-test".into(), + url: format!("http://{}/events", listener.local_addr().unwrap()), + method: Method::GET, + body: None, + headers: HashMap::new(), + last_event_id: None, + }; + let server = tokio::spawn(async move { axum::serve(listener, router).await.unwrap() }); + let app = tauri::test::mock_app(); + let handle = app.handle().clone(); + let request = serde_json::from_value(json!({"tabId":"partial-test","url":target.url})).unwrap(); + let client = build_sse_client(&request, ProxyAction::Disable).unwrap(); + let (shutdown, mut cancelled) = oneshot::channel(); + let stream = + tokio::spawn(async move { stream_loop(&handle, &client, &target, &mut cancelled).await }); + for expected in [None, Some("1".to_string())] { + let last_event_id = timeout(Duration::from_secs(5), received.recv()) + .await + .unwrap() + .unwrap(); + assert_eq!(last_event_id, expected); + } + shutdown.send(()).unwrap(); + timeout(Duration::from_secs(2), stream) + .await + .unwrap() + .unwrap(); + server.abort(); +} diff --git a/src-tauri/src/commands/store_files.rs b/src-tauri/src/commands/store_files.rs index 62eeba97..f21b289f 100644 --- a/src-tauri/src/commands/store_files.rs +++ b/src-tauri/src/commands/store_files.rs @@ -11,7 +11,7 @@ use serde_json::{Map, Value}; use std::fs; use std::io::Write; -use std::path::{Path, PathBuf}; +use std::path::Path; use super::fs_secure::restrict_file; @@ -107,68 +107,9 @@ pub(crate) fn write_store(path: &Path, map: &Map) -> Result<(), S result } -/// Moves every relocated key out of the main store and into its own file. -/// -/// Runs on every launch and is a cheap no-op once there is nothing left to -/// move. The new files are written before the main store is rewritten, so an -/// interruption leaves the keys still in the main store and the next launch -/// simply redoes the same move. -/// -/// @param dir - The app data directory holding the store files -/// @returns How many keys were relocated -pub fn migrate_store_split(dir: &Path) -> Result { - let main_path = dir.join(MAIN_STORE); - if !main_path.exists() { - return Ok(0); - } - - let mut main = read_store(&main_path); - let mut pending: Vec<(PathBuf, Map)> = Vec::new(); - let mut moved = 0u32; - - for file in ALL_STORES.iter().filter(|name| **name != MAIN_STORE) { - let target_path = dir.join(file); - let mut target = read_store(&target_path); - let mut changed = false; - - for (key, _) in RELOCATED_KEYS.iter().filter(|(_, owner)| owner == file) { - if let Some(value) = main.remove(*key) { - target.insert((*key).to_string(), value); - changed = true; - moved += 1; - } - } - - if changed { - pending.push((target_path, target)); - } - } - - if moved == 0 { - return Ok(0); - } - - for (path, map) in &pending { - write_store(path, map)?; - } - - write_store(&main_path, &main)?; - - Ok(moved) -} - #[cfg(test)] mod tests { use super::*; - use tempfile::TempDir; - - fn write_json(dir: &Path, name: &str, value: Value) { - fs::write(dir.join(name), serde_json::to_string(&value).unwrap()).unwrap(); - } - - fn read_json(dir: &Path, name: &str) -> Value { - serde_json::from_str(&fs::read_to_string(dir.join(name)).unwrap()).unwrap() - } #[test] fn relocated_keys_route_to_their_own_files() { @@ -198,116 +139,4 @@ mod tests { assert_eq!(store_file_for(key), MAIN_STORE, "key {} moved", key); } } - - #[test] - fn migration_moves_each_key_to_its_file_and_strips_it_from_main() { - let temp = TempDir::new().unwrap(); - write_json( - temp.path(), - MAIN_STORE, - serde_json::json!({ - "requestHistory": [{"id": "h1"}], - "workspace-tabs": [{"id": "t1"}], - "active-tab-id": "t1", - "cookieJar": [{"name": "sid"}], - "graphqlSchemaCache": {"https://api": {}}, - "settings": {"timeout": 30000}, - "collectionIndex": {"c1": "/tmp/c1"} - }), - ); - - let moved = migrate_store_split(temp.path()).unwrap(); - - assert_eq!(moved, 5); - - let main = read_json(temp.path(), MAIN_STORE); - assert_eq!(main["settings"]["timeout"], 30000); - assert_eq!(main["collectionIndex"]["c1"], "/tmp/c1"); - assert!(main.get("requestHistory").is_none()); - assert!(main.get("workspace-tabs").is_none()); - assert!(main.get("active-tab-id").is_none()); - assert!(main.get("cookieJar").is_none()); - assert!(main.get("graphqlSchemaCache").is_none()); - - assert_eq!( - read_json(temp.path(), HISTORY_STORE)["requestHistory"][0]["id"], - "h1" - ); - let tabs = read_json(temp.path(), TABS_STORE); - assert_eq!(tabs["workspace-tabs"][0]["id"], "t1"); - assert_eq!(tabs["active-tab-id"], "t1"); - assert_eq!( - read_json(temp.path(), COOKIES_STORE)["cookieJar"][0]["name"], - "sid" - ); - assert!(read_json(temp.path(), GRAPHQL_CACHE_STORE)["graphqlSchemaCache"].is_object()); - } - - #[test] - fn migration_is_idempotent() { - let temp = TempDir::new().unwrap(); - write_json( - temp.path(), - MAIN_STORE, - serde_json::json!({ "requestHistory": [{"id": "h1"}], "settings": {} }), - ); - - assert_eq!(migrate_store_split(temp.path()).unwrap(), 1); - assert_eq!(migrate_store_split(temp.path()).unwrap(), 0); - - assert_eq!( - read_json(temp.path(), HISTORY_STORE)["requestHistory"][0]["id"], - "h1" - ); - } - - #[test] - fn migration_leaves_an_already_split_store_untouched() { - let temp = TempDir::new().unwrap(); - write_json( - temp.path(), - MAIN_STORE, - serde_json::json!({ "settings": {} }), - ); - - assert_eq!(migrate_store_split(temp.path()).unwrap(), 0); - - assert!(!temp.path().join(HISTORY_STORE).exists()); - } - - #[test] - fn migration_without_a_main_store_is_a_no_op() { - let temp = TempDir::new().unwrap(); - - assert_eq!(migrate_store_split(temp.path()).unwrap(), 0); - } - - #[test] - fn migration_preserves_keys_already_in_a_target_file() { - let temp = TempDir::new().unwrap(); - write_json( - temp.path(), - MAIN_STORE, - serde_json::json!({ "active-tab-id": "t2" }), - ); - write_json( - temp.path(), - TABS_STORE, - serde_json::json!({ "workspace-tabs": [{"id": "kept"}] }), - ); - - migrate_store_split(temp.path()).unwrap(); - - let tabs = read_json(temp.path(), TABS_STORE); - assert_eq!(tabs["workspace-tabs"][0]["id"], "kept"); - assert_eq!(tabs["active-tab-id"], "t2"); - } - - #[test] - fn migration_treats_a_corrupt_main_store_as_empty() { - let temp = TempDir::new().unwrap(); - fs::write(temp.path().join(MAIN_STORE), "{not json").unwrap(); - - assert_eq!(migrate_store_split(temp.path()).unwrap(), 0); - } } diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 69492489..5f6778f5 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -20,7 +20,6 @@ use commands::{ collections_get_path, collections_git_branches, collections_git_status, collections_list, collections_load_errors, collections_open_existing, collections_pick_directory, }, - data_migrations::{MigrationStatus, data_migration_status}, graphql_subscription::{ GraphqlSubscriptionState, graphql_subscription_close, graphql_subscription_send, }, @@ -91,7 +90,6 @@ fn main() { .manage(MqttState::default()) .manage(PendingUpdate::default()) .manage(OAuth2State::default()) - .manage(MigrationStatus::default()) .on_window_event(|window, event| { if matches!(event, tauri::WindowEvent::Destroyed) { use tauri::Manager; @@ -112,8 +110,9 @@ fn main() { // Data migrations run before anything reads the store: the // webview loads theme, tabs and collections during boot. - let issues = commands::data_migrations::run(&dir); - *app.state::().0.lock().unwrap() = issues; + for issue in commands::data_migrations::run(&dir) { + eprintln!("data migration issue: {:?}", issue); + } for file in commands::store_files::ALL_STORES { commands::fs_secure::restrict_file(&dir.join(file)); @@ -126,7 +125,6 @@ fn main() { // App app_get_version, app_set_window_theme, - data_migration_status, // Store store_get, store_set, diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 8e6213af..db10c62d 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -1,7 +1,7 @@ { "$schema": "https://schema.tauri.app/config/2", "productName": "Resonance", - "version": "3.6.0", + "version": "3.6.1", "identifier": "io.github.db-mobile.resonance", "build": { "beforeDevCommand": "npm run build && node serve.js", diff --git a/src/i18n/locales/de.json b/src/i18n/locales/de.json index fd3a1177..3f5647be 100644 --- a/src/i18n/locales/de.json +++ b/src/i18n/locales/de.json @@ -578,12 +578,6 @@ "in_collection": "in {{name}}", "no_collection": "Füge zuerst eine Anfrage hinzu, damit der Runner zu einer Sammlung gehört", "data_file_not_portable": "Außerhalb des Sammlungsordners: dieser Pfad funktioniert nur auf diesem Rechner", - "migration_moved_one": "{{count}} gespeicherter Runner in seinen Sammlungsordner verschoben", - "migration_moved_other": "{{count}} gespeicherte Runner in ihre Sammlungsordner verschoben", - "migration_dropped_one": "{{count}} Runner hat Anfragen aus anderen Sammlungen verloren: {{names}}", - "migration_dropped_other": "{{count}} Runner haben Anfragen aus anderen Sammlungen verloren: {{names}}", - "migration_skipped_one": "{{count}} gespeicherter Runner hat keine Anfragen und wurde nicht verschoben", - "migration_skipped_other": "{{count}} gespeicherte Runner haben keine Anfragen und wurden nicht verschoben", "tags": "Tags", "tags_include": "Tags einschließen", "tags_exclude": "Tags ausschließen", @@ -658,13 +652,6 @@ "format": "Formatieren", "format_title": "JSON formatieren (Umschalt+Alt+F)" }, - "data_migration": { - "title": "Datenkonvertierung", - "failed": "Die Konvertierung Ihrer Daten in das aktuelle Format ist bei „{{step}}“ fehlgeschlagen: {{message}}. Es wurde nichts entfernt; die Konvertierung läuft beim nächsten Start von Resonance erneut.", - "too_old": "Ihre Daten stammen aus einer Resonance-Version, die diese Version nicht mehr konvertieren kann. Sie wurden nicht verändert. Installieren Sie Resonance {{release}}, starten Sie es einmal und aktualisieren Sie dann erneut.", - "global_store_collections": "In Ihren Einstellungen liegen noch Collections aus einer Resonance-Version vor März 2026. Diese Version kann sie nicht mehr konvertieren; sie wurden nicht verändert. Installieren Sie Resonance {{release}}, starten Sie es einmal und aktualisieren Sie dann erneut.", - "unknown": "Ihre Daten konnten nicht vollständig in das aktuelle Format konvertiert werden." - }, "environment_manager": { "create_default_name": "Neue Umgebung", "create_title": "Umgebung erstellen", diff --git a/src/i18n/locales/en.json b/src/i18n/locales/en.json index ce18491e..3d09168d 100644 --- a/src/i18n/locales/en.json +++ b/src/i18n/locales/en.json @@ -627,12 +627,6 @@ "in_collection": "in {{name}}", "no_collection": "Add a request first so the runner belongs to a collection", "data_file_not_portable": "Outside the collection folder: this path only works on this machine", - "migration_moved_one": "Moved {{count}} saved runner into its collection folder", - "migration_moved_other": "Moved {{count}} saved runners into their collection folders", - "migration_dropped_one": "{{count}} runner lost requests from other collections: {{names}}", - "migration_dropped_other": "{{count}} runners lost requests from other collections: {{names}}", - "migration_skipped_one": "{{count}} saved runner has no requests and was not moved", - "migration_skipped_other": "{{count}} saved runners have no requests and were not moved", "tags": "Tags", "tags_include": "Include tags", "tags_exclude": "Exclude tags", @@ -721,13 +715,6 @@ "keychain_unavailable": "Secrets unencrypted", "keychain_hint": "No OS keychain available. Secrets are stored locally without encryption at rest." }, - "data_migration": { - "title": "Data conversion", - "failed": "Converting your data to the current format failed at \"{{step}}\": {{message}}. Nothing was removed, and the conversion runs again the next time Resonance starts.", - "too_old": "Your data was saved by a Resonance release this version can no longer convert, so it was left unchanged. Install Resonance {{release}}, start it once, then update again.", - "global_store_collections": "Collections saved by a Resonance release from before March 2026 are still in your settings. This version can no longer convert them, so they were left unchanged. Install Resonance {{release}}, start it once, then update again.", - "unknown": "Your data could not be fully converted to the current format." - }, "environment_manager": { "create_default_name": "New Environment", "create_title": "Create Environment", diff --git a/src/i18n/locales/es.json b/src/i18n/locales/es.json index 444b919f..9e0eb851 100644 --- a/src/i18n/locales/es.json +++ b/src/i18n/locales/es.json @@ -572,12 +572,6 @@ "in_collection": "en {{name}}", "no_collection": "Añade primero una solicitud para que el runner pertenezca a una colección", "data_file_not_portable": "Fuera de la carpeta de la colección: esta ruta solo funciona en este equipo", - "migration_moved_one": "Se movió {{count}} runner guardado a la carpeta de su colección", - "migration_moved_other": "Se movieron {{count}} runners guardados a las carpetas de sus colecciones", - "migration_dropped_one": "{{count}} runner perdió solicitudes de otras colecciones: {{names}}", - "migration_dropped_other": "{{count}} runners perdieron solicitudes de otras colecciones: {{names}}", - "migration_skipped_one": "{{count}} runner guardado no tiene solicitudes y no se movió", - "migration_skipped_other": "{{count}} runners guardados no tienen solicitudes y no se movieron", "tags": "Etiquetas", "tags_include": "Incluir etiquetas", "tags_exclude": "Excluir etiquetas", @@ -652,13 +646,6 @@ "format": "Formatear", "format_title": "Formatear JSON (Mayús+Alt+F)" }, - "data_migration": { - "title": "Conversión de datos", - "failed": "La conversión de tus datos al formato actual falló en \"{{step}}\": {{message}}. No se eliminó nada y la conversión se repetirá la próxima vez que se inicie Resonance.", - "too_old": "Tus datos se guardaron con una versión de Resonance que esta versión ya no puede convertir, así que no se modificaron. Instala Resonance {{release}}, ábrelo una vez y vuelve a actualizar.", - "global_store_collections": "Tu configuración todavía contiene colecciones guardadas con una versión de Resonance anterior a marzo de 2026. Esta versión ya no puede convertirlas, así que no se modificaron. Instala Resonance {{release}}, ábrelo una vez y vuelve a actualizar.", - "unknown": "No se pudieron convertir por completo tus datos al formato actual." - }, "environment_manager": { "create_default_name": "Nuevo entorno", "create_title": "Crear entorno", diff --git a/src/i18n/locales/fr.json b/src/i18n/locales/fr.json index 867dfe04..da553a5f 100644 --- a/src/i18n/locales/fr.json +++ b/src/i18n/locales/fr.json @@ -572,12 +572,6 @@ "in_collection": "dans {{name}}", "no_collection": "Ajoutez d’abord une requête pour que le runner appartienne à une collection", "data_file_not_portable": "Hors du dossier de la collection : ce chemin ne fonctionne que sur cette machine", - "migration_moved_one": "{{count}} runner enregistré déplacé dans le dossier de sa collection", - "migration_moved_other": "{{count}} runners enregistrés déplacés dans les dossiers de leurs collections", - "migration_dropped_one": "{{count}} runner a perdu des requêtes d’autres collections : {{names}}", - "migration_dropped_other": "{{count}} runners ont perdu des requêtes d’autres collections : {{names}}", - "migration_skipped_one": "{{count}} runner enregistré n’a aucune requête et n’a pas été déplacé", - "migration_skipped_other": "{{count}} runners enregistrés n’ont aucune requête et n’ont pas été déplacés", "tags": "Tags", "tags_include": "Inclure des tags", "tags_exclude": "Exclure des tags", @@ -652,13 +646,6 @@ "format": "Formater", "format_title": "Formater le JSON (Maj+Alt+F)" }, - "data_migration": { - "title": "Conversion des données", - "failed": "La conversion de vos données au format actuel a échoué à l’étape « {{step}} » : {{message}}. Rien n’a été supprimé et la conversion sera relancée au prochain démarrage de Resonance.", - "too_old": "Vos données ont été enregistrées par une version de Resonance que cette version ne peut plus convertir ; elles n’ont pas été modifiées. Installez Resonance {{release}}, lancez-le une fois, puis mettez à jour de nouveau.", - "global_store_collections": "Vos paramètres contiennent encore des collections enregistrées par une version de Resonance antérieure à mars 2026. Cette version ne peut plus les convertir ; elles n’ont pas été modifiées. Installez Resonance {{release}}, lancez-le une fois, puis mettez à jour de nouveau.", - "unknown": "Vos données n’ont pas pu être entièrement converties au format actuel." - }, "environment_manager": { "create_default_name": "Nouvel environnement", "create_title": "Créer un environnement", diff --git a/src/i18n/locales/it.json b/src/i18n/locales/it.json index fdcb423b..f27ab674 100644 --- a/src/i18n/locales/it.json +++ b/src/i18n/locales/it.json @@ -572,12 +572,6 @@ "in_collection": "in {{name}}", "no_collection": "Aggiungi prima una richiesta in modo che il runner appartenga a una raccolta", "data_file_not_portable": "Fuori dalla cartella della raccolta: questo percorso funziona solo su questo computer", - "migration_moved_one": "{{count}} runner salvato spostato nella cartella della sua raccolta", - "migration_moved_other": "{{count}} runner salvati spostati nelle cartelle delle loro raccolte", - "migration_dropped_one": "{{count}} runner ha perso richieste di altre raccolte: {{names}}", - "migration_dropped_other": "{{count}} runner hanno perso richieste di altre raccolte: {{names}}", - "migration_skipped_one": "{{count}} runner salvato non ha richieste e non è stato spostato", - "migration_skipped_other": "{{count}} runner salvati non hanno richieste e non sono stati spostati", "tags": "Tag", "tags_include": "Includi tag", "tags_exclude": "Escludi tag", @@ -652,13 +646,6 @@ "format": "Formatta", "format_title": "Formatta JSON (Maiusc+Alt+F)" }, - "data_migration": { - "title": "Conversione dei dati", - "failed": "La conversione dei tuoi dati nel formato attuale non è riuscita al passaggio \"{{step}}\": {{message}}. Non è stato rimosso nulla e la conversione verrà ripetuta al prossimo avvio di Resonance.", - "too_old": "I tuoi dati sono stati salvati da una versione di Resonance che questa versione non può più convertire, quindi non sono stati modificati. Installa Resonance {{release}}, avvialo una volta e poi aggiorna di nuovo.", - "global_store_collections": "Le tue impostazioni contengono ancora collezioni salvate da una versione di Resonance precedente a marzo 2026. Questa versione non può più convertirle, quindi non sono state modificate. Installa Resonance {{release}}, avvialo una volta e poi aggiorna di nuovo.", - "unknown": "Non è stato possibile convertire completamente i tuoi dati nel formato attuale." - }, "environment_manager": { "create_default_name": "Nuovo ambiente", "create_title": "Crea ambiente", diff --git a/src/i18n/locales/pt-BR.json b/src/i18n/locales/pt-BR.json index a2a628a4..ebe658bb 100644 --- a/src/i18n/locales/pt-BR.json +++ b/src/i18n/locales/pt-BR.json @@ -572,12 +572,6 @@ "in_collection": "em {{name}}", "no_collection": "Adicione uma requisição primeiro para que o runner pertença a uma coleção", "data_file_not_portable": "Fora da pasta da coleção: este caminho só funciona nesta máquina", - "migration_moved_one": "{{count}} runner salvo movido para a pasta da sua coleção", - "migration_moved_other": "{{count}} runners salvos movidos para as pastas das suas coleções", - "migration_dropped_one": "{{count}} runner perdeu requisições de outras coleções: {{names}}", - "migration_dropped_other": "{{count}} runners perderam requisições de outras coleções: {{names}}", - "migration_skipped_one": "{{count}} runner salvo não tem requisições e não foi movido", - "migration_skipped_other": "{{count}} runners salvos não têm requisições e não foram movidos", "tags": "Tags", "tags_include": "Incluir tags", "tags_exclude": "Excluir tags", @@ -652,13 +646,6 @@ "format": "Formatar", "format_title": "Formatar JSON (Shift+Alt+F)" }, - "data_migration": { - "title": "Conversão de dados", - "failed": "A conversão dos seus dados para o formato atual falhou em \"{{step}}\": {{message}}. Nada foi removido, e a conversão será executada novamente na próxima vez que o Resonance iniciar.", - "too_old": "Seus dados foram salvos por uma versão do Resonance que esta versão não consegue mais converter, então não foram alterados. Instale o Resonance {{release}}, abra-o uma vez e atualize novamente.", - "global_store_collections": "Suas configurações ainda contêm coleções salvas por uma versão do Resonance anterior a março de 2026. Esta versão não consegue mais convertê-las, então não foram alteradas. Instale o Resonance {{release}}, abra-o uma vez e atualize novamente.", - "unknown": "Não foi possível converter completamente seus dados para o formato atual." - }, "environment_manager": { "create_default_name": "Novo ambiente", "create_title": "Criar ambiente", diff --git a/src/modules/CurlParser.js b/src/modules/CurlParser.js index 223e5ebe..2daf0d6e 100644 --- a/src/modules/CurlParser.js +++ b/src/modules/CurlParser.js @@ -39,11 +39,13 @@ const VALUE_OPTIONS = { '--data-binary': appendBody, '--data-urlencode': (request, value, parser) => appendBody(request, parser.encodeDataUrlencodeToken(value)), '-u': (request, value) => { - const [username, password] = value.split(':'); + const separator = value.indexOf(':'); request.auth = { type: 'basic', - username: username || '', - password: password || '' + config: { + username: separator === -1 ? value : value.slice(0, separator), + password: separator === -1 ? '' : value.slice(separator + 1) + } }; }, '-A': setHeader('User-Agent'), diff --git a/src/modules/auth/authSecrets.js b/src/modules/auth/authSecrets.js index da8626bb..a7181e31 100644 --- a/src/modules/auth/authSecrets.js +++ b/src/modules/auth/authSecrets.js @@ -14,7 +14,7 @@ export const SECRET_AUTH_FIELDS = Object.freeze(Object.fromEntries( * @param {*} value * @returns {boolean} */ -function isTemplateRef(value) { +export function isTemplateRef(value) { return typeof value === 'string' && /\{\{.*\}\}/.test(value); } diff --git a/src/modules/controllers/WorkspaceTabController.js b/src/modules/controllers/WorkspaceTabController.js index 3710d6ee..67d30264 100644 --- a/src/modules/controllers/WorkspaceTabController.js +++ b/src/modules/controllers/WorkspaceTabController.js @@ -813,9 +813,15 @@ export class WorkspaceTabController { return; } try { + if (tab.id !== await this.service.getActiveTabId()) { + await this.switchTab(tab.id); + if (tab.id !== await this.service.getActiveTabId()) { + return; + } + tab = (await this.getActiveTab()) || tab; + } const bound = tab.endpoint?.collectionId && tab.endpoint?.endpointId ? tab.endpoint : null; - const activeTabId = await this.service.getActiveTabId(); - const endpoint = bound || (tab.id === activeTabId ? getCurrentEndpoint() : null); + const endpoint = bound || getCurrentEndpoint(); if (endpoint) { await this.collectionRequests.saveAllRequestModifications(endpoint.collectionId, endpoint.endpointId); diff --git a/src/modules/ipcBridge.js b/src/modules/ipcBridge.js index b68e83e6..9a637ca0 100644 --- a/src/modules/ipcBridge.js +++ b/src/modules/ipcBridge.js @@ -14,8 +14,7 @@ if (isTauri) { api = { app: { getVersion: () => invoke('app_get_version'), - setWindowTheme: (theme) => invoke('app_set_window_theme', { theme }), - migrationStatus: () => invoke('data_migration_status') + setWindowTheme: (theme) => invoke('app_set_window_theme', { theme }) }, execution: { request: (input) => invoke('execute_request', { input }), diff --git a/src/modules/runner.feature.js b/src/modules/runner.feature.js index 973451b0..8cf812da 100644 --- a/src/modules/runner.feature.js +++ b/src/modules/runner.feature.js @@ -13,17 +13,13 @@ export const runnerFeature = { create(ctx) { const repository = RunnerRepository.shared(ctx.backendAPI); const historyRepository = RunnerHistoryRepository.shared(ctx.backendAPI); - const bookkeeping = new RunnerBookkeeping({ backendAPI: ctx.backendAPI, repository, historyRepository }); + const bookkeeping = new RunnerBookkeeping({ repository, historyRepository }); return { repository, historyRepository, bookkeeping }; }, globals: { runnerBookkeeping: 'bookkeeping' }, - init({ repository, bookkeeping }, ctx) { + init({ repository }, ctx) { const collectionController = ctx.get('collectionController'); - const collectionRepository = ctx.get('collectionRepository'); - collectionController?.onCollectionsLoaded((collections) => { - repository.invalidate(); - bookkeeping.runMigrationPass(collections, collectionRepository); - }); + collectionController?.onCollectionsLoaded(() => repository.invalidate()); window.addEventListener('focus', () => repository.invalidate()); } }; diff --git a/src/modules/services/RunnerBookkeeping.js b/src/modules/services/RunnerBookkeeping.js index 4fdeb1bf..e8a68cf4 100644 --- a/src/modules/services/RunnerBookkeeping.js +++ b/src/modules/services/RunnerBookkeeping.js @@ -1,76 +1,17 @@ /** - * @fileoverview Desktop-local runner state that outlives any runner tab: the store migration and per-collection cleanup + * @fileoverview Desktop-local runner state that outlives any runner tab: per-collection cleanup * @module services/RunnerBookkeeping */ -import { toast } from '../ui/Toast.js'; -import { translateCount } from '../utils/translate.js'; -import { migrateLegacyRunners } from '../storage/runnerMigration.js'; -import { loadEndpointRequestConfig } from './endpointRequestConfig.js'; - export class RunnerBookkeeping { /** * @param {Object} deps - * @param {Object} deps.backendAPI * @param {Object} deps.repository * @param {Object} deps.historyRepository */ - constructor({ backendAPI, repository, historyRepository }) { - this.backendAPI = backendAPI; + constructor({ repository, historyRepository }) { this.repository = repository; this.historyRepository = historyRepository; - this._pass = null; - this._announcedSkipped = false; - } - - /** - * @param {Array} collections - * @param {Object} collectionRepository - * @returns {Promise} - */ - runMigrationPass(collections, collectionRepository) { - if (this._pass) { - return this._pass; - } - this._pass = migrateLegacyRunners({ - backendAPI: this.backendAPI, - repository: this.repository, - collections, - resolveDefaults: (collectionId, endpointId) => - loadEndpointRequestConfig(collectionRepository, collectionId, endpointId) - }) - .then((summary) => { - this._announce(summary); - return summary; - }) - .catch(() => null) - .finally(() => { - this._pass = null; - }); - return this._pass; - } - - /** @param {{moved: Array, dropped: Array, skipped: Array}} summary */ - _announce(summary) { - if (summary.moved.length > 0) { - toast.success(translateCount('runner.migration_moved', summary.moved.length, { - one: 'Moved {{count}} saved runner into its collection folder', - other: 'Moved {{count}} saved runners into their collection folders' - })); - } - if (summary.dropped.length > 0) { - toast.warning(translateCount('runner.migration_dropped', summary.dropped.length, { - one: '{{count}} runner lost requests from other collections: {{names}}', - other: '{{count}} runners lost requests from other collections: {{names}}' - }, { names: summary.dropped.map(entry => entry.name).join(', ') })); - } - if (summary.skipped.length > 0 && !this._announcedSkipped) { - this._announcedSkipped = true; - toast.info(translateCount('runner.migration_skipped', summary.skipped.length, { - one: '{{count}} saved runner has no requests and was not moved', - other: '{{count}} saved runners have no requests and were not moved' - })); - } } /** diff --git a/src/modules/storage/WorkspaceTabRepository.js b/src/modules/storage/WorkspaceTabRepository.js index bcf09d38..4e313155 100644 --- a/src/modules/storage/WorkspaceTabRepository.js +++ b/src/modules/storage/WorkspaceTabRepository.js @@ -5,6 +5,55 @@ import { truncateBody } from '../utils/truncateBody.js'; import { generateId } from '../utils/ids.js'; +import { getSecretAuthFields, isTemplateRef } from '../auth/authSecrets.js'; + +/** @type {string} */ +const MQTT_PASSWORD_KEY = 'mqttPassword'; + +/** @type {string} */ +const AUTH_KEY_PREFIX = 'auth.'; + +/** + * @param {Object} request + * @returns {string[]} + */ +function authSecretFields(request) { + const config = request?.authConfig; + if (!config || typeof config !== 'object') { + return []; + } + return getSecretAuthFields(request.authType).filter((field) => { + const value = config[field]; + return typeof value === 'string' && value !== '' && !isTemplateRef(value); + }); +} + +/** + * @param {Object} tab + * @returns {Object} + */ +function tabSecrets(tab) { + const request = tab?.request; + const secrets = {}; + if (!request) { + return secrets; + } + if (typeof request.password === 'string' && request.password !== '') { + secrets[MQTT_PASSWORD_KEY] = request.password; + } + for (const field of authSecretFields(request)) { + secrets[AUTH_KEY_PREFIX + field] = request.authConfig[field]; + } + return secrets; +} + +/** + * @param {Object} tab + * @returns {boolean} + */ +function hasPlaintextSecrets(tab) { + return Object.keys(tabSecrets(tab)).length > 0; +} export class WorkspaceTabRepository { /** @@ -64,10 +113,25 @@ export class WorkspaceTabRepository { * @returns {Object} */ _withoutSecrets(tab) { - if (!tab?.request || !('password' in tab.request)) { + const request = tab?.request; + if (!request) { + return tab; + } + const authFields = authSecretFields(request); + if (!('password' in request) && authFields.length === 0) { return tab; } - return { ...tab, request: { ...tab.request, password: '' } }; + const scrubbed = { ...request }; + if ('password' in request) { + scrubbed.password = ''; + } + if (authFields.length > 0) { + scrubbed.authConfig = { ...request.authConfig }; + for (const field of authFields) { + scrubbed.authConfig[field] = ''; + } + } + return { ...tab, request: scrubbed }; } /** @@ -78,16 +142,20 @@ export class WorkspaceTabRepository { const liveIds = new Set(); for (const tab of tabs) { liveIds.add(tab.id); - const password = tab.request?.password || ''; - if (this._syncedSecrets.get(tab.id) === password) { - continue; + const next = tabSecrets(tab); + const previous = this._syncedSecrets.get(tab.id) || {}; + const scope = this._secretScope(tab.id); + for (const key of Object.keys(previous)) { + if (!(key in next)) { + await this.secretStore.delete(scope, key); + } } - if (password) { - await this.secretStore.set(this._secretScope(tab.id), 'mqttPassword', password); - } else { - await this.secretStore.delete(this._secretScope(tab.id), 'mqttPassword'); + for (const [key, value] of Object.entries(next)) { + if (previous[key] !== value) { + await this.secretStore.set(scope, key, value); + } } - this._syncedSecrets.set(tab.id, password); + this._syncedSecrets.set(tab.id, next); } for (const tabId of [...this._syncedSecrets.keys()]) { if (!liveIds.has(tabId)) { @@ -107,16 +175,31 @@ export class WorkspaceTabRepository { } const hydrated = []; for (const tab of tabs) { - if (tab?.request?.protocol !== 'mqtt') { + if (!tab?.request) { hydrated.push(tab); continue; } - const stored = await this.secretStore.get(this._secretScope(tab.id), 'mqttPassword'); - if (stored !== undefined && stored !== null) { + const stored = await this.secretStore.getScope(this._secretScope(tab.id)); + if (Object.keys(stored).length > 0) { this._syncedSecrets.set(tab.id, stored); } - const password = stored ?? tab.request.password ?? ''; - hydrated.push({ ...tab, request: { ...tab.request, password } }); + const request = { ...tab.request }; + if (MQTT_PASSWORD_KEY in stored) { + request.password = stored[MQTT_PASSWORD_KEY]; + } else if (request.protocol === 'mqtt') { + request.password = request.password ?? ''; + } + for (const [key, value] of Object.entries(stored)) { + if (!key.startsWith(AUTH_KEY_PREFIX)) { + continue; + } + const field = key.slice(AUTH_KEY_PREFIX.length); + const config = request.authConfig && typeof request.authConfig === 'object' ? request.authConfig : {}; + if (config[field] === '' || config[field] === undefined || config[field] === null) { + request.authConfig = { ...config, [field]: value }; + } + } + hydrated.push({ ...tab, request }); } return hydrated; } @@ -145,7 +228,7 @@ export class WorkspaceTabRepository { } catch { } this._tabsCache = hydrated; - if (this.secretStore && data.some(tab => tab?.request?.password)) { + if (this.secretStore && data.some(hasPlaintextSecrets)) { this._queueStoreWrite(this.STORE_KEY, hydrated).catch(() => { }); } return [...hydrated]; diff --git a/src/modules/storage/runnerMigration.js b/src/modules/storage/runnerMigration.js deleted file mode 100644 index b58e0d5c..00000000 --- a/src/modules/storage/runnerMigration.js +++ /dev/null @@ -1,144 +0,0 @@ -/** - * @fileoverview One-time, resumable move of saved runners from the desktop store into collection files - * @module storage/runnerMigration - */ - -import { OVERRIDES_VERSION, endpointDefaults, stripUnchangedOverrides } from '../utils/requestOverrides.js'; -import { toCollectionRelativePath } from '../utils/dataFilePath.js'; - -export const LEGACY_RUNNERS_KEY = 'collectionRunners'; -export const LEGACY_BACKUP_KEY = 'collectionRunnersLegacyBackup'; -export const MIGRATED_COLLECTIONS_KEY = 'runnersMigratedCollections'; - -/** - * @param {Object} runner - * @returns {string|null} - */ -export function ownerCollectionId(runner) { - return runner?.requests?.[0]?.collectionId ?? null; -} - -/** - * @param {Object} legacy - * @param {Object} collection - * @returns {{runner: Object, droppedCount: number}} - */ -export function convertLegacyRunner(legacy, collection) { - const requests = (legacy.requests || []).filter(entry => entry.collectionId === collection.id); - const options = { ...(legacy.options || {}) }; - if (options.dataFile?.path) { - const { path } = toCollectionRelativePath(options.dataFile.path, collection.storagePath); - options.dataFile = { path, name: options.dataFile.name || '' }; - } else { - options.dataFile = null; - } - return { - runner: { - id: legacy.id, - collectionId: collection.id, - name: legacy.name || '', - options, - requests: requests.map(entry => ({ - collectionId: collection.id, - endpointId: entry.endpointId, - name: entry.name || '', - method: entry.method || '', - path: entry.path || '', - postResponseScript: entry.postResponseScript || '', - overrides: entry.overrides || {} - })) - }, - droppedCount: (legacy.requests || []).length - requests.length - }; -} - -/** - * @param {Object} runner - * @param {number|null} overridesVersion - * @param {function(string, string): Promise} resolveDefaults - * @returns {Promise} - */ -async function withCurrentOverrides(runner, overridesVersion, resolveDefaults) { - if (overridesVersion === OVERRIDES_VERSION) { - return runner; - } - const requests = await Promise.all(runner.requests.map(async (entry) => { - try { - const config = await resolveDefaults(entry.collectionId, entry.endpointId); - return { ...entry, overrides: stripUnchangedOverrides(entry.overrides, endpointDefaults(config)) }; - } catch { - return entry; - } - })); - return { ...runner, requests }; -} - -/** - * @param {Object} params - * @param {Object} params.backendAPI - * @param {Object} params.repository - * @param {Array} params.collections - * @param {function(string, string): Promise} params.resolveDefaults - * @returns {Promise<{moved: Array<{id: string, name: string, collectionId: string}>, dropped: Array<{name: string, count: number}>, skipped: Array, pending: Array, finished: boolean}>} - */ -export async function migrateLegacyRunners({ backendAPI, repository, collections, resolveDefaults }) { - const summary = { moved: [], dropped: [], skipped: [], pending: [], finished: true }; - const legacy = await backendAPI.store.get(LEGACY_RUNNERS_KEY); - if (!Array.isArray(legacy) || legacy.length === 0) { - return summary; - } - if (!(await backendAPI.store.get(LEGACY_BACKUP_KEY))) { - await backendAPI.store.set(LEGACY_BACKUP_KEY, { runners: legacy, savedAt: Date.now() }); - } - - const done = new Set((await backendAPI.store.get(MIGRATED_COLLECTIONS_KEY)) || []); - const byOwner = new Map(); - for (const runner of legacy) { - const owner = ownerCollectionId(runner); - if (!owner) { - summary.skipped.push(runner.name || runner.id); - continue; - } - if (!byOwner.has(owner)) { - byOwner.set(owner, []); - } - byOwner.get(owner).push(runner); - } - - for (const [collectionId, runners] of byOwner) { - if (done.has(collectionId)) { - continue; - } - const collection = (collections || []).find(candidate => candidate.id === collectionId); - if (!collection) { - summary.pending.push(collectionId); - continue; - } - const existing = new Set((await repository.getAllByCollection(collectionId)).map(runner => runner.id)); - const lastRuns = {}; - for (const legacyRunner of runners) { - if (existing.has(legacyRunner.id)) { - continue; - } - const { runner, droppedCount } = convertLegacyRunner(legacyRunner, collection); - const converted = await withCurrentOverrides(runner, legacyRunner.overridesVersion ?? null, resolveDefaults); - await repository.put(collectionId, converted); - if (typeof legacyRunner.lastRunAt === 'number') { - lastRuns[legacyRunner.id] = legacyRunner.lastRunAt; - } - summary.moved.push({ id: legacyRunner.id, name: converted.name, collectionId }); - if (droppedCount > 0) { - summary.dropped.push({ name: converted.name, count: droppedCount }); - } - } - await repository.mergeLastRuns(lastRuns); - done.add(collectionId); - await backendAPI.store.set(MIGRATED_COLLECTIONS_KEY, [...done]); - } - - summary.finished = summary.pending.length === 0; - if (summary.finished) { - await backendAPI.store.set(LEGACY_RUNNERS_KEY, null); - } - return summary; -} diff --git a/src/modules/ui/EnvironmentManager.js b/src/modules/ui/EnvironmentManager.js index db703a34..ab9f5632 100644 --- a/src/modules/ui/EnvironmentManager.js +++ b/src/modules/ui/EnvironmentManager.js @@ -64,7 +64,11 @@ export class EnvironmentManager extends BaseModal { exportAllBtn.addEventListener('click', () => this.handleExportAll()); } - async loadEnvironments() { + /** + * @param {string|null} [preferredId] + * @returns {Promise} + */ + async loadEnvironments(preferredId = null) { try { const environments = await this.service.getAllEnvironments(); const activeEnvId = await this.service.getActiveEnvironmentId(); @@ -77,7 +81,8 @@ export class EnvironmentManager extends BaseModal { listContainer.appendChild(item); }); - const envToSelect = activeEnvId || environments[0]?.id; + const keepPreferred = preferredId && environments.some(env => env.id === preferredId); + const envToSelect = keepPreferred ? preferredId : (activeEnvId || environments[0]?.id); if (envToSelect) { await this.selectEnvironment(envToSelect); } @@ -218,7 +223,7 @@ export class EnvironmentManager extends BaseModal { if (newName && newName !== environment.name) { try { await this.service.updateEnvironment(environment.id, { name: newName }); - await this.loadEnvironments(); + await this.loadEnvironments(environment.id); } catch (error) { toast.error(error.message); nameInput.value = environment.name; @@ -246,8 +251,7 @@ export class EnvironmentManager extends BaseModal { if (colorInput) { colorInput.dataset.savedColor = color || ''; } - await this.loadEnvironments(); - await this.loadEnvironmentDetails(environment.id); + await this.loadEnvironments(environment.id); } catch (error) { toast.error(error.message); this._renderColorState(environment.color); diff --git a/src/modules/ui/MigrationNoticeDialog.js b/src/modules/ui/MigrationNoticeDialog.js deleted file mode 100644 index 0baf69dc..00000000 --- a/src/modules/ui/MigrationNoticeDialog.js +++ /dev/null @@ -1,78 +0,0 @@ -/** - * @fileoverview Modal dialog reporting data the startup migration could not convert - * @module ui/MigrationNoticeDialog - */ - -import { BaseModal } from './BaseModal.js'; -import { translate } from '../utils/translate.js'; - -const LAST_RELEASE_CONVERTING_OLD_DATA_VERSIONS = '3.5.8'; -const LAST_RELEASE_CONVERTING_GLOBAL_STORE_COLLECTIONS = '3.3.0'; - -/** - * @param {{kind: string, step?: string, message?: string, found?: number, min?: number, data?: string}} issue - * @returns {string} - */ -export function describeMigrationIssue(issue) { - switch (issue.kind) { - case 'failed': - return translate( - 'data_migration.failed', - 'Converting your data to the current format failed at "{{step}}": {{message}}. Nothing was removed, and the conversion runs again the next time Resonance starts.', - { step: issue.step, message: issue.message } - ); - case 'tooOld': - return translate( - 'data_migration.too_old', - 'Your data was saved by a Resonance release this version can no longer convert, so it was left unchanged. Install Resonance {{release}}, start it once, then update again.', - { release: LAST_RELEASE_CONVERTING_OLD_DATA_VERSIONS } - ); - case 'unsupported': - return translate( - 'data_migration.global_store_collections', - 'Collections saved by a Resonance release from before March 2026 are still in your settings. This version can no longer convert them, so they were left unchanged. Install Resonance {{release}}, start it once, then update again.', - { release: LAST_RELEASE_CONVERTING_GLOBAL_STORE_COLLECTIONS } - ); - default: - return translate('data_migration.unknown', 'Your data could not be fully converted to the current format.'); - } -} - -/** @augments */ -export class MigrationNoticeDialog extends BaseModal { - - /** - * @param {Array} issues - * @returns {void} - */ - show(issues) { - const dialog = this.mount({ - overlayClass: 'migration-notice-overlay', - dialogClass: 'migration-notice-dialog modal-dialog modal-dialog--sm', - templatePath: './src/templates/dialogs/migrationNotice.html', - templateId: 'tpl-migration-notice' - }); - - dialog.querySelector('[data-role="title"]').textContent = translate('data_migration.title', 'Data conversion'); - dialog.querySelector('[data-role="ok-text"]').textContent = translate('common.ok', 'OK'); - - const messages = dialog.querySelector('[data-role="messages"]'); - for (const issue of issues) { - const paragraph = document.createElement('p'); - paragraph.textContent = describeMigrationIssue(issue); - messages.appendChild(paragraph); - } - - const okButton = dialog.querySelector('#migration-notice-ok-btn'); - okButton.addEventListener('click', () => this.destroy()); - okButton.focus(); - } -} - -/** @returns {Promise} */ -export async function showDataMigrationIssues() { - const issues = await window.backendAPI?.app?.migrationStatus?.(); - if (Array.isArray(issues) && issues.length > 0) { - new MigrationNoticeDialog().show(issues); - } -} diff --git a/src/modules/utils/requestOverrides.js b/src/modules/utils/requestOverrides.js index ea3ad0d6..1e37a3ff 100644 --- a/src/modules/utils/requestOverrides.js +++ b/src/modules/utils/requestOverrides.js @@ -3,8 +3,6 @@ * @module utils/requestOverrides */ -export const OVERRIDES_VERSION = 2; - /** @type {ReadonlyArray} */ const ROW_FIELDS = Object.freeze(['pathParams', 'queryParams', 'headers']); diff --git a/src/renderer.js b/src/renderer.js index fa43c940..18d95f66 100644 --- a/src/renderer.js +++ b/src/renderer.js @@ -52,7 +52,6 @@ import { keyboardShortcuts } from './modules/keyboardShortcuts.js'; import { loadEditor, warmEditors } from './modules/editorLoader.js'; import { UrlAutocomplete } from './modules/ui/UrlAutocomplete.js'; import { toast } from './modules/ui/Toast.js'; -import { showDataMigrationIssues } from './modules/ui/MigrationNoticeDialog.js'; const statusDisplayAdapter = new StatusDisplayAdapter(updateStatusDisplay); @@ -623,12 +622,6 @@ document.addEventListener('DOMContentLoaded', async () => { await initMqttHandler(); await initGrpcStreamHandler(); - - try { - await showDataMigrationIssues(); - } catch (error) { - toast.error(`Migration check failed: ${error.message}`); - } }, 2000); scheduleIdleTask(() => { diff --git a/src/templates/dialogs/migrationNotice.html b/src/templates/dialogs/migrationNotice.html deleted file mode 100644 index 0de16838..00000000 --- a/src/templates/dialogs/migrationNotice.html +++ /dev/null @@ -1,9 +0,0 @@ - diff --git a/tests/CurlParser.test.js b/tests/CurlParser.test.js index 49b052c5..32de3f62 100644 --- a/tests/CurlParser.test.js +++ b/tests/CurlParser.test.js @@ -68,3 +68,23 @@ describe('CurlParser data flags', () => { expect(parsed.body).toBe('raw=1&q=a%20b'); }); }); + +describe('CurlParser basic auth', () => { + test('-u produces the {type, config} auth shape the app persists', () => { + const parsed = CurlParser.parse('curl -u alice:s3cret https://api.test'); + + expect(parsed.auth).toEqual({ type: 'basic', config: { username: 'alice', password: 's3cret' } }); + }); + + test('a colon inside the password is kept', () => { + const parsed = CurlParser.parse('curl --user user:pa:ss https://api.test'); + + expect(parsed.auth.config).toEqual({ username: 'user', password: 'pa:ss' }); + }); + + test('a user without a password gets an empty password', () => { + const parsed = CurlParser.parse('curl -u alice https://api.test'); + + expect(parsed.auth.config).toEqual({ username: 'alice', password: '' }); + }); +}); diff --git a/tests/controllers/WorkspaceTabController.saveTab.test.js b/tests/controllers/WorkspaceTabController.saveTab.test.js index 59b986ef..1ed26bef 100644 --- a/tests/controllers/WorkspaceTabController.saveTab.test.js +++ b/tests/controllers/WorkspaceTabController.saveTab.test.js @@ -41,17 +41,42 @@ describe('WorkspaceTabController.saveTab', () => { expect(controller.collectionRequests.showSaveToCollectionDialog).not.toHaveBeenCalled(); }); - test('an active unbound tab falls back to the current endpoint; a background one does not', async () => { + test('an active unbound tab falls back to the current endpoint', async () => { const controller = makeController(); setCurrentEndpoint({ collectionId: 'c1', endpointId: 'e9' }); - controller.collectionRequests.showSaveToCollectionDialog.mockResolvedValue(null); await controller.saveTab({ id: 't1', name: 'Active' }); + expect(controller.collectionRequests.saveAllRequestModifications).toHaveBeenCalledWith('c1', 'e9'); + }); + + test('a background tab is switched to before its form is read and saved', async () => { + const controller = makeController(); + const background = { id: 't2', name: 'Background', endpoint: { collectionId: 'c1', endpointId: 'e2' } }; + const order = []; + controller.switchTab = jest.fn(async (tabId) => { + order.push(`switch:${tabId}`); + controller.service.getActiveTabId.mockResolvedValue(tabId); + controller.service.getActiveTab.mockResolvedValue(background); + }); + controller.collectionRequests.saveAllRequestModifications.mockImplementation(async (c, e) => { + order.push(`save:${e}`); + }); + + await controller.saveTab(background); - await controller.saveTab({ id: 't2', name: 'Background' }); - expect(controller.collectionRequests.saveAllRequestModifications).toHaveBeenCalledTimes(1); - expect(controller.collectionRequests.showSaveToCollectionDialog).toHaveBeenCalledTimes(1); + expect(order).toEqual(['switch:t2', 'save:e2']); + expect(controller.markCurrentTabUnmodified).toHaveBeenCalledTimes(1); + }); + + test('a superseded switch saves nothing', async () => { + const controller = makeController(); + controller.switchTab = jest.fn().mockResolvedValue(undefined); + + await controller.saveTab({ id: 't2', name: 'Background', endpoint: { collectionId: 'c1', endpointId: 'e2' } }); + + expect(controller.collectionRequests.saveAllRequestModifications).not.toHaveBeenCalled(); + expect(controller.collectionRequests.showSaveToCollectionDialog).not.toHaveBeenCalled(); }); test('a runner tab and a controller without a collection port are ignored', async () => { diff --git a/tests/features/runner.feature.test.js b/tests/features/runner.feature.test.js index b69bb51f..af0f25df 100644 --- a/tests/features/runner.feature.test.js +++ b/tests/features/runner.feature.test.js @@ -7,10 +7,9 @@ function makeContext() { const shared = new Map(); const listeners = []; shared.set('collectionController', { onCollectionsLoaded: jest.fn((listener) => { listeners.push(listener); return () => {}; }) }); - shared.set('collectionRepository', { marker: 'collections' }); return { ctx: { - backendAPI: { collections: { list: jest.fn().mockResolvedValue([]), runners: {} }, store: { get: jest.fn().mockResolvedValue(null), set: jest.fn() } }, + backendAPI: { collections: { list: jest.fn().mockResolvedValue([]), runners: {} } }, _shared: shared, provide(name, value) { shared.set(name, value); return value; }, get(name) { return shared.get(name); } @@ -30,17 +29,15 @@ describe('runner feature', () => { expect(runnerFeature.globals).toEqual({ runnerBookkeeping: 'bookkeeping' }); }); - test('init invalidates the cache and runs a migration pass whenever collections load, and on focus', () => { + test('init invalidates the cache whenever collections load, and on focus', () => { const { ctx, listeners } = makeContext(); const instances = runnerFeature.create(ctx); instances.repository.invalidate = jest.fn(); - instances.bookkeeping.runMigrationPass = jest.fn().mockResolvedValue(null); runnerFeature.init(instances, ctx); listeners[0]([{ id: 'c1' }]); window.dispatchEvent(new window.Event('focus')); expect(instances.repository.invalidate).toHaveBeenCalledTimes(2); - expect(instances.bookkeeping.runMigrationPass).toHaveBeenCalledWith([{ id: 'c1' }], ctx.get('collectionRepository')); }); }); diff --git a/tests/services/RunnerBookkeeping.test.js b/tests/services/RunnerBookkeeping.test.js index c82aa56f..45414551 100644 --- a/tests/services/RunnerBookkeeping.test.js +++ b/tests/services/RunnerBookkeeping.test.js @@ -1,33 +1,16 @@ import { RunnerBookkeeping } from '../../src/modules/services/RunnerBookkeeping.js'; -import { LEGACY_RUNNERS_KEY } from '../../src/modules/storage/runnerMigration.js'; -import { toast } from '../../src/modules/ui/Toast.js'; -jest.mock('../../src/modules/ui/Toast.js', () => ({ - toast: { success: jest.fn(), warning: jest.fn(), info: jest.fn(), error: jest.fn() } -})); - -function setup(legacy = null) { - const store = { [LEGACY_RUNNERS_KEY]: legacy }; - const backendAPI = { - store: { - get: jest.fn(async (key) => store[key] ?? null), - set: jest.fn(async (key, value) => { store[key] = value; }) - } - }; +function setup() { const repository = { getAllByCollection: jest.fn(async () => [{ id: 'r1' }, { id: 'r2' }]), - put: jest.fn(async (_cid, runner) => runner), - mergeLastRuns: jest.fn(async () => {}), removeLastRuns: jest.fn(async () => {}), invalidate: jest.fn() }; const historyRepository = { removeMany: jest.fn(async () => {}) }; - const bookkeeping = new RunnerBookkeeping({ backendAPI, repository, historyRepository }); - return { bookkeeping, repository, historyRepository, backendAPI, store }; + const bookkeeping = new RunnerBookkeeping({ repository, historyRepository }); + return { bookkeeping, repository, historyRepository }; } -beforeEach(() => jest.clearAllMocks()); - test('forgetCollection clears last runs and history for that collection and drops its cache', async () => { const { bookkeeping, repository, historyRepository } = setup(); @@ -39,32 +22,13 @@ test('forgetCollection clears last runs and history for that collection and drop expect(repository.invalidate).toHaveBeenCalledWith('c1'); }); -test('a migration pass announces moved runners and dropped entries, and runs once at a time', async () => { - const legacy = [ - { id: 'r9', name: 'Mixed', overridesVersion: 2, requests: [{ collectionId: 'c1', endpointId: 'a' }, { collectionId: 'c2', endpointId: 'b' }], options: {} } - ]; - const { bookkeeping, repository } = setup(legacy); +test('a collection without runners only drops the cache', async () => { + const { bookkeeping, repository, historyRepository } = setup(); repository.getAllByCollection.mockResolvedValue([]); - const collections = [{ id: 'c1', name: 'One' }]; - const collectionRepository = {}; - - const [first, second] = await Promise.all([ - bookkeeping.runMigrationPass(collections, collectionRepository), - bookkeeping.runMigrationPass(collections, collectionRepository) - ]); - - expect(first).toBe(second); - expect(repository.put).toHaveBeenCalledTimes(1); - expect(toast.success).toHaveBeenCalledWith('Moved 1 saved runner into its collection folder'); - expect(toast.warning).toHaveBeenCalledWith('1 runner lost requests from other collections: Mixed'); -}); - -test('a quiet pass shows no toast', async () => { - const { bookkeeping } = setup(null); - await bookkeeping.runMigrationPass([], {}); + await bookkeeping.forgetCollection('c2'); - expect(toast.success).not.toHaveBeenCalled(); - expect(toast.warning).not.toHaveBeenCalled(); - expect(toast.info).not.toHaveBeenCalled(); + expect(repository.removeLastRuns).not.toHaveBeenCalled(); + expect(historyRepository.removeMany).not.toHaveBeenCalled(); + expect(repository.invalidate).toHaveBeenCalledWith('c2'); }); diff --git a/tests/storage/mqttPasswordSecrets.test.js b/tests/storage/mqttPasswordSecrets.test.js index b9857a3c..2a56968a 100644 --- a/tests/storage/mqttPasswordSecrets.test.js +++ b/tests/storage/mqttPasswordSecrets.test.js @@ -128,3 +128,68 @@ describe('copying and deleting request data', () => { expect(await repo.secretStore.get('auth:c1:__folder__:f1', 'token')).toBeUndefined(); }); }); + +const httpTab = (id, authType, authConfig) => ({ + id, + name: 'HTTP', + request: { protocol: 'http', url: 'https://x.test', authType, authConfig }, + response: {}, + endpoint: null +}); + +describe('workspace tabs keep auth secrets out of the plaintext store', () => { + test('a bearer token is persisted only in the secret store and hydrates back', async () => { + const api = makeBackend(); + const repo = new WorkspaceTabRepository(api, new SecretStore(api)); + + await repo.saveTabs([httpTab('t1', 'bearer', { token: 'tok-1' })]); + + expect(api.__store['workspace-tabs'][0].request.authConfig.token).toBe(''); + expect(api.__chain.get('tab:t1|auth.token')).toBe('tok-1'); + + const reloaded = new WorkspaceTabRepository(api, new SecretStore(api)); + const tabs = await reloaded.getTabs(); + expect(tabs[0].request.authConfig.token).toBe('tok-1'); + }); + + test('only secret fields are scrubbed and template references stay in place', async () => { + const api = makeBackend(); + const repo = new WorkspaceTabRepository(api, new SecretStore(api)); + + await repo.saveTabs([ + httpTab('t1', 'basic', { username: 'alice', password: 'pw' }), + httpTab('t2', 'bearer', { token: '{{token}}' }) + ]); + + const [basic, bearer] = api.__store['workspace-tabs']; + expect(basic.request.authConfig).toEqual({ username: 'alice', password: '' }); + expect(bearer.request.authConfig).toEqual({ token: '{{token}}' }); + expect(api.__chain.has('tab:t2|auth.token')).toBe(false); + }); + + test('legacy plaintext auth secrets are migrated and scrubbed on load', async () => { + const api = makeBackend(); + api.__store['workspace-tabs'] = [httpTab('t1', 'api-key', { keyName: 'X-Key', keyValue: 'k-1' })]; + const repo = new WorkspaceTabRepository(api, new SecretStore(api)); + + const tabs = await repo.getTabs(); + await repo._writeChain; + + expect(tabs[0].request.authConfig.keyValue).toBe('k-1'); + expect(api.__store['workspace-tabs'][0].request.authConfig.keyValue).toBe(''); + expect(api.__chain.get('tab:t1|auth.keyValue')).toBe('k-1'); + }); + + test('clearing a secret removes it and closing the tab removes the rest', async () => { + const api = makeBackend(); + const repo = new WorkspaceTabRepository(api, new SecretStore(api)); + await repo.saveTabs([httpTab('t1', 'oauth2', { clientSecret: 's', token: 't' })]); + + await repo.saveTabs([httpTab('t1', 'oauth2', { clientSecret: '', token: 't' })]); + expect(api.__chain.has('tab:t1|auth.clientSecret')).toBe(false); + expect(api.__chain.get('tab:t1|auth.token')).toBe('t'); + + await repo.deleteTab('t1'); + expect(api.__chain.has('tab:t1|auth.token')).toBe(false); + }); +}); diff --git a/tests/storage/runnerMigration.test.js b/tests/storage/runnerMigration.test.js deleted file mode 100644 index b791145a..00000000 --- a/tests/storage/runnerMigration.test.js +++ /dev/null @@ -1,155 +0,0 @@ -import { - migrateLegacyRunners, - convertLegacyRunner, - ownerCollectionId, - LEGACY_RUNNERS_KEY, - LEGACY_BACKUP_KEY, - MIGRATED_COLLECTIONS_KEY -} from '../../src/modules/storage/runnerMigration.js'; - -function legacyRunner(id, requests, extra = {}) { - return { id, name: id.toUpperCase(), overridesVersion: 2, options: { stopOnError: true, delayMs: 0 }, requests, lastRunAt: null, ...extra }; -} - -function entry(collectionId, endpointId, overrides = {}) { - return { collectionId, endpointId, name: endpointId, method: 'GET', path: `/${endpointId}`, postResponseScript: '', overrides }; -} - -function setup(legacy, { collections = [{ id: 'c1', name: 'One', storagePath: '/work/one' }, { id: 'c2', name: 'Two', storagePath: '/work/two' }], existing = {} } = {}) { - const store = { [LEGACY_RUNNERS_KEY]: legacy }; - const backendAPI = { - store: { - get: jest.fn(async (key) => store[key] ?? null), - set: jest.fn(async (key, value) => { store[key] = value; }) - } - }; - const saved = []; - const lastRuns = []; - const repository = { - getAllByCollection: jest.fn(async (collectionId) => (existing[collectionId] || []).map(id => ({ id }))), - put: jest.fn(async (collectionId, runner) => { saved.push({ collectionId, runner }); return runner; }), - mergeLastRuns: jest.fn(async (entries) => { lastRuns.push(entries); }) - }; - const resolveDefaults = jest.fn(async () => ({ pathParams: [], queryParams: [{ key: 'page', value: '1', enabled: true }], headers: [], body: '' })); - const run = () => migrateLegacyRunners({ backendAPI, repository, collections, resolveDefaults }); - return { store, backendAPI, repository, saved, lastRuns, resolveDefaults, run }; -} - -describe('runnerMigration', () => { - test('moves a legacy runner under the collection of its first request with its id preserved', async () => { - const { run, saved, store } = setup([legacyRunner('r1', [entry('c1', 'a'), entry('c1', 'b')])]); - - const summary = await run(); - - expect(saved).toHaveLength(1); - expect(saved[0].collectionId).toBe('c1'); - expect(saved[0].runner).toMatchObject({ id: 'r1', collectionId: 'c1', name: 'R1' }); - expect(saved[0].runner.requests.map(r => r.endpointId)).toEqual(['a', 'b']); - expect(summary).toMatchObject({ moved: [{ id: 'r1', name: 'R1', collectionId: 'c1' }], dropped: [], skipped: [], pending: [], finished: true }); - expect(store[LEGACY_RUNNERS_KEY]).toBeNull(); - expect(store[MIGRATED_COLLECTIONS_KEY]).toEqual(['c1']); - }); - - test('drops entries from other collections and reports the runner name and count', async () => { - const { run, saved } = setup([legacyRunner('mixed', [entry('c1', 'a'), entry('c2', 'x'), entry('c1', 'b'), entry('c2', 'y')])]); - - const summary = await run(); - - expect(saved[0].runner.requests.map(r => r.endpointId)).toEqual(['a', 'b']); - expect(summary.dropped).toEqual([{ name: 'MIXED', count: 2 }]); - }); - - test('makes the data file path relative when it is inside the collection folder', async () => { - const { run, saved } = setup([ - legacyRunner('inside', [entry('c1', 'a')], { options: { dataFile: { path: '/work/one/data/users.csv', name: 'users.csv', rowCount: 3 } } }), - legacyRunner('outside', [entry('c2', 'x')], { options: { dataFile: { path: '/elsewhere/users.csv', name: 'users.csv' } } }) - ]); - - await run(); - - expect(saved.find(s => s.runner.id === 'inside').runner.options.dataFile).toEqual({ path: 'data/users.csv', name: 'users.csv' }); - expect(saved.find(s => s.runner.id === 'outside').runner.options.dataFile).toEqual({ path: '/elsewhere/users.csv', name: 'users.csv' }); - }); - - test('copies lastRunAt into the local last-run store per collection', async () => { - const { run, lastRuns } = setup([ - legacyRunner('r1', [entry('c1', 'a')], { lastRunAt: 1234 }), - legacyRunner('r2', [entry('c1', 'b')]) - ]); - - await run(); - - expect(lastRuns).toEqual([{ r1: 1234 }]); - }); - - test('migrates v1 overrides so the file only holds real edits', async () => { - const { run, saved, resolveDefaults } = setup([ - legacyRunner('old', [entry('c1', 'a', { queryParams: [{ key: 'page', value: '1' }], headers: [{ key: 'X-A', value: 'edited' }] })], { overridesVersion: null }) - ]); - - await run(); - - expect(resolveDefaults).toHaveBeenCalledWith('c1', 'a'); - expect(saved[0].runner.requests[0].overrides).toEqual({ headers: [{ key: 'X-A', value: 'edited' }] }); - }); - - test('a second pass is idempotent: existing ids are skipped and nothing is saved twice', async () => { - const { run, saved, store } = setup([legacyRunner('r1', [entry('c1', 'a')])], { existing: { c1: ['r1'] } }); - - const summary = await run(); - - expect(saved).toHaveLength(0); - expect(summary.moved).toEqual([]); - expect(store[MIGRATED_COLLECTIONS_KEY]).toEqual(['c1']); - expect(store[LEGACY_RUNNERS_KEY]).toBeNull(); - }); - - test('a runner whose collection is not loaded stays pending and the legacy key survives', async () => { - const { run, saved, store } = setup([legacyRunner('r1', [entry('c1', 'a')]), legacyRunner('r9', [entry('gone', 'z')])]); - - const summary = await run(); - - expect(saved.map(s => s.runner.id)).toEqual(['r1']); - expect(summary.pending).toEqual(['gone']); - expect(summary.finished).toBe(false); - expect(store[LEGACY_RUNNERS_KEY]).toHaveLength(2); - expect(store[MIGRATED_COLLECTIONS_KEY]).toEqual(['c1']); - - const again = await run(); - expect(saved).toHaveLength(1); - expect(again.moved).toEqual([]); - expect(again.pending).toEqual(['gone']); - }); - - test('writes the backup once before the first move and never again', async () => { - const { run, store } = setup([legacyRunner('r1', [entry('c1', 'a')]), legacyRunner('r9', [entry('gone', 'z')])]); - - await run(); - const backup = store[LEGACY_BACKUP_KEY]; - expect(backup.runners).toHaveLength(2); - await run(); - - expect(store[LEGACY_BACKUP_KEY]).toBe(backup); - }); - - test('runners with no requests are skipped and reported, and an empty store is a no-op', async () => { - const { run, saved } = setup([legacyRunner('empty', [])]); - const result = await run(); - expect(saved).toHaveLength(0); - expect(result.skipped).toEqual(['EMPTY']); - expect(result.finished).toBe(true); - - const empty = setup(null); - expect(await empty.run()).toMatchObject({ moved: [], finished: true }); - expect(empty.backendAPI.store.set).not.toHaveBeenCalled(); - }); - - test('convertLegacyRunner and ownerCollectionId are pure helpers', () => { - expect(ownerCollectionId(legacyRunner('r', [entry('c2', 'x')]))).toBe('c2'); - expect(ownerCollectionId(legacyRunner('r', []))).toBeNull(); - const { runner, droppedCount } = convertLegacyRunner(legacyRunner('r', [entry('c1', 'a'), entry('c3', 'q')]), { id: 'c1' }); - expect(droppedCount).toBe(1); - expect(runner.requests).toEqual([{ collectionId: 'c1', endpointId: 'a', name: 'a', method: 'GET', path: '/a', postResponseScript: '', overrides: {} }]); - expect(runner.options.dataFile).toBeNull(); - }); -}); diff --git a/tests/ui/EnvironmentManager.test.js b/tests/ui/EnvironmentManager.test.js index 683bfaea..0b0ab743 100644 --- a/tests/ui/EnvironmentManager.test.js +++ b/tests/ui/EnvironmentManager.test.js @@ -191,6 +191,41 @@ describe('EnvironmentManager', () => { manager.close(); }); + test('changing the colour of a non-active environment keeps it selected for edits', async () => { + const { manager } = await openManager(service); + await manager.selectEnvironment('prod'); + const colorInput = document.querySelector('#env-color-input'); + + colorInput.value = '#00ff00'; + colorInput.dispatchEvent(new Event('change')); + for (let i = 0; i < 6; i++) { + await flush(); + } + + expect(service.updateEnvironment).toHaveBeenCalledWith('prod', { color: '#00FF00' }); + expect(manager.currentEnvironmentId).toBe('prod'); + expect(document.querySelector('.env-list-item.is-selected').dataset.envId).toBe('prod'); + + manager.close(); + }); + + test('renaming a non-active environment keeps it selected', async () => { + const { manager } = await openManager(service); + await manager.selectEnvironment('prod'); + const nameInput = document.querySelector('#env-name-input'); + + nameInput.value = 'Production'; + nameInput.dispatchEvent(new Event('blur')); + for (let i = 0; i < 6; i++) { + await flush(); + } + + expect(service.updateEnvironment).toHaveBeenCalledWith('prod', { name: 'Production' }); + expect(manager.currentEnvironmentId).toBe('prod'); + + manager.close(); + }); + test('clearing the colour saves null and resets the preview', async () => { const { manager } = await openManager(service); diff --git a/tests/ui/MigrationNoticeDialog.test.js b/tests/ui/MigrationNoticeDialog.test.js deleted file mode 100644 index bcb89313..00000000 --- a/tests/ui/MigrationNoticeDialog.test.js +++ /dev/null @@ -1,61 +0,0 @@ -/* global document, DOMParser, window */ -import fs from 'fs'; -import path from 'path'; -import { describeMigrationIssue, showDataMigrationIssues } from '../../src/modules/ui/MigrationNoticeDialog.js'; -import { templateLoader } from '../../src/modules/templateLoader.js'; - -const TEMPLATE_PATH = './src/templates/dialogs/migrationNotice.html'; - -describe('MigrationNoticeDialog', () => { - beforeEach(() => { - const html = fs.readFileSync(path.join(process.cwd(), 'src/templates/dialogs/migrationNotice.html'), 'utf8'); - templateLoader.cache.set(TEMPLATE_PATH, new DOMParser().parseFromString(html, 'text/html')); - document.body.innerHTML = ''; - }); - - afterEach(() => { - document.body.innerHTML = ''; - delete window.backendAPI; - }); - - test('a failed step names the step and says it is retried', () => { - const text = describeMigrationIssue({ kind: 'failed', step: 'store split', message: 'disk full' }); - expect(text).toContain('"store split": disk full'); - expect(text).toContain('runs again the next time'); - }); - - test('too-old data points at the last release that still converts every data version', () => { - expect(describeMigrationIssue({ kind: 'tooOld', found: 0, min: 2 })).toContain('Install Resonance 3.5.8'); - }); - - test('global-store collections point at the last release that still read them', () => { - const text = describeMigrationIssue({ kind: 'unsupported', data: 'globalStoreCollections' }); - expect(text).toContain('before March 2026'); - expect(text).toContain('Install Resonance 3.3.0'); - }); - - test('no dialog opens when the migration reported nothing', async () => { - window.backendAPI = { app: { migrationStatus: async () => [] } }; - - await showDataMigrationIssues(); - - expect(document.querySelector('.migration-notice-dialog')).toBeNull(); - }); - - test('one paragraph per issue, dismissed by OK', async () => { - window.backendAPI = { - app: { - migrationStatus: async () => [ - { kind: 'failed', step: 'a', message: 'b' }, - { kind: 'unsupported', data: 'globalStoreCollections' } - ] - } - }; - - await showDataMigrationIssues(); - - expect(document.querySelectorAll('.migration-notice-dialog [data-role="messages"] p')).toHaveLength(2); - document.getElementById('migration-notice-ok-btn').click(); - expect(document.querySelector('.migration-notice-dialog')).toBeNull(); - }); -});