@@ -1259,6 +1259,136 @@ Regression: when called from input buffer, state is nil → \"unknown\"."
12591259 (should completing-read-called)
12601260 (should (equal captured-initial " opus" ))))
12611261
1262+ (ert-deftest pi-coding-agent-test-select-thinking-refreshes-state-from-server ()
1263+ " Thinking selector refreshes state so server clamping is visible in the UI."
1264+ (let (captured-prompt captured-collection rpc-commands last-message)
1265+ (with-temp-buffer
1266+ (pi-coding-agent-chat-mode)
1267+ (setq pi-coding-agent--process :fake-proc
1268+ pi-coding-agent--state '(:thinking-level " low" ))
1269+ (cl-letf (((symbol-function 'completing-read )
1270+ (lambda (prompt collection &rest _ )
1271+ (setq captured-prompt prompt
1272+ captured-collection collection)
1273+ " high" ))
1274+ ((symbol-function 'pi-coding-agent--rpc-async )
1275+ (lambda (_proc cmd callback )
1276+ (push cmd rpc-commands)
1277+ (pcase (plist-get cmd :type )
1278+ (" set_thinking_level"
1279+ (funcall callback '(:success t :command " set_thinking_level" )))
1280+ (" get_state"
1281+ (funcall callback
1282+ '(:success t
1283+ :data (:thinkingLevel " medium"
1284+ :isStreaming nil
1285+ :isCompacting nil )))))))
1286+ ((symbol-function 'message )
1287+ (lambda (fmt &rest args )
1288+ (setq last-message (apply #'format fmt args)))))
1289+ (pi-coding-agent-select-thinking)
1290+ (should (equal (plist-get pi-coding-agent--state :thinking-level ) " medium" ))))
1291+ (should (equal captured-prompt " Thinking level (current: low): " ))
1292+ (should (equal captured-collection
1293+ '(" off" " minimal" " low" " medium" " high" " xhigh" )))
1294+ (let ((commands (nreverse rpc-commands)))
1295+ (should (equal (mapcar (lambda (cmd ) (plist-get cmd :type )) commands)
1296+ '(" set_thinking_level" " get_state" )))
1297+ (should (equal (car commands)
1298+ '(:type " set_thinking_level" :level " high" )))
1299+ (should (equal (cadr commands) '(:type " get_state" ))))
1300+ (should (equal last-message " Pi: Thinking level: medium" ))))
1301+
1302+ (ert-deftest pi-coding-agent-test-select-thinking-noop-when-unchanged ()
1303+ " Thinking selector does not send RPC when the user picks the current level."
1304+ (let (rpc-called)
1305+ (with-temp-buffer
1306+ (pi-coding-agent-chat-mode)
1307+ (setq pi-coding-agent--process :fake-proc
1308+ pi-coding-agent--state '(:thinking-level " medium" ))
1309+ (cl-letf (((symbol-function 'completing-read )
1310+ (lambda (&rest _ ) " medium" ))
1311+ ((symbol-function 'pi-coding-agent--rpc-async )
1312+ (lambda (&rest _ )
1313+ (setq rpc-called t ))))
1314+ (pi-coding-agent-select-thinking)))
1315+ (should-not rpc-called)))
1316+
1317+ (ert-deftest pi-coding-agent-test-select-thinking-errors-without-process ()
1318+ " Thinking selector should fail loudly when no pi process is running."
1319+ (with-temp-buffer
1320+ (pi-coding-agent-chat-mode)
1321+ (should-error (pi-coding-agent-select-thinking) :type 'user-error )))
1322+
1323+ (ert-deftest pi-coding-agent-test-select-thinking-shows-rpc-error ()
1324+ " Thinking selector reports set_thinking_level RPC failures."
1325+ (let (rpc-commands shown-message)
1326+ (with-temp-buffer
1327+ (pi-coding-agent-chat-mode)
1328+ (setq pi-coding-agent--process :fake-proc
1329+ pi-coding-agent--state '(:thinking-level " low" ))
1330+ (cl-letf (((symbol-function 'completing-read )
1331+ (lambda (&rest _ ) " high" ))
1332+ ((symbol-function 'pi-coding-agent--rpc-async )
1333+ (lambda (_proc cmd callback )
1334+ (push cmd rpc-commands)
1335+ (funcall callback '(:success nil :error " unsupported" ))))
1336+ ((symbol-function 'message )
1337+ (lambda (fmt &rest args )
1338+ (setq shown-message (apply #'format fmt args)))))
1339+ (pi-coding-agent-select-thinking)
1340+ (should (equal (plist-get pi-coding-agent--state :thinking-level ) " low" ))))
1341+ (should (equal rpc-commands
1342+ '((:type " set_thinking_level" :level " high" ))))
1343+ (should (equal shown-message
1344+ " Pi: Failed to set thinking level: unsupported" ))))
1345+
1346+ (ert-deftest pi-coding-agent-test-select-thinking-warns-when-state-refresh-fails ()
1347+ " Thinking selector warns instead of guessing when state refresh fails."
1348+ (let (shown-message)
1349+ (with-temp-buffer
1350+ (pi-coding-agent-chat-mode)
1351+ (setq pi-coding-agent--process :fake-proc
1352+ pi-coding-agent--state '(:thinking-level " low" ))
1353+ (cl-letf (((symbol-function 'completing-read )
1354+ (lambda (&rest _ ) " high" ))
1355+ ((symbol-function 'pi-coding-agent--rpc-async )
1356+ (lambda (_proc cmd callback )
1357+ (pcase (plist-get cmd :type )
1358+ (" set_thinking_level"
1359+ (funcall callback '(:success t :command " set_thinking_level" )))
1360+ (" get_state"
1361+ (funcall callback '(:success nil :error " state unavailable" ))))))
1362+ ((symbol-function 'message )
1363+ (lambda (fmt &rest args )
1364+ (setq shown-message (apply #'format fmt args)))))
1365+ (pi-coding-agent-select-thinking)
1366+ (should (equal (plist-get pi-coding-agent--state :thinking-level ) " low" ))))
1367+ (should (equal shown-message
1368+ " Pi: Thinking level updated, but failed to refresh state: state unavailable" ))))
1369+
1370+ (ert-deftest pi-coding-agent-test-thinking-selector-uses-t-key-leaving-T-for-templates ()
1371+ " Main menu binds `t' to thinking selection without taking Templates `T' ."
1372+ (let ((pi-coding-agent--commands
1373+ '((:name " review" :description " Code review" :source " prompt" ))))
1374+ (unwind-protect
1375+ (progn
1376+ (pi-coding-agent--rebuild-commands-menu)
1377+ (transient-setup 'pi-coding-agent-menu )
1378+ (let ((thinking-suffix
1379+ (cl-find-if (lambda (obj )
1380+ (equal (oref obj key) " t" ))
1381+ transient--suffixes))
1382+ (templates-suffix
1383+ (cl-find-if (lambda (obj )
1384+ (equal (oref obj key) " T" ))
1385+ transient--suffixes)))
1386+ (should thinking-suffix)
1387+ (should (eq (oref thinking-suffix command)
1388+ 'pi-coding-agent-select-thinking ))
1389+ (should templates-suffix)))
1390+ (ignore-errors (transient-remove-suffix 'pi-coding-agent-menu '(4 ))))))
1391+
12621392; ;; sourceInfo normalization
12631393
12641394(ert-deftest pi-coding-agent-test-normalize-command-extracts-source-info ()
0 commit comments