Add playwright E2E tests (#2174)

This commit is contained in:
Sam Freund
2025-12-04 22:25:48 -06:00
committed by GitHub
parent f821657d2b
commit 017b074eae
18 changed files with 421 additions and 46 deletions

View File

@@ -74,6 +74,12 @@ public class RequestHandler {
private static final ObjectMapper kObjectMapper = new ObjectMapper();
private static boolean testMode = false;
public static void setTestMode(boolean isTestMode) {
testMode = isTestMode;
}
private record CommonCameraUniqueName(String cameraUniqueName) {}
public static void onSettingsImportRequest(Context ctx) {
@@ -662,35 +668,36 @@ public class RequestHandler {
ModelProperties modelProperties =
new ModelProperties(modelPath, nickname, labels, width, height, family, version);
ObjectDetector objDetector = null;
try {
objDetector =
switch (family) {
case RUBIK -> new RubikModel(modelProperties).load();
case RKNN -> new RknnModel(modelProperties).load();
};
} catch (RuntimeException e) {
ctx.status(400);
ctx.result("Failed to load object detection model: " + e.getMessage());
if (!testMode) {
ObjectDetector objDetector = null;
try {
Files.deleteIfExists(modelPath);
} catch (IOException ex) {
e.addSuppressed(ex);
}
objDetector =
switch (family) {
case RUBIK -> new RubikModel(modelProperties).load();
case RKNN -> new RknnModel(modelProperties).load();
};
} catch (RuntimeException e) {
ctx.status(400);
ctx.result("Failed to load object detection model: " + e.getMessage());
logger.error("Failed to load object detection model", e);
return;
} finally {
// this finally block will run regardless of what happens in try/catch
// please see https://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html
// for a summary on how finally works
if (objDetector != null) {
objDetector.release();
try {
Files.deleteIfExists(modelPath);
} catch (IOException ex) {
e.addSuppressed(ex);
}
logger.error("Failed to load object detection model", e);
return;
} finally {
// this finally block will run regardless of what happens in try/catch
// please see https://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html
// for a summary on how finally works
if (objDetector != null) {
objDetector.release();
}
}
}
ConfigManager.getInstance()
.getConfig()
.neuralNetworkPropertyManager()

View File

@@ -162,6 +162,13 @@ public class Server {
app.post("/api/objectdetection/rename", RequestHandler::onRenameObjectDetectionModelRequest);
app.post("/api/objectdetection/nuke", RequestHandler::onNukeObjectDetectionModelsRequest);
/* Testing API Events */
app.post("/api/test/resetBackend", TestRequestHandler::handleResetRequest);
app.post("/api/test/activateTestMode", TestRequestHandler::testMode);
app.post("/api/test/override/platform", TestRequestHandler::handlePlatformOverrideRequest);
app.start(port);
}

View File

@@ -0,0 +1,67 @@
/*
* Copyright (C) Photon Vision.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.photonvision.server;
import io.javalin.http.Context;
import org.photonvision.common.configuration.ConfigManager;
import org.photonvision.common.configuration.NeuralNetworkModelManager;
import org.photonvision.common.hardware.Platform;
import org.photonvision.common.logging.LogGroup;
import org.photonvision.common.logging.Logger;
import org.photonvision.common.util.file.JacksonUtils;
public class TestRequestHandler {
// Treat all 2XX calls as "INFO"
// Treat all 4XX calls as "ERROR"
// Treat all 5XX calls as "ERROR"
static Logger logger = new Logger(TestRequestHandler.class, LogGroup.WebServer);
public static void handleResetRequest(Context ctx) {
logger.info("Resetting Backend");
// Reset backend
ConfigManager.nukeConfigDirectory();
ConfigManager.getInstance().load();
}
private record PlatformOverrideRequest(Platform platform) {}
public static void handlePlatformOverrideRequest(Context ctx) {
try {
PlatformOverrideRequest request =
JacksonUtils.deserialize(ctx.body(), PlatformOverrideRequest.class);
Platform platform = request.platform();
logger.info("Overriding platform to: " + platform);
Platform.overridePlatform(platform);
NeuralNetworkModelManager.getInstance(true).extractModels();
NeuralNetworkModelManager.getInstance().discoverModels();
ctx.status(200);
} catch (Exception e) {
logger.error("Failed to parse platform override request: " + e.getMessage());
ctx.status(400).result("Invalid request");
}
}
public static void testMode(Context ctx) {
logger.info("Test mode activated");
RequestHandler.setTestMode(true);
ctx.status(200).result("Test mode activated");
}
}