Ура линии работают тесты прошли! Третий тест в лог выводиться!
This commit is contained in:
AidarKC 2025-12-29 14:14:53 +03:00
parent 783b5b08e3
commit ae3838ccf2
6 changed files with 146 additions and 55 deletions

View File

@ -1,5 +1,6 @@
package test.it; package test.it;
import test.it.utils.TestConfig;
import test.it.utils.TestLog; import test.it.utils.TestLog;
import java.io.IOException; import java.io.IOException;
@ -24,7 +25,7 @@ public class IT_CleanAllDate {
cleanupDataDir(DATA_DIR); cleanupDataDir(DATA_DIR);
} catch (Throwable t) { } catch (Throwable t) {
TestLog.boom("Не смог очистить data/. Причина: " + t.getMessage()); TestLog.boom("Не смог очистить data/. Причина: " + t.getMessage());
if (TestLog.VERBOSE) t.printStackTrace(System.out); if (TestConfig.DEBUG()) t.printStackTrace(System.out);
System.exit(1); System.exit(1);
} }

View File

@ -14,6 +14,10 @@ import java.util.Base64;
* ВАЖНО: * ВАЖНО:
* - ключи/имя блокчейна вычисляются из login (через ItRunContext). * - ключи/имя блокчейна вычисляются из login (через ItRunContext).
* - тесты можно запускать по отдельности, ItRunContext сам инициализируется при первом обращении. * - тесты можно запускать по отдельности, ItRunContext сам инициализируется при первом обращении.
*
* ЛОГИ:
* - детальный вывод включается флагом:
* -Dit.debug=true
*/ */
public final class TestConfig { public final class TestConfig {
@ -34,6 +38,11 @@ public final class TestConfig {
// Любая строка клиента (для логов) // Любая строка клиента (для логов)
public static final String TEST_CLIENT_INFO = "it-tests"; public static final String TEST_CLIENT_INFO = "it-tests";
/** DEBUG-режим: подробные логи отправки/получения/ожиданий (по умолчанию false). */
public static boolean DEBUG() {
return Boolean.parseBoolean(System.getProperty("it.debug", "true"));
}
/** login для прогона (по умолчанию DEFAULT_LOGIN, можно переопределить -Dit.login=...). */ /** login для прогона (по умолчанию DEFAULT_LOGIN, можно переопределить -Dit.login=...). */
public static String LOGIN() { public static String LOGIN() {
return System.getProperty("it.login", DEFAULT_LOGIN); return System.getProperty("it.login", DEFAULT_LOGIN);

View File

@ -3,22 +3,22 @@ package test.it.utils;
/** /**
* TestLog единое место для: * TestLog единое место для:
* - ANSI цветов * - ANSI цветов
* - стандартных красивых сообщений (title/ok/boom/line/step/send/recv) * - стандартных красивых сообщений (title/line/step/send/recv)
* *
* Включение/выключение подробных логов: * РЕЖИМЫ:
* -Dit.verbose=false * - it.debug=false (по умолчанию):
* * печатаем ТОЛЬКО итог: PASS/FAIL по каждому тесту
* По умолчанию verbose=true (удобно для ручного прогона). * - it.debug=true:
* печатаем всё: ожидания, отправка/ответ (JSON), промежуточные проверки
*/ */
public final class TestLog { public final class TestLog {
private TestLog() {} private TestLog() {}
// ============================ // ============================
// VERBOSE // DEBUG SWITCH
// ============================ // ============================
// включается так: ./gradlew test -Dit.verbose=true public static final boolean DEBUG = TestConfig.DEBUG();
public static final boolean VERBOSE = Boolean.parseBoolean(System.getProperty("it.verbose", "true"));
// ============================ // ============================
// ANSI COLORS // ANSI COLORS
@ -34,54 +34,55 @@ public final class TestLog {
// BASIC OUTPUT // BASIC OUTPUT
// ============================ // ============================
/** Дебаг-инфо (печатается только при DEBUG=true). */
public static void info(String s) { public static void info(String s) {
if (VERBOSE) System.out.println(s); if (DEBUG) System.out.println(s);
} }
public static void line() { public static void line() {
if (!VERBOSE) return; if (!DEBUG) return;
System.out.println(C + "------------------------------------------------------------" + R); System.out.println(C + "------------------------------------------------------------" + R);
} }
/** Короткое заглавие. */ /** Короткое заглавие (только DEBUG). */
public static void title(String s) { public static void title(String s) {
if (!VERBOSE) return; if (!DEBUG) return;
System.out.println(C + "\n============================================================" + R); System.out.println(C + "\n============================================================" + R);
System.out.println(C + s + R); System.out.println(C + s + R);
System.out.println(C + "============================================================\n" + R); System.out.println(C + "============================================================\n" + R);
} }
/** /** Длинное заглавие (только DEBUG). */
* Длинное заглавие (под многострочный текст).
*
* Пример:
* TestLog.titleBlock("""
* ТЕСТ: ...
* Ожидание: ...
* """);
*/
public static void titleBlock(String multiLineText) { public static void titleBlock(String multiLineText) {
if (!VERBOSE) return; if (!DEBUG) return;
System.out.println(C + "\n============================================================" + R); System.out.println(C + "\n============================================================" + R);
System.out.println(C + multiLineText + R); System.out.println(C + multiLineText + R);
System.out.println(C + "============================================================\n" + R); System.out.println(C + "============================================================\n" + R);
} }
/** Заголовок шага (только DEBUG). */
public static void stepTitle(String s) { public static void stepTitle(String s) {
if (!VERBOSE) return; if (!DEBUG) return;
System.out.println(C + "\n-------------------- " + s + " --------------------" + R); System.out.println(C + "\n-------------------- " + s + " --------------------" + R);
} }
/** Промежуточное ОК (только DEBUG). */
public static void ok(String s) { public static void ok(String s) {
if (!VERBOSE) return; if (!DEBUG) return;
System.out.println(G + "" + s + R);
}
/** Итоговый PASS (печатается ВСЕГДА). */
public static void pass(String s) {
System.out.println(G + "" + s + R); System.out.println(G + "" + s + R);
} }
public static void warn(String s) { public static void warn(String s) {
if (!VERBOSE) return; if (!DEBUG) return;
System.out.println(Y + "⚠️ " + s + R); System.out.println(Y + "⚠️ " + s + R);
} }
/** FAIL (печатается ВСЕГДА). */
public static void boom(String s) { public static void boom(String s) {
System.out.println(RED + "****************************************************************" + R); System.out.println(RED + "****************************************************************" + R);
System.out.println(RED + "" + s + R); System.out.println(RED + "" + s + R);
@ -89,14 +90,14 @@ public final class TestLog {
} }
public static void send(String op, String json) { public static void send(String op, String json) {
if (!VERBOSE) return; if (!DEBUG) return;
System.out.println("📤 [" + op + "] Request JSON:"); System.out.println("📤 [" + op + "] Request JSON:");
System.out.println(json); System.out.println(json);
line(); line();
} }
public static void recv(String op, String json) { public static void recv(String op, String json) {
if (!VERBOSE) return; if (!DEBUG) return;
System.out.println("📥 [" + op + "] Response JSON:"); System.out.println("📥 [" + op + "] Response JSON:");
System.out.println(json); System.out.println(json);
line(); line();
@ -117,11 +118,11 @@ public final class TestLog {
public static int runOne(String testName, Runnable body) { public static int runOne(String testName, Runnable body) {
try { try {
body.run(); body.run();
ok(testName + ": OK"); pass(testName + ": OK");
return 0; return 0;
} catch (Throwable t) { } catch (Throwable t) {
boom(testName + ": FAIL. Причина: " + t.getMessage()); boom(testName + ": FAIL. Причина: " + t.getMessage());
if (VERBOSE) t.printStackTrace(System.out); if (DEBUG) t.printStackTrace(System.out);
return 1; return 1;
} }
} }

View File

@ -7,6 +7,7 @@ import blockchain.body.ReactionBody;
import blockchain.body.TextBody; import blockchain.body.TextBody;
import test.it.utils.JsonParsers; import test.it.utils.JsonParsers;
import test.it.utils.TestConfig; import test.it.utils.TestConfig;
import test.it.utils.TestLog;
import utils.crypto.Ed25519Util; import utils.crypto.Ed25519Util;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
@ -31,6 +32,13 @@ import static org.junit.jupiter.api.Assertions.*;
* *
* Важно: * Важно:
* - Этот класс НЕ занимается красивыми логами. Только логика + проверки. * - Этот класс НЕ занимается красивыми логами. Только логика + проверки.
*
* ДОБАВЛЕНО:
* - При it.debug=true печатаем:
* * какой блок шлём (global/line/lineNum)
* * локальный hash
* * serverLastGlobalHash
* * итоги проверок
*/ */
public final class AddBlockFlow { public final class AddBlockFlow {
@ -72,7 +80,7 @@ public final class AddBlockFlow {
); );
String req = buildAddBlockJson(TestConfig.BCH_NAME(), 0, ZERO64, base64(header.fullBytes)); String req = buildAddBlockJson(TestConfig.BCH_NAME(), 0, ZERO64, base64(header.fullBytes));
String resp = WsJsonOneShot.request(req, timeout); String resp = WsJsonOneShot.request("AddBlock#HEADER", req, timeout);
assert200("AddBlock(HEADER)", resp); assert200("AddBlock(HEADER)", resp);
@ -81,6 +89,12 @@ public final class AddBlockFlow {
assertEquals(64, serverLastGlobalHash0.trim().length(), "HEADER: serverLastGlobalHash must be 64 hex chars"); assertEquals(64, serverLastGlobalHash0.trim().length(), "HEADER: serverLastGlobalHash must be 64 hex chars");
String localHash0 = bytesToHex64(header.hash32); String localHash0 = bytesToHex64(header.hash32);
if (TestConfig.DEBUG()) {
TestLog.ok("HEADER: локальный hash=" + localHash0);
TestLog.ok("HEADER: serverLastGlobalHash=" + serverLastGlobalHash0);
}
assertEquals(localHash0, serverLastGlobalHash0, "HEADER: serverLastGlobalHash должен совпасть с локальным hash"); assertEquals(localHash0, serverLastGlobalHash0, "HEADER: serverLastGlobalHash должен совпасть с локальным hash");
// обновляем локальное состояние // обновляем локальное состояние
@ -90,6 +104,10 @@ public final class AddBlockFlow {
lineLastNumber[LINE_HEADER] = 0; lineLastNumber[LINE_HEADER] = 0;
lineLastHashHex[LINE_HEADER] = localHash0; lineLastHashHex[LINE_HEADER] = localHash0;
if (TestConfig.DEBUG()) {
TestLog.ok("HEADER: проверка OK, состояние обновлено (globalLastNumber=0)");
}
} }
/** Шлём следующий TEXT блок в line=1. */ /** Шлём следующий TEXT блок в line=1. */
@ -110,7 +128,8 @@ public final class AddBlockFlow {
); );
String req = buildAddBlockJson(TestConfig.BCH_NAME(), nextGlobal, globalLastHashHex, base64(b.fullBytes)); String req = buildAddBlockJson(TestConfig.BCH_NAME(), nextGlobal, globalLastHashHex, base64(b.fullBytes));
String resp = WsJsonOneShot.request(req, timeout); String op = "AddBlock#TEXT (global=" + nextGlobal + ", line=1, lineNum=" + lineNum + ")";
String resp = WsJsonOneShot.request(op, req, timeout);
assert200("AddBlock(TEXT)", resp); assert200("AddBlock(TEXT)", resp);
@ -119,6 +138,12 @@ public final class AddBlockFlow {
assertEquals(64, serverLastGlobalHash.trim().length(), "TEXT: serverLastGlobalHash must be 64 hex chars"); assertEquals(64, serverLastGlobalHash.trim().length(), "TEXT: serverLastGlobalHash must be 64 hex chars");
String localHash = bytesToHex64(b.hash32); String localHash = bytesToHex64(b.hash32);
if (TestConfig.DEBUG()) {
TestLog.ok("TEXT: локальный hash=" + localHash);
TestLog.ok("TEXT: serverLastGlobalHash=" + serverLastGlobalHash);
}
assertEquals(localHash, serverLastGlobalHash, "TEXT: serverLastGlobalHash должен совпасть с локальным hash"); assertEquals(localHash, serverLastGlobalHash, "TEXT: serverLastGlobalHash должен совпасть с локальным hash");
// обновляем состояние // обновляем состояние
@ -127,6 +152,10 @@ public final class AddBlockFlow {
lineLastNumber[LINE_TEXT] = lineNum; lineLastNumber[LINE_TEXT] = lineNum;
lineLastHashHex[LINE_TEXT] = localHash; lineLastHashHex[LINE_TEXT] = localHash;
if (TestConfig.DEBUG()) {
TestLog.ok("TEXT: проверка OK, состояние обновлено (globalLastNumber=" + globalLastNumber + ")");
}
return b; return b;
} }
@ -157,7 +186,8 @@ public final class AddBlockFlow {
); );
String req = buildAddBlockJson(TestConfig.BCH_NAME(), nextGlobal, globalLastHashHex, base64(b.fullBytes)); String req = buildAddBlockJson(TestConfig.BCH_NAME(), nextGlobal, globalLastHashHex, base64(b.fullBytes));
String resp = WsJsonOneShot.request(req, timeout); String op = "AddBlock#REACT (global=" + nextGlobal + ", line=2, lineNum=" + lineNum + ")";
String resp = WsJsonOneShot.request(op, req, timeout);
assert200("AddBlock(REACT)", resp); assert200("AddBlock(REACT)", resp);
@ -166,6 +196,12 @@ public final class AddBlockFlow {
assertEquals(64, serverLastGlobalHash.trim().length(), "REACT: serverLastGlobalHash must be 64 hex chars"); assertEquals(64, serverLastGlobalHash.trim().length(), "REACT: serverLastGlobalHash must be 64 hex chars");
String localHash = bytesToHex64(b.hash32); String localHash = bytesToHex64(b.hash32);
if (TestConfig.DEBUG()) {
TestLog.ok("REACT: локальный hash=" + localHash);
TestLog.ok("REACT: serverLastGlobalHash=" + serverLastGlobalHash);
}
assertEquals(localHash, serverLastGlobalHash, "REACT: serverLastGlobalHash должен совпасть с локальным hash"); assertEquals(localHash, serverLastGlobalHash, "REACT: serverLastGlobalHash должен совпасть с локальным hash");
// обновляем состояние // обновляем состояние
@ -174,6 +210,10 @@ public final class AddBlockFlow {
lineLastNumber[LINE_REACT] = lineNum; lineLastNumber[LINE_REACT] = lineNum;
lineLastHashHex[LINE_REACT] = localHash; lineLastHashHex[LINE_REACT] = localHash;
if (TestConfig.DEBUG()) {
TestLog.ok("REACT: проверка OK, состояние обновлено (globalLastNumber=" + globalLastNumber + ")");
}
return b; return b;
} }
@ -355,6 +395,9 @@ public final class AddBlockFlow {
private static void assert200(String op, String resp) { private static void assert200(String op, String resp) {
int st = JsonParsers.status(resp); int st = JsonParsers.status(resp);
assertEquals(200, st, op + ": expected status=200, but got=" + st + ", resp=" + resp); assertEquals(200, st, op + ": expected status=200, but got=" + st + ", resp=" + resp);
if (TestConfig.DEBUG()) {
TestLog.ok(op + ": status=200");
}
} }
private static String extractPayloadString(String json, String field) { private static String extractPayloadString(String json, String field) {

View File

@ -4,8 +4,8 @@ import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import test.it.utils.ItRunContext; import test.it.utils.ItRunContext;
import test.it.utils.JsonBuilders; import test.it.utils.JsonBuilders;
import test.it.utils.JsonParsers;
import test.it.utils.TestConfig; import test.it.utils.TestConfig;
import test.it.utils.TestLog;
import java.time.Duration; import java.time.Duration;
@ -38,6 +38,7 @@ public class IT_03_AddBlock_NoAuth {
ItRunContext.initIfNeeded(); ItRunContext.initIfNeeded();
ensureUserExists(); ensureUserExists();
new IT_03_AddBlock_NoAuth().addBlock_shouldAppendHeaderThenTextThenReaction(); new IT_03_AddBlock_NoAuth().addBlock_shouldAppendHeaderThenTextThenReaction();
TestLog.pass("IT_03_AddBlock_NoAuth: OK");
} }
@BeforeAll @BeforeAll
@ -47,23 +48,11 @@ public class IT_03_AddBlock_NoAuth {
// ВАЖНО: // ВАЖНО:
// - requestId тут не важен, но пусть будет. // - requestId тут не важен, но пусть будет.
// - отдельная авторизация не нужна, но пользователь должен существовать. // - отдельная авторизация не нужна, но пользователь должен существовать.
String reqJson = JsonBuilders.addUser("it03-adduser-beforeall"); //
/** // Если хочешь реально включить предусловие здесь просто раскомментируй блок,
String resp = WsJsonOneShot.request(reqJson, Duration.ofSeconds(5)); // но сейчас у тебя он закомментирован.
int st = JsonParsers.status(resp); // String reqJson = JsonBuilders.addUser("it03-adduser-beforeall");
// ничего не делаем предусловие временно отключено
if (st == 200) {
// ok
return;
}
if (st == 409) {
String code = JsonParsers.errorCode(resp);
if ("USER_ALREADY_EXISTS".equals(code)) return;
fail("User precondition failed. status=409, code=" + code + ", resp=" + resp);
}
fail("User precondition failed. status=" + st + ", resp=" + resp);
*/
} }
@Test @Test
@ -73,24 +62,41 @@ public class IT_03_AddBlock_NoAuth {
// таймаут на каждый one-shot запрос // таймаут на каждый one-shot запрос
Duration t = Duration.ofSeconds(8); Duration t = Duration.ofSeconds(8);
if (TestConfig.DEBUG()) {
TestLog.titleBlock("""
IT_03_AddBlock_NoAuth: сценарий AddBlock без отдельной авторизации
Используем:
login = %s
blockchainName = %s
debug=true: покажем отправку/ответ (JSON) и проверки hash
""".formatted(TestConfig.LOGIN(), TestConfig.BCH_NAME()));
}
// 1) состояние + сборка + отправка // 1) состояние + сборка + отправка
AddBlockFlow flow = new AddBlockFlow(); AddBlockFlow flow = new AddBlockFlow();
// ========================================================= // =========================================================
// ШАГ 0: ВАЖНО первым всегда HEADER global=0 // ШАГ 0: ВАЖНО первым всегда HEADER global=0
// ========================================================= // =========================================================
if (TestConfig.DEBUG()) TestLog.stepTitle("ШАГ 0: AddBlock HEADER (global=0, line=0, lineNum=0)");
flow.sendHeader0(t); flow.sendHeader0(t);
// ========================================================= // =========================================================
// ШАГ 1..3: TEXT (line=1) // ШАГ 1..3: TEXT (line=1)
// ========================================================= // =========================================================
if (TestConfig.DEBUG()) TestLog.stepTitle("ШАГ 1: AddBlock TEXT#1 (line=1)");
AddBlockFlow.BuiltBlock text1 = flow.sendNextText("Hello #1 from IT_03 test", t); AddBlockFlow.BuiltBlock text1 = flow.sendNextText("Hello #1 from IT_03 test", t);
if (TestConfig.DEBUG()) TestLog.stepTitle("ШАГ 2: AddBlock TEXT#2 (line=1)");
flow.sendNextText("Hello #2 from IT_03 test", t); flow.sendNextText("Hello #2 from IT_03 test", t);
if (TestConfig.DEBUG()) TestLog.stepTitle("ШАГ 3: AddBlock TEXT#3 (line=1)");
flow.sendNextText("Hello #3 from IT_03 test", t); flow.sendNextText("Hello #3 from IT_03 test", t);
// ========================================================= // =========================================================
// ШАГ 4: REACT#1 (line=2) -> на TEXT#1 (global=1, hash=text1) // ШАГ 4: REACT#1 (line=2) -> на TEXT#1 (global=1, hash=text1)
// ========================================================= // =========================================================
if (TestConfig.DEBUG()) TestLog.stepTitle("ШАГ 4: AddBlock REACT#1 (line=2) -> на TEXT#1 (global=1)");
flow.sendNextReaction( flow.sendNextReaction(
1, // reactionCode (пример: 1 = like) 1, // reactionCode (пример: 1 = like)
TestConfig.BCH_NAME(), // toBlockchainName TestConfig.BCH_NAME(), // toBlockchainName
@ -105,5 +111,8 @@ public class IT_03_AddBlock_NoAuth {
assertEquals(1, flow.lineLastNumber(AddBlockFlow.LINE_REACT), "В line=2 должен быть 1 блок"); assertEquals(1, flow.lineLastNumber(AddBlockFlow.LINE_REACT), "В line=2 должен быть 1 блок");
assertNotNull(flow.globalLastHashHex()); assertNotNull(flow.globalLastHashHex());
assertEquals(64, flow.globalLastHashHex().length()); assertEquals(64, flow.globalLastHashHex().length());
// Итог (в обычном режиме это будет единственная строка)
TestLog.pass("IT_03_AddBlock_NoAuth: OK");
} }
} }

View File

@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import test.it.utils.TestConfig; import test.it.utils.TestConfig;
import test.it.utils.TestLog;
import test.it.utils.WsTestClient; import test.it.utils.WsTestClient;
import java.time.Duration; import java.time.Duration;
@ -32,15 +33,33 @@ public final class WsJsonOneShot {
private WsJsonOneShot() {} private WsJsonOneShot() {}
/** /**
* Отправить JSON строкой и вернуть JSON ответ строкой. * Старый API (без имени операции) оставляем для совместимости.
* Соединение создаётся и закрывается ВНУТРИ.
*/ */
public static String request(String json, Duration timeout) { public static String request(String json, Duration timeout) {
return request("WS", json, timeout);
}
/**
* Отправить JSON строкой и вернуть JSON ответ строкой.
* Соединение создаётся и закрывается ВНУТРИ.
*
* Если включён it.debug=true печатаем request/response.
*/
public static String request(String op, String json, Duration timeout) {
String patched = forceRequestId(json, FIXED_REQUEST_ID); String patched = forceRequestId(json, FIXED_REQUEST_ID);
if (TestConfig.DEBUG()) {
TestLog.send(op, prettyOrRaw(patched));
}
try (WsTestClient client = new WsTestClient(TestConfig.WS_URI)) { try (WsTestClient client = new WsTestClient(TestConfig.WS_URI)) {
// requestId нам нужен только как ключ ожидания в WsTestClient String resp = client.request(FIXED_REQUEST_ID, patched, timeout);
return client.request(FIXED_REQUEST_ID, patched, timeout);
if (TestConfig.DEBUG()) {
TestLog.recv(op, prettyOrRaw(resp));
}
return resp;
} }
} }
@ -59,4 +78,13 @@ public final class WsJsonOneShot {
return json; return json;
} }
} }
private static String prettyOrRaw(String json) {
try {
JsonNode n = MAPPER.readTree(json);
return MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(n);
} catch (Exception ignore) {
return json;
}
}
} }