This commit is contained in:
root
2025-12-20 21:49:36 +05:00
parent fb62bebbe2
commit 9ffdcbcd3b
2 changed files with 142 additions and 1 deletions
@@ -0,0 +1,112 @@
package kz.konturai.parser.service;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.junit.jupiter.api.Test;
class MarketingAnalysisServiceChartPlacementTest {
// Keep this test free of Spring/Mockito to avoid agent-attach requirements.
private final MarketingAnalysisService marketingAnalysisService = new MarketingAnalysisService(null, null, null,
null);
@SuppressWarnings({ "unchecked", "rawtypes" })
private Map buildPlaceholderMap() throws Exception {
Class<?> chartMappingClass = Class.forName("kz.konturai.parser.service.MarketingAnalysisService$ChartMapping");
Constructor<?> ctor = chartMappingClass.getDeclaredConstructor(String.class, String.class);
ctor.setAccessible(true);
Map placeholderMap = new HashMap();
placeholderMap.put("[[CHART_SEASONALITY]]", ctor.newInstance("seasonality", "seasonality"));
placeholderMap.put("[[CHART_AGE_DISTRIBUTION]]", ctor.newInstance("audienceAge", "audienceAge"));
placeholderMap.put("[[CHART_GENDER_DISTRIBUTION]]", ctor.newInstance("audienceGender", "audienceGender"));
placeholderMap.put("[[CHART_MARKET_SHARE]]", ctor.newInstance("marketShareChart", "marketShareChart"));
placeholderMap.put("[[TABLE_COMPARISON]]", ctor.newInstance("comparisonTable", "comparisonTable"));
placeholderMap.put("[[CHART_SWOT]]", ctor.newInstance("swot", "swot"));
placeholderMap.put("[[CHART_CHANNELS]]", ctor.newInstance("channelsPotential", "channelsPotential"));
placeholderMap.put("[[CHART_FUNNEL]]", ctor.newInstance("conversionFunnel", "funnel"));
return placeholderMap;
}
private Map<String, Object> buildChartsDataSeasonalityOnly() {
Map<String, Object> chartsData = new HashMap<>();
chartsData.put("seasonality", Map.of("labels", java.util.List.of("Jan"), "values", java.util.List.of(1)));
return chartsData;
}
@Test
void addMissingChartsToSections_shouldIgnoreTocListItems_andUseRealMarkdownHeading() throws Exception {
String report = ""
+ "## Оглавление\n"
+ "- [III. Анализ рынка и сезонности](#section-iii)\n"
+ "- [IV. Анализ целевой аудитории](#section-iv)\n"
+ "\n"
+ "## III. Анализ рынка и сезонности\n"
+ "Тут есть текст про рынок.\n"
+ "\n"
+ "## IV. Анализ целевой аудитории\n"
+ "Тут аудитория.\n";
Method m = MarketingAnalysisService.class.getDeclaredMethod("addMissingChartsToSections", String.class,
Map.class,
Map.class, Set.class);
m.setAccessible(true);
String out = (String) m.invoke(
marketingAnalysisService,
report,
buildChartsDataSeasonalityOnly(),
buildPlaceholderMap(),
new HashSet<String>());
int tocEnd = out.indexOf("## III.");
assertTrue(tocEnd > 0, "Sanity check: section III should exist");
String tocPart = out.substring(0, tocEnd);
assertFalse(tocPart.contains("[[CHART_SEASONALITY]]"), "Must not insert into TOC block");
int s3Start = out.indexOf("## III.");
int s4Start = out.indexOf("## IV.");
assertTrue(s3Start >= 0 && s4Start > s3Start, "Sanity check: III before IV");
String section3Body = out.substring(s3Start, s4Start);
assertTrue(section3Body.contains("[[CHART_SEASONALITY]]"), "Must insert into section III body, not TOC");
}
@Test
void addMissingChartsToSections_whenAnyPlaceholderProcessed_shouldNotAppendAnything() throws Exception {
String report = ""
+ "## III. Анализ рынка и сезонности\n"
+ "Тут есть текст про рынок.\n"
+ "\n"
+ "## IV. Анализ целевой аудитории\n"
+ "Тут аудитория.\n";
Method m = MarketingAnalysisService.class.getDeclaredMethod("addMissingChartsToSections", String.class,
Map.class,
Map.class, Set.class);
m.setAccessible(true);
// Simulate: placeholder was already successfully replaced earlier in the
// pipeline
// (replaceChartPlaceholders adds normalized tokens to processedPlaceholders).
Set<String> processed = new HashSet<>();
processed.add("[[CHART_SEASONALITY]]".replaceAll("\\s+", "").toUpperCase(java.util.Locale.ROOT));
String out = (String) m.invoke(
marketingAnalysisService,
report,
buildChartsDataSeasonalityOnly(),
buildPlaceholderMap(),
processed);
assertEquals(report, out, "If any placeholder was processed, must not append any missing placeholders");
}
}