@@ -899,6 +899,105 @@ def self.configuration=(config)
899899
900900 # RSC utility method tests moved to react_on_rails_pro/spec/react_on_rails_pro/utils_spec.rb
901901
902+ describe ".normalize_to_relative_path" do
903+ let ( :rails_root ) { "/app" }
904+
905+ before do
906+ allow ( Rails ) . to receive ( :root ) . and_return ( Pathname . new ( rails_root ) )
907+ end
908+
909+ context "with absolute path containing Rails.root" do
910+ it "removes Rails.root prefix" do
911+ expect ( described_class . normalize_to_relative_path ( "/app/ssr-generated" ) )
912+ . to eq ( "ssr-generated" )
913+ end
914+
915+ it "handles paths with trailing slash in Rails.root" do
916+ expect ( described_class . normalize_to_relative_path ( "/app/ssr-generated/nested" ) )
917+ . to eq ( "ssr-generated/nested" )
918+ end
919+
920+ it "removes leading slash after Rails.root" do
921+ allow ( Rails ) . to receive ( :root ) . and_return ( Pathname . new ( "/app/" ) )
922+ expect ( described_class . normalize_to_relative_path ( "/app/ssr-generated" ) )
923+ . to eq ( "ssr-generated" )
924+ end
925+ end
926+
927+ context "with Pathname object" do
928+ it "converts Pathname to relative string" do
929+ path = Pathname . new ( "/app/ssr-generated" )
930+ expect ( described_class . normalize_to_relative_path ( path ) )
931+ . to eq ( "ssr-generated" )
932+ end
933+
934+ it "handles already relative Pathname" do
935+ path = Pathname . new ( "ssr-generated" )
936+ expect ( described_class . normalize_to_relative_path ( path ) )
937+ . to eq ( "ssr-generated" )
938+ end
939+ end
940+
941+ context "with already relative path" do
942+ it "returns the path unchanged" do
943+ expect ( described_class . normalize_to_relative_path ( "ssr-generated" ) )
944+ . to eq ( "ssr-generated" )
945+ end
946+
947+ it "handles nested relative paths" do
948+ expect ( described_class . normalize_to_relative_path ( "config/ssr-generated" ) )
949+ . to eq ( "config/ssr-generated" )
950+ end
951+
952+ it "handles paths with . prefix" do
953+ expect ( described_class . normalize_to_relative_path ( "./ssr-generated" ) )
954+ . to eq ( "./ssr-generated" )
955+ end
956+ end
957+
958+ context "with nil path" do
959+ it "returns nil" do
960+ expect ( described_class . normalize_to_relative_path ( nil ) ) . to be_nil
961+ end
962+ end
963+
964+ context "with absolute path not containing Rails.root" do
965+ it "returns path unchanged" do
966+ expect ( described_class . normalize_to_relative_path ( "/other/path/ssr-generated" ) )
967+ . to eq ( "/other/path/ssr-generated" )
968+ end
969+ end
970+
971+ context "with path containing Rails.root as substring" do
972+ it "only removes Rails.root prefix, not substring matches" do
973+ allow ( Rails ) . to receive ( :root ) . and_return ( Pathname . new ( "/app" ) )
974+ # Path contains "/app" but not as prefix
975+ expect ( described_class . normalize_to_relative_path ( "/myapp/ssr-generated" ) )
976+ . to eq ( "/myapp/ssr-generated" )
977+ end
978+ end
979+
980+ context "with complex Rails.root paths" do
981+ it "handles Rails.root with special characters" do
982+ allow ( Rails ) . to receive ( :root ) . and_return ( Pathname . new ( "/home/user/my-app" ) )
983+ expect ( described_class . normalize_to_relative_path ( "/home/user/my-app/ssr-generated" ) )
984+ . to eq ( "ssr-generated" )
985+ end
986+
987+ it "handles Rails.root with spaces" do
988+ allow ( Rails ) . to receive ( :root ) . and_return ( Pathname . new ( "/home/user/my app" ) )
989+ expect ( described_class . normalize_to_relative_path ( "/home/user/my app/ssr-generated" ) )
990+ . to eq ( "ssr-generated" )
991+ end
992+
993+ it "handles Rails.root with dots" do
994+ allow ( Rails ) . to receive ( :root ) . and_return ( Pathname . new ( "/home/user/app.v2" ) )
995+ expect ( described_class . normalize_to_relative_path ( "/home/user/app.v2/ssr-generated" ) )
996+ . to eq ( "ssr-generated" )
997+ end
998+ end
999+ end
1000+
9021001 describe ".normalize_immediate_hydration" do
9031002 context "with Pro license" do
9041003 before do
0 commit comments