diff --git a/api/root.go b/api/root.go index 1d425e8..0313183 100644 --- a/api/root.go +++ b/api/root.go @@ -28,6 +28,7 @@ func AddAPILayer(app *fiber.App) { hashAPI(app) timeAPI(app) propertiesAPI(app) + compareTextAPI(app) } func healthCheck(app *fiber.App) { diff --git a/api/text.go b/api/text.go new file mode 100644 index 0000000..00e958d --- /dev/null +++ b/api/text.go @@ -0,0 +1,33 @@ +package api + +import ( + "encoding/json" + "github.com/ahelmy/xdev/internal" + "github.com/gofiber/fiber/v3" +) + +const ( + TextsPath = "/text" +) + +type CompareRequest struct { + Text1 string `json:"text1"` + Text2 string `json:"text2"` + CheckLine bool `json:"check_line"` +} + +func compareTextAPI(app *fiber.App) { + app.Post(APIPrefix+TextsPath, func(c fiber.Ctx) error { + req := CompareRequest{} + err := json.Unmarshal(c.Request().Body(), &req) + + if err != nil { + return c.JSON(Response{Success: false, Message: err.Error()}) + } + diffs := internal.CompareText(req.Text1, req.Text2, req.CheckLine) + + res := Response{ + Success: true, Data: map[string]interface{}{"diffs": diffs}} + return c.JSON(res) + }) +} diff --git a/api/text_test.go b/api/text_test.go new file mode 100644 index 0000000..eca64c8 --- /dev/null +++ b/api/text_test.go @@ -0,0 +1,49 @@ +package api + +import ( + "bytes" + "encoding/json" + "github.com/gofiber/fiber/v3" + "github.com/stretchr/testify/assert" + "net/http" + "net/http/httptest" + "testing" +) + +func TestCompareTextAPI(t *testing.T) { + app := fiber.New() + compareTextAPI(app) + + t.Run("Valid Request", func(t *testing.T) { + compareRequest := CompareRequest{ + Text1: "text", + Text2: "text bro", + CheckLine: false, + } + reqBody, _ := json.Marshal(compareRequest) + + req := httptest.NewRequest(http.MethodPost, "/api/text", bytes.NewReader(reqBody)) + req.Header.Set("Content-Type", "application/json") + res, err := app.Test(req) + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, res.StatusCode) + + var response Response + err = json.NewDecoder(res.Body).Decode(&response) + assert.NoError(t, err) + assert.True(t, response.Success) + }) + + t.Run("JSON Unmarshal Error", func(t *testing.T) { + req := httptest.NewRequest(http.MethodPost, "/api/text", bytes.NewReader([]byte("invalid json"))) + req.Header.Set("Content-Type", "application/json") + resp, err := app.Test(req) + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, resp.StatusCode) + + var response Response + err = json.NewDecoder(resp.Body).Decode(&response) + assert.NoError(t, err) + assert.False(t, response.Success) + }) +} diff --git a/go.mod b/go.mod index 10ad16e..b93830a 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/google/uuid v1.6.0 github.com/itchyny/json2yaml v0.1.4 github.com/oklog/ulid/v2 v2.1.0 + github.com/sergi/go-diff v1.3.1 github.com/spf13/cobra v1.8.0 github.com/stretchr/testify v1.8.4 golang.org/x/crypto v0.18.0 diff --git a/go.sum b/go.sum index c13b46e..ab92cfb 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,7 @@ github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1 github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY= github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/gofiber/fiber/v3 v3.0.0-20240106164914-960b65258767 h1:3v7G15sjIVzo/HuqQ37veATuZkdYESgncM7oejXNphw= @@ -24,6 +25,7 @@ github.com/itchyny/json2yaml v0.1.4 h1:/pErVOXGG5iTyXHi/QKR4y3uzhLjGTEmmJIy97YT+ github.com/itchyny/json2yaml v0.1.4/go.mod h1:6iudhBZdarpjLFRNj+clWLAkGft+9uCcjAZYXUH9eGI= github.com/klauspost/compress v1.17.4 h1:Ej5ixsIri7BrIjBkRZLTo6ghwrEtHFk7ijlczPW4fZ4= github.com/klauspost/compress v1.17.4/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= @@ -46,10 +48,14 @@ github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/f github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8= +github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I= github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0= github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= @@ -65,7 +71,10 @@ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU= golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/text.go b/internal/text.go new file mode 100644 index 0000000..a47119a --- /dev/null +++ b/internal/text.go @@ -0,0 +1,9 @@ +package internal + +import "github.com/sergi/go-diff/diffmatchpatch" + +func CompareText(txt1 string, txt2 string, checkLines bool) string { + dmp := diffmatchpatch.New() + diffs := dmp.DiffMain(txt1, txt2, checkLines) + return dmp.DiffPrettyHtml(diffs) +} diff --git a/internal/text_test.go b/internal/text_test.go new file mode 100644 index 0000000..9573eb1 --- /dev/null +++ b/internal/text_test.go @@ -0,0 +1,47 @@ +package internal + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestCompareTexts(t *testing.T) { + tests := []struct { + name string + txt1 string + txt2 string + expected string + }{ + { + name: "Same texts", + txt1: "Hello, World", + txt2: "Hello, World", + expected: "Hello, World", + }, + { + name: "Texts with additional words", + txt1: "Hello World", + txt2: "Hello", + expected: "Hello World", + }, + { + name: "Texts with less words", + txt1: "Hello", + txt2: "Hello World", + expected: "Hello World", + }, + { + name: "Texts with different characters", + txt1: "Hello", + txt2: "Helli", + expected: "Helloi", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := CompareText(tt.txt1, tt.txt2, false) + assert.Equal(t, tt.expected, result) + }) + } +} diff --git a/server/server.go b/server/server.go index a4fdd7a..8737eed 100644 --- a/server/server.go +++ b/server/server.go @@ -18,21 +18,22 @@ import ( ) const ( - Prefix = "ui/" - APIPrefix = "/api" - MainLayout = Prefix + "layouts/main" - BasePath = "ui" - JSONPath = "/json" - YAMLPath = "/yaml" - JWTPath = "/jwt" - UUIDPath = "/uuid" - ULIDPath = "/ulid" - PasswordPath = "/password" - Base64Path = "/base64" - URLPath = "/url" - HashPath = "/hash" - TimePath = "/time" - Properties = "/properties" + Prefix = "ui/" + APIPrefix = "/api" + MainLayout = Prefix + "layouts/main" + BasePath = "ui" + JSONPath = "/json" + YAMLPath = "/yaml" + JWTPath = "/jwt" + UUIDPath = "/uuid" + ULIDPath = "/ulid" + PasswordPath = "/password" + Base64Path = "/base64" + URLPath = "/url" + HashPath = "/hash" + TimePath = "/time" + PropertiesPath = "/properties" + TextsPath = "/text" ) //go:embed ui/* @@ -150,7 +151,7 @@ func StartServer(port int32, isVerbose bool) { hashPage(app) timePage(app) propertiesPage(app) - + compareTextPage(app) log.Fatal(app.Listen(":"+strconv.FormatInt(int64(port), 10), fiber.ListenConfig{EnablePrefork: true})) } @@ -249,12 +250,20 @@ func timePage(app *fiber.App) { } func propertiesPage(app *fiber.App) { - app.Get(Properties, func(c fiber.Ctx) error { + app.Get(PropertiesPath, func(c fiber.Ctx) error { return c.Render(Prefix+"properties", newMap(map[string]any{ "Title": "Properties", }), MainLayout) }) +} +func compareTextPage(app *fiber.App) { + app.Get(TextsPath, func(c fiber.Ctx) error { + return c.Render(Prefix+"text", newMap(map[string]any{ + "Title": "Text BS", + }), MainLayout) + }) + } func defineResources(app *fiber.App) { diff --git a/server/server_test.go b/server/server_test.go index 0e11a44..23702bc 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -528,3 +528,21 @@ func TestPropertiesPage(t *testing.T) { } }) } + +func TestCompareTextPage(t *testing.T) { + app := newApp() + compareTextPage(app) + + t.Run("Test Compare Text Page", func(t *testing.T) { + // Create a test request to the "/text-comparer" route + req := httptest.NewRequest(http.MethodGet, "/text-comparer", nil) + resp, err := app.Test(req) + if err != nil { + t.Fatalf("Failed to send test request: %v", err) + } + // Check the response status code + if resp.StatusCode != http.StatusOK { + t.Errorf("Expected status code %d, but got %d", http.StatusOK, resp.StatusCode) + } + }) +} diff --git a/server/ui/index.html b/server/ui/index.html index 2f3a6fb..4eec793 100644 --- a/server/ui/index.html +++ b/server/ui/index.html @@ -128,5 +128,18 @@
Properties
+ +
+ +
+
+
Text BS
+

+ Find text differences.

+
+
+
+
+ \ No newline at end of file diff --git a/server/ui/partials/navbar.html b/server/ui/partials/navbar.html index 17f761a..c31450d 100644 --- a/server/ui/partials/navbar.html +++ b/server/ui/partials/navbar.html @@ -43,6 +43,9 @@ + diff --git a/server/ui/text.html b/server/ui/text.html new file mode 100644 index 0000000..8968b38 --- /dev/null +++ b/server/ui/text.html @@ -0,0 +1,231 @@ + + + + + + + + + + + + + + + + +
+
+
+
+
+
+ Text 1 +
+
+ +
+
+
+
+
+
+ Text 2 +
+
+ +
+
+
+ +
+
+
+ +
+
+ + + + +
+
+
+ + + + +
+
+
 
+
+
+
+
+
+ Result +
+
+
+
+
+
+
+
+
+ + \ No newline at end of file