// SPDX-License-Identifier: BSD-3-Clause // Copyright (c) 2026, The OpenROAD Authors #include #include #include #include #include "boost/json/object.hpp" #include "boost/json/parse.hpp" #include "gtest/gtest.h" #include "odb/db.h" #include "request_handler.h" #include "tile_generator.h" #include "tst/nangate45_fixture.h" namespace web { namespace { // Helper to extract payload as string. std::string payloadStr(const WebSocketResponse& resp) { return std::string(resp.payload.begin(), resp.payload.end()); } // Helper to parse a JSON literal into a boost::json::object for tests. boost::json::object parseObj(std::string_view json) { return boost::json::parse(json).as_object(); } // ─── TileGenerator::snapAt tests ───────────────────────────────────────────── class SnapTest : public tst::Nangate45Fixture { protected: void SetUp() override { block_->setDieArea(odb::Rect(0, 0, 100000, 100000)); block_->setCoreArea(odb::Rect(0, 0, 100000, 100000)); } void makeTileGen() { tile_gen_ = std::make_unique( getDb(), /*sta=*/nullptr, getLogger()); } odb::dbInst* placeInst(const char* master_name, const char* inst_name, int x, int y) { odb::dbMaster* master = lib_->findMaster(master_name); EXPECT_NE(master, nullptr); odb::dbInst* inst = odb::dbInst::create(block_, master, inst_name); inst->setLocation(x, y); inst->setPlacementStatus(odb::dbPlacementStatus::PLACED); return inst; } std::unique_ptr tile_gen_; }; TEST_F(SnapTest, NoBlockReturnsNotFound) { // Create a generator with a valid db but then clear the block's die area // to create an empty design. makeTileGen(); TileVisibility vis; std::set layers{"metal1"}; auto result = tile_gen_->snapAt(50000, 50000, 1000, 10, true, true, vis, layers); // With an empty design (no instances, no routing), snap should not find // anything except possibly the die area edges. // We just verify it doesn't crash and returns a valid result. EXPECT_TRUE(result.found || !result.found); // no crash } TEST_F(SnapTest, SnapNearInstanceEdge) { // Place an instance at (0,0). BUF_X16 has a known size in Nangate45. placeInst("BUF_X16", "buf1", 0, 0); makeTileGen(); TileVisibility vis; std::set layers; // Query near the origin where the instance is placed. // The die area edge is at x=0 and y=0 which should be findable. auto result = tile_gen_->snapAt(100, 100, 5000, 50, true, true, vis, layers); // Should find at least the die area boundary. EXPECT_TRUE(result.found); EXPECT_LT(result.distance, 5000); } TEST_F(SnapTest, SnapWithHorizontalConstraint) { placeInst("BUF_X16", "buf1", 0, 0); makeTileGen(); TileVisibility vis; std::set layers; // horizontal=true, vertical=false → only horizontal edges (top/bottom) auto result = tile_gen_->snapAt(50000, 100, 5000, 50, true, false, vis, layers); if (result.found) { // A horizontal-only snap should return an edge where y1 == y2 EXPECT_EQ(result.edge.first.y(), result.edge.second.y()); } } TEST_F(SnapTest, SnapWithVerticalConstraint) { placeInst("BUF_X16", "buf1", 0, 0); makeTileGen(); TileVisibility vis; std::set layers; // horizontal=false, vertical=true → only vertical edges (left/right) auto result = tile_gen_->snapAt(100, 50000, 5000, 50, false, true, vis, layers); if (result.found) { // A vertical-only snap should return an edge where x1 == x2 EXPECT_EQ(result.edge.first.x(), result.edge.second.x()); } } TEST_F(SnapTest, SnapAtCenterReportsFarDistance) { makeTileGen(); TileVisibility vis; std::set layers; // Die area (0,0)-(100000,100000). At center the nearest edge is 50000 away. // Snap always finds the die area edge, but the distance should be large. auto result = tile_gen_->snapAt(50000, 50000, 10, 5, true, true, vis, layers); EXPECT_TRUE(result.found); EXPECT_GE(result.distance, 49000); } TEST_F(SnapTest, DieAreaEdgeSnap) { makeTileGen(); TileVisibility vis; std::set layers; // Die area is (0,0)→(100000,100000). Bottom edge is at y=0. // Query just above the bottom edge. auto result = tile_gen_->snapAt(50000, 50, 1000, 10, true, true, vis, layers); EXPECT_TRUE(result.found); // The bottom edge is horizontal at y=0. EXPECT_EQ(result.edge.first.y(), 0); EXPECT_EQ(result.edge.second.y(), 0); } TEST_F(SnapTest, VisibilityFilterHidesStdcells) { placeInst("BUF_X16", "buf1", 0, 0); makeTileGen(); TileVisibility vis; vis.stdcells = true; std::set layers; // With stdcells visible, should find instance edges near origin. auto result_visible = tile_gen_->snapAt(100, 100, 5000, 50, true, true, vis, layers); vis.stdcells = false; auto result_hidden = tile_gen_->snapAt(100, 100, 5000, 50, true, true, vis, layers); // Both may find die area edges, but the instance edges should be different. // At minimum, both calls should not crash. EXPECT_TRUE(result_visible.found); EXPECT_TRUE(result_hidden.found); } // ─── handleSnap request handler tests ──────────────────────────────────────── class SnapHandlerTest : public tst::Nangate45Fixture { protected: void SetUp() override { block_->setDieArea(odb::Rect(0, 0, 100000, 100000)); block_->setCoreArea(odb::Rect(0, 0, 100000, 100000)); placeInst("BUF_X16", "buf1", 0, 0); gen_ = std::make_shared( getDb(), /*sta=*/nullptr, getLogger()); tcl_eval_ = std::make_shared(/*interp=*/nullptr, getLogger()); handler_ = std::make_unique(gen_, tcl_eval_); } odb::dbInst* placeInst(const char* master_name, const char* inst_name, int x, int y) { odb::dbMaster* master = lib_->findMaster(master_name); EXPECT_NE(master, nullptr); odb::dbInst* inst = odb::dbInst::create(block_, master, inst_name); inst->setLocation(x, y); inst->setPlacementStatus(odb::dbPlacementStatus::PLACED); return inst; } std::shared_ptr gen_; std::shared_ptr tcl_eval_; std::unique_ptr handler_; }; TEST_F(SnapHandlerTest, SnapReturnsJson) { WebSocketRequest req; req.id = 100; req.type = WebSocketRequest::kSnap; req.json = parseObj( R"({"dbu_x":50000,"dbu_y":50,"radius":1000,"point_threshold":10,"horizontal":true,"vertical":true,"visible_layers":[]})"); auto resp = handler_->handleSnap(req); EXPECT_EQ(resp.id, 100u); EXPECT_EQ(resp.type, WebSocketResponse::kJson); // JSON std::string json = payloadStr(resp); EXPECT_NE(json.find("\"found\""), std::string::npos); } TEST_F(SnapHandlerTest, SnapFoundContainsEdge) { WebSocketRequest req; req.id = 101; req.type = WebSocketRequest::kSnap; // Query near die area bottom edge at y=0. req.json = parseObj( R"({"dbu_x":50000,"dbu_y":50,"radius":1000,"point_threshold":10,"horizontal":true,"vertical":true,"visible_layers":[]})"); auto resp = handler_->handleSnap(req); EXPECT_EQ(resp.type, WebSocketResponse::kJson); std::string json = payloadStr(resp); // Should find the die area bottom edge. EXPECT_NE(json.find("\"found\":true"), std::string::npos); EXPECT_NE(json.find("\"edge\""), std::string::npos); EXPECT_NE(json.find("\"is_point\""), std::string::npos); } TEST_F(SnapHandlerTest, SnapAlwaysFindsEdgeDueToChipBoundary) { WebSocketRequest req; req.id = 102; req.type = WebSocketRequest::kSnap; // Center of design -- die area edges are always checked so snap always // finds something (the closest die boundary). req.json = parseObj( R"({"dbu_x":50000,"dbu_y":50000,"radius":10,"point_threshold":5,"horizontal":true,"vertical":true,"visible_layers":[]})"); auto resp = handler_->handleSnap(req); EXPECT_EQ(resp.type, WebSocketResponse::kJson); std::string json = payloadStr(resp); EXPECT_NE(json.find("\"found\":true"), std::string::npos); EXPECT_NE(json.find("\"edge\""), std::string::npos); } TEST_F(SnapHandlerTest, SnapDispatchesCorrectly) { // Verify that dispatch_request routes SNAP type (even though SNAP goes // through SelectHandler, not dispatch_request -- just verify SNAP type // is recognized and doesn't error out through the general path). WebSocketRequest req; req.id = 103; req.type = WebSocketRequest::kSnap; req.json = parseObj( R"({"dbu_x":100,"dbu_y":100,"radius":5000,"point_threshold":50,"horizontal":true,"vertical":true,"visible_layers":[]})"); auto resp = handler_->handleSnap(req); EXPECT_EQ(resp.id, 103u); EXPECT_NE(resp.type, WebSocketResponse::kError); // not error } TEST_F(SnapHandlerTest, TechResponseIncludesDbuPerMicron) { // The TECH response should include dbu_per_micron which is needed by the // ruler for distance calculations. TileHandler tile_handler(gen_); SessionState state; WebSocketRequest req; req.id = 104; req.type = WebSocketRequest::kTech; auto resp = tile_handler.handleTile(req, state); EXPECT_EQ(resp.type, WebSocketResponse::kJson); std::string json = payloadStr(resp); EXPECT_NE(json.find("\"dbu_per_micron\""), std::string::npos); } } // namespace } // namespace web