Skip to content

Commit 361e79b

Browse files
authored
fix: report chat list message counts (#2185)
1 parent d2de5df commit 361e79b

2 files changed

Lines changed: 98 additions & 2 deletions

File tree

inc/Cli/Commands/ChatCommand.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,12 @@ static function ( $session ) {
132132
// Flatten for display.
133133
$display_items = array();
134134
foreach ( $sessions as $session ) {
135-
$metadata = $session['metadata'] ?? array();
135+
$metadata = is_array( $session['metadata'] ?? null ) ? $session['metadata'] : array();
136136
$display_items[] = array(
137137
'session_id' => $session['session_id'],
138138
'title' => $session['title'] ?? '(untitled)',
139139
'mode' => $session['mode'] ?? 'chat',
140-
'message_count' => $metadata['message_count'] ?? 0,
140+
'message_count' => self::get_session_message_count( $session ),
141141
'created_at' => $metadata['started_at'] ?? $session['created_at'] ?? '-',
142142
);
143143
}
@@ -149,6 +149,26 @@ static function ( $session ) {
149149
$this->output_pagination( $offset, count( $sessions ), $total, $format, 'sessions' );
150150
}
151151

152+
/**
153+
* Resolve the message count for a session summary row.
154+
*
155+
* @param array $session Session summary or full session payload.
156+
* @return int
157+
*/
158+
private static function get_session_message_count( array $session ): int {
159+
if ( isset( $session['message_count'] ) && is_numeric( $session['message_count'] ) ) {
160+
return max( 0, (int) $session['message_count'] );
161+
}
162+
163+
$metadata = is_array( $session['metadata'] ?? null ) ? $session['metadata'] : array();
164+
if ( isset( $metadata['message_count'] ) && is_numeric( $metadata['message_count'] ) ) {
165+
return max( 0, (int) $metadata['message_count'] );
166+
}
167+
168+
$messages = $session['messages'] ?? null;
169+
return is_array( $messages ) ? count( $messages ) : 0;
170+
}
171+
152172
/**
153173
* Get a specific chat session with conversation.
154174
*
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
/**
3+
* Pure-PHP smoke test for chat list message count resolution.
4+
*
5+
* Run with: php tests/chat-list-message-count-smoke.php
6+
*
7+
* @package DataMachine\Tests
8+
*/
9+
10+
if ( ! defined( 'ABSPATH' ) ) {
11+
define( 'ABSPATH', __DIR__ . '/' );
12+
}
13+
14+
if ( ! class_exists( 'WP_CLI_Command' ) ) {
15+
class WP_CLI_Command {}
16+
}
17+
18+
require_once __DIR__ . '/../inc/Cli/BaseCommand.php';
19+
require_once __DIR__ . '/../inc/Cli/Commands/ChatCommand.php';
20+
21+
$failures = array();
22+
$passes = 0;
23+
24+
$assert_same = static function ( int $expected, int $actual, string $label ) use ( &$failures, &$passes ): void {
25+
if ( $expected === $actual ) {
26+
++$passes;
27+
echo "PASS: {$label}\n";
28+
return;
29+
}
30+
31+
$failures[] = sprintf( '%s (expected %d, got %d)', $label, $expected, $actual );
32+
echo "FAIL: {$label}\n";
33+
};
34+
35+
echo "chat-list-message-count-smoke\n";
36+
37+
$method = new ReflectionMethod( DataMachine\Cli\Commands\ChatCommand::class, 'get_session_message_count' );
38+
39+
$count_messages = static function ( array $session ) use ( $method ): int {
40+
return (int) $method->invoke( null, $session );
41+
};
42+
43+
$assert_same( 8, $count_messages( array( 'message_count' => 8 ) ), 'uses top-level session index count' );
44+
$assert_same( 4, $count_messages( array( 'metadata' => array( 'message_count' => 4 ) ) ), 'falls back to metadata count' );
45+
$assert_same(
46+
2,
47+
$count_messages(
48+
array(
49+
'messages' => array(
50+
array( 'role' => 'user', 'content' => 'hello' ),
51+
array( 'role' => 'assistant', 'content' => 'hi' ),
52+
),
53+
)
54+
),
55+
'falls back to full message array count'
56+
);
57+
$assert_same(
58+
8,
59+
$count_messages(
60+
array(
61+
'message_count' => 8,
62+
'metadata' => array( 'message_count' => 0 ),
63+
)
64+
),
65+
'prefers top-level session index count over stale metadata'
66+
);
67+
68+
if ( $failures ) {
69+
echo "\nFAILED: " . count( $failures ) . " chat list message count assertions failed.\n";
70+
foreach ( $failures as $failure ) {
71+
echo "- {$failure}\n";
72+
}
73+
exit( 1 );
74+
}
75+
76+
echo "\nAll {$passes} chat list message count assertions passed.\n";

0 commit comments

Comments
 (0)