From 626ab209bc5b1b9f3a608a1c2a536ead3cd27a7f Mon Sep 17 00:00:00 2001
From: nsfisis <nsfisis@gmail.com>
Date: Wed, 2 Apr 2025 00:50:07 +0900
Subject: fix(backend): fix incorrect aggregation of testcase results

---
 backend/query.sql | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

(limited to 'backend/query.sql')

diff --git a/backend/query.sql b/backend/query.sql
index 2d56c1c..49fe2c1 100644
--- a/backend/query.sql
+++ b/backend/query.sql
@@ -99,15 +99,17 @@ VALUES ($1, $2, $3, $4, $5);
 -- name: AggregateTestcaseResults :one
 SELECT
     CASE
-        WHEN COUNT(CASE WHEN r.status IS NULL            THEN 1 END) > 0 THEN 'running'
+        WHEN COUNT(*) < (SELECT COUNT(*) FROM testcases WHERE problem_id =
+                         (SELECT problem_id FROM games WHERE game_id =
+                          (SELECT game_id FROM submissions AS s WHERE s.submission_id = $1)))
+        THEN 'running'
         WHEN COUNT(CASE WHEN r.status = 'internal_error' THEN 1 END) > 0 THEN 'internal_error'
         WHEN COUNT(CASE WHEN r.status = 'timeout'        THEN 1 END) > 0 THEN 'timeout'
         WHEN COUNT(CASE WHEN r.status = 'runtime_error'  THEN 1 END) > 0 THEN 'runtime_error'
         WHEN COUNT(CASE WHEN r.status = 'wrong_answer'   THEN 1 END) > 0 THEN 'wrong_answer'
         ELSE 'success'
     END AS status
-FROM testcases
-LEFT JOIN testcase_results AS r ON testcases.testcase_id = r.testcase_id
+FROM testcase_results AS r
 WHERE r.submission_id = $1;
 
 -- name: GetLatestState :one
-- 
cgit v1.2.3-70-g09d2

