joyride

pez 2025-11-01T20:14:01.895729Z

This makes the test runner hang on the test. I don’t understand why:

(deftest-async namespaced-keywords-in-messages
  (testing "post-message!+ handles namespaced keywords"
    (p/let
      [received-message (p/deferred)
       message-handler (fn [msg] (p/resolve! received-message (js->clj msg :keywordize-keys true)))
       message {:msg/type "test",
                :foo 42,
                :bar "42",
                :msg/data {:user/name "Alice", :user/id 123, :nested {:action/name "click"}}}
       ;; This flare echoes messages back
       _ (flare/flare!+
           {:key :test/namespaced-keywords,
            :html
              [:div [:h1 "Test"]
               [:script
                "const vscode = acquireVsCodeApi();
                 window.addEventListener('message', event => {
                   vscode.postMessage(event.data);
                 });"]],
            :title "Test",
            :message-handler message-handler})
       _ (flare/post-message!+ :test/namespaced-keywords message)]
      (is (= message received-message) "The message survives the roundtrip intact")
      (flare/close! :test/namespaced-keywords))))

pez 2025-11-01T20:15:47.637999Z

So I have to do this boring thing instead…

(deftest-async namespaced-keywords-in-messages
  (testing "post-message!+ handles namespaced keywords"
    (p/let [received-message (atom nil)
            message-handler (fn [msg]
                              (reset! received-message (js->clj msg :keywordize-keys true)))
            message {:msg/type "test"
                     :foo 42
                     :bar "42"
                     :msg/data {:user/name "Alice"
                                :user/id 123
                                :nested {:action/name "click"}}}
            ;; This flare echoes messages back
            _ (flare/flare!+ {:key :test-namespaced-keywords
                              :html [:div
                                     [:h1 "Test"]
                                     [:script
                                      "const vscode = acquireVsCodeApi();
                                       window.addEventListener('message', event => {
                                        vscode.postMessage(event.data);
                                       });"]]
                              :title "Test"
                              :message-handler message-handler})
            ;; Wait a bit for the flare to be created
            _ (p/delay 200)
            _ (flare/post-message!+ :test-namespaced-keywords
                                    message)
            ;; Wait for the message to be received (next tick?)
            _ (p/delay 0)]
      (is (= message @received-message)
          "The message survives the roundtrip intact")
      (flare/close! :test-namespaced-keywords))))

pez 2025-11-01T20:23:23.974399Z

And 200ms wasn’t even enough on CI…

pez 2025-11-01T20:33:43.691989Z

OK. I see why things hang now…

pez 2025-11-01T20:43:32.177699Z

I still need to do it like this.

(deftest-async namespaced-keywords-in-messages
  (testing "post-message!+ handles namespaced keywords"
    (let [received-message (p/deferred)]
      (p/let
       [message-handler (fn [msg]
                          (p/resolve! received-message (js->clj msg :keywordize-keys true)))
        message {:msg/type "test",
                 :foo 42,
                 :bar "42",
                 :msg/data {:user/name "Alice", :user/id 123, :nested {:action/name "click"}}}
        ;; This flare echoes messages back
        _ (flare/flare!+
           {:key :test/namespaced-keywords,
            :html
            [:div [:h1 "Test"]
             [:script
              "const vscode = acquireVsCodeApi();
                 window.addEventListener('message', event => {
                   vscode.postMessage(event.data);
                 });"]],
            :title "Test",
            :message-handler message-handler})
        _ (p/delay 500)
        _ (flare/post-message!+ :test/namespaced-keywords message)
        _ (p/delay 0)
        _ (flare/close! :test/namespaced-keywords)
        resolved-message received-message]
        (is (= message resolved-message)
            "The message survives the roundtrip intact")))))
I guess the root problem is that I haven’t figured out how to resolve the flare creation promise reliably.

pez 2025-11-01T21:27:51.216389Z

Afaiu VS Code doesn’t provide a reliable way to defer resolving the flare creation promise when things are ready for message posting, But at least I can get stable testing doing this:

(deftest-async namespaced-keywords-in-messages
  (testing "post-message!+ handles namespaced keywords"
    (let [ready? (p/deferred)
          received-message (p/deferred)]
      (p/let [message-handler (fn [msg]
                                (if (= true msg)
                                  (p/resolve! ready? true)
                                  (p/resolve! received-message (js->clj msg :keywordize-keys true))))
              message {:msg/type "test",
                       :foo 42,
                       :bar "42",
                       :msg/data {:user/name "Alice", :user/id 123, :nested {:action/name "click"}}}
              ;; This flare echoes messages back
              _ (flare/flare!+
                 {:key :test/namespaced-keywords,
                  :html
                  [:div [:h1 "Test"]
                   [:script
                    "const vscode = acquireVsCodeApi();
                     vscode.postMessage(true)
                     window.addEventListener('message', event => {
                       vscode.postMessage(event.data);
                     });"]],
                  :title "Test",
                  :message-handler message-handler})
              _ ready?
              ;; Next tick we're ready for realz
              _ (p/delay 0)
              _ (flare/post-message!+ :test/namespaced-keywords message)
              ;; We need to wait a tick before closing the flare
              _ (p/delay 0)
              _ (flare/close! :test/namespaced-keywords)
              resolved-message received-message]
        (is (= message resolved-message)
            "The message survives the roundtrip intact")))))