Skip to content

Commit 6a41298

Browse files
committed
Fixed issue with COMPONENT errors
1 parent 86c7e1e commit 6a41298

4 files changed

Lines changed: 116 additions & 48 deletions

File tree

Plan/common/src/main/java/com/djrapitops/plan/settings/config/PlanConfig.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,9 @@ public boolean isFalse(Setting<Boolean> setting) {
109109
return !isTrue(setting);
110110
}
111111

112-
public <T> void set(Setting<T> setting, T value) {
112+
public <T> PlanConfig set(Setting<T> setting, T value) {
113113
set(setting.getPath(), value);
114+
return this;
114115
}
115116

116117
public TimeZone getTimeZone() {

Plan/common/src/test/java/com/djrapitops/plan/delivery/webserver/JSErrorRegressionTest.java

Lines changed: 110 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,26 @@
1717
package com.djrapitops.plan.delivery.webserver;
1818

1919
import com.djrapitops.plan.PlanSystem;
20+
import com.djrapitops.plan.component.Component;
21+
import com.djrapitops.plan.component.ComponentService;
22+
import com.djrapitops.plan.extension.DataExtension;
23+
import com.djrapitops.plan.extension.annotation.ComponentProvider;
24+
import com.djrapitops.plan.extension.annotation.PluginInfo;
2025
import com.djrapitops.plan.gathering.domain.DataMap;
2126
import com.djrapitops.plan.gathering.domain.FinishedSession;
2227
import com.djrapitops.plan.identification.ServerUUID;
23-
import com.djrapitops.plan.settings.config.PlanConfig;
24-
import com.djrapitops.plan.settings.config.paths.DisplaySettings;
25-
import com.djrapitops.plan.settings.config.paths.ProxySettings;
2628
import com.djrapitops.plan.settings.config.paths.WebserverSettings;
27-
import com.djrapitops.plan.settings.locale.LangCode;
2829
import com.djrapitops.plan.settings.locale.Locale;
2930
import com.djrapitops.plan.storage.database.DBSystem;
3031
import com.djrapitops.plan.storage.database.Database;
3132
import com.djrapitops.plan.storage.database.transactions.events.PlayerRegisterTransaction;
3233
import com.djrapitops.plan.storage.database.transactions.events.StoreSessionTransaction;
3334
import com.djrapitops.plan.storage.database.transactions.events.StoreWorldNameTransaction;
35+
import extension.FullSystemExtension;
3436
import extension.SeleniumExtension;
37+
import org.intellij.lang.annotations.Language;
3538
import org.junit.jupiter.api.*;
3639
import org.junit.jupiter.api.extension.ExtendWith;
37-
import org.junit.jupiter.api.io.TempDir;
3840
import org.junit.jupiter.params.ParameterizedTest;
3941
import org.junit.jupiter.params.provider.CsvSource;
4042
import org.openqa.selenium.By;
@@ -44,11 +46,9 @@
4446
import org.openqa.selenium.logging.LogType;
4547
import utilities.RandomData;
4648
import utilities.TestConstants;
47-
import utilities.mocks.PluginMockComponent;
4849

49-
import java.nio.file.Path;
5050
import java.util.*;
51-
import java.util.stream.Collectors;
51+
import java.util.stream.Stream;
5252

5353
import static com.djrapitops.plan.delivery.export.ExportTestUtilities.assertNoLogs;
5454

@@ -58,38 +58,24 @@
5858
* Errors may have been caused by:
5959
* - Javascript mistakes / build issues
6060
* - Missed console.log statements
61-
* - Automatic formatting of plugin javascript (See https://github.com/plan-player-analytics/Plan/issues/820)
6261
* - Missing file definition in Mocker
6362
*/
64-
@ExtendWith(SeleniumExtension.class)
63+
@ExtendWith({FullSystemExtension.class, SeleniumExtension.class})
6564
class JSErrorRegressionTest {
6665

6766
private static final int TEST_PORT_NUMBER = 9091;
6867

69-
public static PluginMockComponent component;
70-
71-
private static PlanSystem planSystem;
72-
private static ServerUUID serverUUID;
73-
7468
@BeforeAll
75-
static void setUpClass(@TempDir Path tempDir) throws Exception {
76-
component = new PluginMockComponent(tempDir);
77-
planSystem = component.getPlanSystem();
78-
79-
PlanConfig config = planSystem.getConfigSystem().getConfig();
80-
config.set(WebserverSettings.PORT, TEST_PORT_NUMBER);
81-
config.set(ProxySettings.IP, "localhost:" + TEST_PORT_NUMBER);
82-
83-
// Avoid accidentally DDoS:ing head image service during tests.
84-
config.set(DisplaySettings.PLAYER_HEAD_IMG_URL, "data:image/png;base64,AA==");
85-
86-
planSystem.enable();
87-
serverUUID = planSystem.getServerInfo().getServerUUID();
88-
savePlayerData();
69+
static void setUpClass(PlanSystem system) {
70+
system.getConfigSystem().getConfig()
71+
.set(WebserverSettings.PORT, TEST_PORT_NUMBER);
72+
system.enable();
73+
savePlayerData(system);
8974
}
9075

91-
private static void savePlayerData() {
92-
DBSystem dbSystem = planSystem.getDatabaseSystem();
76+
private static void savePlayerData(PlanSystem system) {
77+
ServerUUID serverUUID = system.getServerInfo().getServerUUID();
78+
DBSystem dbSystem = system.getDatabaseSystem();
9379
Database database = dbSystem.getDatabase();
9480
UUID uuid = TestConstants.PLAYER_ONE_UUID;
9581
database.executeTransaction(new PlayerRegisterTransaction(uuid, RandomData::randomTime, TestConstants.PLAYER_ONE_NAME));
@@ -99,9 +85,9 @@ private static void savePlayerData() {
9985
}
10086

10187
@AfterAll
102-
static void tearDownClass() {
103-
if (planSystem != null) {
104-
planSystem.disable();
88+
static void tearDownClass(PlanSystem system) {
89+
if (system != null) {
90+
system.disable();
10591
}
10692
}
10793

@@ -111,7 +97,7 @@ void tearDownTest(WebDriver driver) {
11197
}
11298

11399
@TestFactory
114-
Collection<DynamicTest> javascriptRegressionTest(ChromeDriver driver) {
100+
Collection<DynamicTest> javascriptRegressionTest(ChromeDriver driver, PlanSystem system) {
115101
String[] addresses = new String[]{
116102
"http://localhost:" + TEST_PORT_NUMBER + "/player/" + TestConstants.PLAYER_ONE_NAME,
117103
"http://localhost:" + TEST_PORT_NUMBER + "/player/" + TestConstants.PLAYER_ONE_UUID_STRING,
@@ -121,19 +107,29 @@ Collection<DynamicTest> javascriptRegressionTest(ChromeDriver driver) {
121107
"http://localhost:" + TEST_PORT_NUMBER + "/query"
122108
};
123109

124-
LangCode[] languages = LangCode.values();
125-
return
126-
Arrays.stream(addresses)
127-
.map(link -> testAddress(link, driver))
128-
.collect(Collectors.toList());
110+
return Arrays.stream(addresses)
111+
.map(link -> testAddress(link, driver, system))
112+
.toList();
129113
}
130114

131-
private DynamicTest testAddress(String address, ChromeDriver driver) {
115+
@TestFactory
116+
Stream<DynamicTest> componentJsRegressionTest(ChromeDriver driver, PlanSystem system) {
117+
system.getApiServices().getExtensionService()
118+
.register(new ComponentExtension())
119+
.orElseThrow(AssertionError::new)
120+
.updatePlayerData(TestConstants.PLAYER_ONE_UUID, TestConstants.PLAYER_ONE_NAME);
121+
122+
String address = "http://localhost:" + TEST_PORT_NUMBER + "/player/" + TestConstants.PLAYER_ONE_UUID_STRING + "/plugins/" + system.getServerInfo().getServerIdentifier().getName().replaceAll(" ", "%20");
123+
return Stream.of(testAddress(address, driver, system));
124+
}
125+
126+
private DynamicTest testAddress(String address, ChromeDriver driver, PlanSystem system) {
132127
return DynamicTest.dynamicTest("Page should not log anything on js console: " + address, () -> {
133-
Locale locale = planSystem.getLocaleSystem().getLocale();
128+
Locale locale = system.getLocaleSystem().getLocale();
134129
try {
135130
driver.get(address);
136-
Thread.sleep(250);
131+
SeleniumExtension.waitForPageLoadForSeconds(5, driver);
132+
SeleniumExtension.waitForElementToBeVisible(By.className("load-in"), driver);
137133
assertNoLogs(driver.manage().logs().get(LogType.BROWSER).getAll(), address);
138134
} finally {
139135
locale.clear(); // Reset locale after test
@@ -175,9 +171,78 @@ private List<String> getLinks(ChromeDriver driver, int attempt) {
175171
.filter(href -> href.contains("localhost") && !href.contains("logout"))
176172
.map(href -> href.split("#")[0])
177173
.distinct()
178-
.collect(Collectors.toList());
179-
} catch (StaleElementReferenceException e) {
174+
.toList();
175+
} catch (StaleElementReferenceException _) {
180176
return getLinks(driver, attempt + 1);
181177
}
182178
}
179+
180+
@PluginInfo(name = "Component-regression")
181+
static class ComponentExtension implements DataExtension {
182+
@ComponentProvider(text = "regression")
183+
public Component component(UUID playerUUID) {
184+
@Language("JSON")
185+
String json = """
186+
{
187+
"extra": [
188+
{
189+
"color": "gray",
190+
"extra": [
191+
{
192+
"color": "#9D50BB",
193+
"extra": [
194+
{
195+
"color": "#914EB7",
196+
"extra": [
197+
{
198+
"color": "#864CB3",
199+
"extra": [
200+
{
201+
"color": "#7A4AAE",
202+
"extra": [
203+
{
204+
"color": "#6E48AA",
205+
"extra": [
206+
{
207+
"color": "gray",
208+
"extra": [
209+
{
210+
"color": "dark_aqua",
211+
"text": "Executive Advisor"
212+
}
213+
],
214+
"text": "] "
215+
}
216+
],
217+
"text": "f"
218+
}
219+
],
220+
"text": "f"
221+
}
222+
],
223+
"text": "a"
224+
}
225+
],
226+
"text": "t"
227+
}
228+
],
229+
"text": "S"
230+
}
231+
],
232+
"text": "["
233+
}
234+
],
235+
"text": " "
236+
}""";
237+
return ComponentService.getInstance().fromJson(json);
238+
}
239+
240+
@ComponentProvider(text = "regression2")
241+
public Component component2(UUID playerUUID) {
242+
@Language("JSON")
243+
String json2 = """
244+
{"color":"dark_aqua","text":"EA"}""";
245+
return ComponentService.getInstance().fromJson(json2);
246+
}
247+
}
183248
}

Plan/common/src/test/java/extension/FullSystemExtension.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.djrapitops.plan.identification.ServerUUID;
3030
import com.djrapitops.plan.settings.ConfigSystem;
3131
import com.djrapitops.plan.settings.config.PlanConfig;
32+
import com.djrapitops.plan.settings.config.paths.DisplaySettings;
3233
import com.djrapitops.plan.settings.config.paths.WebserverSettings;
3334
import com.djrapitops.plan.settings.locale.LocaleSystem;
3435
import com.djrapitops.plan.settings.theme.Theme;
@@ -95,7 +96,8 @@ public void beforeAll(ExtensionContext context) throws IOException {
9596
component = new PluginMockComponent(tempDir);
9697
planSystem = component.getPlanSystem();
9798
planSystem.getConfigSystem().getConfig()
98-
.set(WebserverSettings.PORT, TEST_PORT_NUMBER);
99+
.set(WebserverSettings.PORT, TEST_PORT_NUMBER)
100+
.set(DisplaySettings.PLAYER_HEAD_IMG_URL, "data:image/png;base64,AA==");
99101
}
100102

101103
@Override

Plan/react/dashboard/src/components/extensions/ExtensionCard.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const valueOrUndefined = (value) => {
5656
const sanitizeComponent = (component) => {
5757
if (!component) return [];
5858
return {
59-
extra: sanitizeComponent(component.extra),
59+
extra: component.extra ? component.extra.filter(Boolean).map(sanitizeComponent) : [],
6060
color: valueOrUndefined(component.color),
6161
bold: valueOrUndefined(component.bold),
6262
italic: valueOrUndefined(component.italic),

0 commit comments

Comments
 (0)