From 18c2af792826ea4ad7522f44408eddb8a7022fa8 Mon Sep 17 00:00:00 2001 From: Kristian Borgwarth <10348902@pm.me> Date: Thu, 2 Apr 2026 11:29:10 +0200 Subject: [PATCH 1/5] ref(server): simplified server --- cmd/main.go | 1 + core/rpc/models.go | 2 +- core/server/server.go | 31 +++++++++---------- .../create_note_handler_test.go | 25 +++++++++------ test/test_integration/main_test.go | 30 +++++++++++++++--- 5 files changed, 57 insertions(+), 32 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index d2efd75..9848439 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -1,6 +1,7 @@ package main import ( + "database/sql" "log/slog" "os" diff --git a/core/rpc/models.go b/core/rpc/models.go index 3dcb7f1..e8ddfb3 100644 --- a/core/rpc/models.go +++ b/core/rpc/models.go @@ -15,7 +15,7 @@ type Request struct { type Response struct { Jsonrpc string `json:"jsonrpc"` ID *int `json:"id"` - Result any `json:"result,omitempty"` + Result json.RawMessage `json:"result,omitempty"` Error *Error `json:"error,omitempty"` } diff --git a/core/server/server.go b/core/server/server.go index a9bf558..6aed220 100644 --- a/core/server/server.go +++ b/core/server/server.go @@ -1,17 +1,20 @@ -// Package server contains the server running the JSON-RPC 2.0 Protocol. +// Package server contains the server running the JSON-RPC 2.0 Protocol. package server import ( "bufio" "context" + "database/sql" "encoding/json" "fmt" "io" + "github.com/KristianJBorgwarth/dendrite.daemon/core/handlers" "github.com/KristianJBorgwarth/dendrite.daemon/core/rpc" ) type Server struct { + Db *sql.DB handlers map[string]handlers.Handler } @@ -21,6 +24,10 @@ func NewServer() *Server { } } +func (s *Server) InitDatabase(db *sql.DB) { + s.Db = db +} + func (s *Server) Register(method string, handler handlers.Handler) { s.handlers[method] = handler } @@ -51,31 +58,21 @@ func (s *Server) handle(w io.Writer, req rpc.Request) { handler, ok := s.handlers[req.Method] if !ok { - s.respond(w, req.ID, nil, &rpc.Error{ - Code: -32601, - Message: "method not found", + s.write(w, rpc.Response{ + Jsonrpc: "2.0", + ID: req.ID, + Error: &rpc.Error{Code: -32601, Message: "method not found"}, }) return } - result, err := handler.Handle(ctx, req.Params) + result := handler.Handle(ctx, req.Params) if req.ID == nil { return } - s.respond(w, req.ID, result, err) -} - -func (s *Server) respond(w io.Writer, id *int, result any, err *rpc.Error) { - resp := rpc.Response{ - Jsonrpc: "2.0", - ID: id, - Result: result, - Error: err, - } - - s.write(w, resp) + s.write(w, *result) } func (s *Server) write(w io.Writer, resp rpc.Response) { diff --git a/test/test_integration/create_note_handler_test.go b/test/test_integration/create_note_handler_test.go index dd4ae2f..97f3348 100644 --- a/test/test_integration/create_note_handler_test.go +++ b/test/test_integration/create_note_handler_test.go @@ -1,7 +1,6 @@ package integration_test import ( - "database/sql" "encoding/json" "testing" @@ -11,14 +10,7 @@ import ( func TestCreateNoteHandlerOnSucess(t *testing.T) { // Arrange - // TODO: move to test vars and main_test.go - db, err := sql.Open("sqlite", DbPath) - if err != nil { - t.Fatalf("failed to open database: %v", err) - } - defer db.Close() - - uow := repositories.NewUnitOfWork(db) + uow := repositories.NewUnitOfWork(Fixture.Db) handler := handlers.NewCreateNoteHandler(uow) @@ -38,5 +30,18 @@ func TestCreateNoteHandlerOnSucess(t *testing.T) { request := CreateTestRequest("createNote", 1, requestParamsBytes) // Act - response, rpcError = handler.Handle(TestContext, request.Params) + response := handler.Handle(Fixture.TestContext, request.Params) + + // Assert + if response.Error != nil { + t.Fatalf("expected no error, got: %v", response.Error) + } + + if response.Result == nil { + t.Fatal("expected result, got nil") + } + + if *response.ID != 1 { + t.Fatal("expected non-empty ID in response result") + } } diff --git a/test/test_integration/main_test.go b/test/test_integration/main_test.go index 9b8515e..14668b8 100644 --- a/test/test_integration/main_test.go +++ b/test/test_integration/main_test.go @@ -2,6 +2,7 @@ package integration_test import ( "context" + "database/sql" "encoding/json" "os" "testing" @@ -10,19 +11,38 @@ import ( "github.com/KristianJBorgwarth/dendrite.daemon/persistence" ) -var DbPath string = os.TempDir() + "/dendrite_test_vault" -var TestContext = context.Background() +type TestFixture struct { + Db *sql.DB + DbPath string + TestContext context.Context +} + +func NewTestFixture() (*TestFixture, error) { + dbPath := os.TempDir() + "/dendrite_test_vault" + db, err := sql.Open("sqlite", dbPath) + if err != nil { + return nil, err + } + + return &TestFixture{ + Db: db, + DbPath: dbPath, + TestContext: context.Background(), + }, nil +} + +var Fixture, err = NewTestFixture() func TestMain(m *testing.M) { - err := persistence.InitializeIndex(DbPath) + err := persistence.InitializeIndex(Fixture.DbPath) if err != nil { panic(err) } code := m.Run() - os.RemoveAll(DbPath) + os.RemoveAll(Fixture.DbPath) os.Exit(code) } @@ -35,3 +55,5 @@ func CreateTestRequest(method string, ID int, params json.RawMessage) *rpc.Reque Params: params, } } + + From 7ff9788d958e33c6e76daa047fafdf0b1448f381 Mon Sep 17 00:00:00 2001 From: Kristian Borgwarth <10348902@pm.me> Date: Thu, 2 Apr 2026 13:00:49 +0200 Subject: [PATCH 2/5] ref(tags): improved frontmatter parse use --- cmd/main.go | 1 - core/files/doc.go | 2 + core/files/file_handler.go | 19 +++++++ core/frontmatter/parser.go | 14 ++++-- core/handlers/create_note_handler.go | 37 +++++++------- core/handlers/handler.go | 4 +- core/handlers/initialize_handler.go | 18 ++----- core/server/server.go | 42 ++++++++++------ core/template/templater.go | 17 +++++++ .../create_note_handler_test.go | 16 +++--- test/test_integration/main_test.go | 50 ++++++++++--------- 11 files changed, 131 insertions(+), 89 deletions(-) create mode 100644 core/files/doc.go create mode 100644 core/files/file_handler.go create mode 100644 core/template/templater.go diff --git a/cmd/main.go b/cmd/main.go index 9848439..d2efd75 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -1,7 +1,6 @@ package main import ( - "database/sql" "log/slog" "os" diff --git a/core/files/doc.go b/core/files/doc.go new file mode 100644 index 0000000..a4727ba --- /dev/null +++ b/core/files/doc.go @@ -0,0 +1,2 @@ +// Package files provides utilities for working with files and directories. +package files diff --git a/core/files/file_handler.go b/core/files/file_handler.go new file mode 100644 index 0000000..3ba059b --- /dev/null +++ b/core/files/file_handler.go @@ -0,0 +1,19 @@ +package files + +import "os" + +func WriteToFile(path string, data []byte) (filePath string, err error) { + if checkIfFileExists(path) { + return path, nil + } + err = os.WriteFile(path, data, 0o644) + if err != nil { + return "", err + } + return path, nil +} + +func checkIfFileExists(path string) bool { + _, err := os.Stat(path) + return err == nil +} diff --git a/core/frontmatter/parser.go b/core/frontmatter/parser.go index 073d394..cf346a7 100644 --- a/core/frontmatter/parser.go +++ b/core/frontmatter/parser.go @@ -16,7 +16,7 @@ type FrontMatter struct { Author string } -func ParseFrontMatter(r io.Reader) (map[string]string, error) { +func parseFrontMatter(r io.Reader) (map[string]string, error) { scanner := bufio.NewScanner(r) frontMatter := make(map[string]string) @@ -41,13 +41,19 @@ func ParseFrontMatter(r io.Reader) (map[string]string, error) { return frontMatter, nil } -func ExtractTags(frontMatter map[string]string) ([]string, error) { +func ExtractTags(file []byte ) ([]string, error) { + r := bytes.NewReader(file) + frontMatter, err := parseFrontMatter(r) + if err != nil { + return nil, err + } tagsStr, ok := frontMatter["tags"] if !ok { - return nil, errors.New("tags not found in front matter") + return nil, nil } + var tags []string - err := json.Unmarshal([]byte(tagsStr), &tags) + err = json.Unmarshal([]byte(tagsStr), &tags) if err != nil { return nil, err } diff --git a/core/handlers/create_note_handler.go b/core/handlers/create_note_handler.go index 841a8ec..c275ecb 100644 --- a/core/handlers/create_note_handler.go +++ b/core/handlers/create_note_handler.go @@ -7,8 +7,8 @@ import ( "encoding/json" "os" + "github.com/KristianJBorgwarth/dendrite.daemon/core/files" "github.com/KristianJBorgwarth/dendrite.daemon/core/frontmatter" - "github.com/KristianJBorgwarth/dendrite.daemon/core/rpc" "github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories" ) @@ -27,26 +27,21 @@ func NewCreateNoteHandler(uow *repositories.UnitOfWork) *CreateNoteHandler { return &CreateNoteHandler{uow: uow} } -func (h CreateNoteHandler) Handle(ctx context.Context, params []byte) (*rpc.Response) { +func (h CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (any, error) { var cmd createNoteCommand - if err := json.Unmarshal(params, &cmd); err != nil { - return nil, h.ReturnError(err) + if err := json.Unmarshal(raw, &cmd); err != nil { + return nil, err } - data, err := os.ReadFile(cmd.TemplatePath) + data, err := h.getTemplate(cmd.TemplatePath) if err != nil { - return nil, h.ReturnError(err) + return nil, err } - feMatter, err := frontmatter.ParseFrontMatter(bytes.NewReader(data)) + tags, err := frontmatter.ExtractTags(data) if err != nil { - return nil, h.ReturnError(err) - } - - tags, err := frontmatter.ExtractTags(feMatter) - if err != nil { - return nil, h.ReturnError(err) + return nil, err } err = h.uow.Execute(ctx, func(tx *sql.Tx) error { @@ -63,14 +58,20 @@ func (h CreateNoteHandler) Handle(ctx context.Context, params []byte) (*rpc.Resp return nil }) - if err != nil { - return nil, h.ReturnError(err) + return nil, err } - return &rpc.Response{Jsonrpc: "2.0", Result: cmd.Path}, nil + return cmd.Path, nil } -func (h *CreateNoteHandler) ReturnError(err error) *rpc.Error { - return &rpc.Error{Code: -1, Message: err.Error()} +func (h *CreateNoteHandler) getTemplate(templatePath string) ([]byte, error) { + if templatePath == "" { + return nil, nil + } + data, err := os.ReadFile(templatePath) + if err != nil { + return nil, err + } + return data, nil } diff --git a/core/handlers/handler.go b/core/handlers/handler.go index cde1d26..001c62e 100644 --- a/core/handlers/handler.go +++ b/core/handlers/handler.go @@ -3,10 +3,8 @@ package handlers import ( "context" "encoding/json" - - "github.com/KristianJBorgwarth/dendrite.daemon/core/rpc" ) type Handler interface { - Handle(ctx context.Context, raw json.RawMessage) (*rpc.Response) + Handle(ctx context.Context, raw json.RawMessage) (any, error) } diff --git a/core/handlers/initialize_handler.go b/core/handlers/initialize_handler.go index a84003d..6e33f12 100644 --- a/core/handlers/initialize_handler.go +++ b/core/handlers/initialize_handler.go @@ -3,36 +3,26 @@ package handlers import ( "context" "encoding/json" - - "github.com/KristianJBorgwarth/dendrite.daemon/core/rpc" "github.com/KristianJBorgwarth/dendrite.daemon/persistence" ) type initializeCommand struct { - VaultPath string `json:"vaultPath"` + VaultPath string `json:"vaultPath"` } type InitializeHandler struct{} -func (h InitializeHandler) Handle(ctx context.Context, raw json.RawMessage) (any, *rpc.Error) { +func (h InitializeHandler) Handle(ctx context.Context, raw json.RawMessage) (any, error) { var params initializeCommand if err := json.Unmarshal(raw, ¶ms); err != nil { - return nil, &rpc.Error{ - Code: -32602, - Message: "invalid params: " + err.Error(), - } + return nil, err } - err := persistence.InitializeIndex(params.VaultPath) if err != nil { - return nil, &rpc.Error{ - Code: -1, - Message: "failed to initialize index: " + err.Error(), - } + return nil, err } return nil, nil } - diff --git a/core/server/server.go b/core/server/server.go index 6aed220..646036a 100644 --- a/core/server/server.go +++ b/core/server/server.go @@ -4,7 +4,6 @@ package server import ( "bufio" "context" - "database/sql" "encoding/json" "fmt" "io" @@ -14,7 +13,6 @@ import ( ) type Server struct { - Db *sql.DB handlers map[string]handlers.Handler } @@ -24,10 +22,6 @@ func NewServer() *Server { } } -func (s *Server) InitDatabase(db *sql.DB) { - s.Db = db -} - func (s *Server) Register(method string, handler handlers.Handler) { s.handlers[method] = handler } @@ -58,24 +52,42 @@ func (s *Server) handle(w io.Writer, req rpc.Request) { handler, ok := s.handlers[req.Method] if !ok { - s.write(w, rpc.Response{ - Jsonrpc: "2.0", - ID: req.ID, - Error: &rpc.Error{Code: -32601, Message: "method not found"}, + s.respond(w, req.ID, nil, &rpc.Error{ + Code: -32601, + Message: "method not found", }) - return -} + return + } - result := handler.Handle(ctx, req.Params) + result, err := handler.Handle(ctx, req.Params) + if err != nil { + s.respond(w, req.ID, nil, &rpc.Error{ + Code: -32000, + Message: err.Error(), + }) + return + } if req.ID == nil { return } - s.write(w, *result) + s.respond(w, req.ID, result, nil) +} + +func (s *Server) respond(w io.Writer, id *int, result any, err *rpc.Error) { + resultJSON, _ := json.Marshal(result) + resp := rpc.Response{ + Jsonrpc: "2.0", + ID: id, + Result: resultJSON, + Error: err, + } + + s.write(w, resp) } func (s *Server) write(w io.Writer, resp rpc.Response) { data, _ := json.Marshal(resp) - fmt.Fprintln(w, string(data)) + fmt.Fprintln(w, string(data)) } diff --git a/core/template/templater.go b/core/template/templater.go new file mode 100644 index 0000000..40576a7 --- /dev/null +++ b/core/template/templater.go @@ -0,0 +1,17 @@ +package template + +import ( + "os" +) + + +func GenerateTemplate(templatePath string) ([]byte, error) { + if templatePath == "" { + return nil, nil + } + data, err := os.ReadFile(templatePath) + if err != nil { + return nil, err + } + return data, nil +} diff --git a/test/test_integration/create_note_handler_test.go b/test/test_integration/create_note_handler_test.go index 97f3348..e210a42 100644 --- a/test/test_integration/create_note_handler_test.go +++ b/test/test_integration/create_note_handler_test.go @@ -10,7 +10,7 @@ import ( func TestCreateNoteHandlerOnSucess(t *testing.T) { // Arrange - uow := repositories.NewUnitOfWork(Fixture.Db) + uow := repositories.NewUnitOfWork(Fixture.DB) handler := handlers.NewCreateNoteHandler(uow) @@ -30,18 +30,14 @@ func TestCreateNoteHandlerOnSucess(t *testing.T) { request := CreateTestRequest("createNote", 1, requestParamsBytes) // Act - response := handler.Handle(Fixture.TestContext, request.Params) + response, err := handler.Handle(Fixture.TestContext, request.Params) // Assert - if response.Error != nil { - t.Fatalf("expected no error, got: %v", response.Error) + if err != nil { + t.Fatalf("handler returned an error: %v", err) } - if response.Result == nil { - t.Fatal("expected result, got nil") - } - - if *response.ID != 1 { - t.Fatal("expected non-empty ID in response result") + if response != nil { + t.Fatalf("expected response to be nil, got: %v", response) } } diff --git a/test/test_integration/main_test.go b/test/test_integration/main_test.go index 14668b8..363ed8f 100644 --- a/test/test_integration/main_test.go +++ b/test/test_integration/main_test.go @@ -5,44 +5,48 @@ import ( "database/sql" "encoding/json" "os" + "path/filepath" "testing" "github.com/KristianJBorgwarth/dendrite.daemon/core/rpc" "github.com/KristianJBorgwarth/dendrite.daemon/persistence" + _ "modernc.org/sqlite" ) -type TestFixture struct { - Db *sql.DB - DbPath string +type DBFixture struct { + DB *sql.DB + DBPath string TestContext context.Context } -func NewTestFixture() (*TestFixture, error) { - dbPath := os.TempDir() + "/dendrite_test_vault" - db, err := sql.Open("sqlite", dbPath) - if err != nil { - return nil, err - } +func NewDBFixture() *DBFixture { + vaultPath := os.TempDir() - return &TestFixture{ - Db: db, - DbPath: dbPath, - TestContext: context.Background(), - }, nil -} - -var Fixture, err = NewTestFixture() - -func TestMain(m *testing.M) { - - err := persistence.InitializeIndex(Fixture.DbPath) + err := persistence.InitializeIndex(vaultPath) if err != nil { panic(err) } + dbPath := filepath.Join(os.TempDir(), ".index", "index.db") + + db, err := sql.Open("sqlite", dbPath) + if err != nil { + panic(err) + } + + return &DBFixture{ + DB: db, + DBPath: dbPath, + TestContext: context.Background(), + } +} + +var Fixture = NewDBFixture() + +func TestMain(m *testing.M) { code := m.Run() - os.RemoveAll(Fixture.DbPath) + os.RemoveAll(Fixture.DBPath) os.Exit(code) } @@ -55,5 +59,3 @@ func CreateTestRequest(method string, ID int, params json.RawMessage) *rpc.Reque Params: params, } } - - From a1f50489f697632f698ccc0e835f4a38976b320c Mon Sep 17 00:00:00 2001 From: Kristian Borgwarth <10348902@pm.me> Date: Thu, 2 Apr 2026 13:01:18 +0200 Subject: [PATCH 3/5] sm(imports): removed unused imports --- core/handlers/create_note_handler.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/core/handlers/create_note_handler.go b/core/handlers/create_note_handler.go index c275ecb..45eadd4 100644 --- a/core/handlers/create_note_handler.go +++ b/core/handlers/create_note_handler.go @@ -1,13 +1,11 @@ package handlers import ( - "bytes" "context" "database/sql" "encoding/json" "os" - "github.com/KristianJBorgwarth/dendrite.daemon/core/files" "github.com/KristianJBorgwarth/dendrite.daemon/core/frontmatter" "github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories" ) From 8cc8ea77912df9d97084b428ac44c4af0eff1aa7 Mon Sep 17 00:00:00 2001 From: Kristian Borgwarth <10348902@pm.me> Date: Thu, 2 Apr 2026 16:38:05 +0200 Subject: [PATCH 4/5] fix(fronmatter): use pkg instead of shit --- core/frontmatter/parser.go | 69 ++++++++++------------------ core/handlers/create_note_handler.go | 2 +- go.mod | 9 +++- go.sum | 40 ++++++++++++++++ test/test_unit/frontmatter_test.go | 52 ++++++--------------- 5 files changed, 88 insertions(+), 84 deletions(-) diff --git a/core/frontmatter/parser.go b/core/frontmatter/parser.go index cf346a7..ea834c9 100644 --- a/core/frontmatter/parser.go +++ b/core/frontmatter/parser.go @@ -1,61 +1,42 @@ package frontmatter import ( - "bufio" "bytes" - "encoding/json" "errors" - "io" + "gopkg.in/yaml.v3" ) type FrontMatter struct { - Title string - Tags []string - Created string - Updated string - Author string + Title string `yaml:"title"` + Tags []string `yaml:"tags"` + Created string `yaml:"created"` + Updated string `yaml:"updated"` + Author string `yaml:"author"` } -func parseFrontMatter(r io.Reader) (map[string]string, error) { - scanner := bufio.NewScanner(r) - frontMatter := make(map[string]string) - - for scanner.Scan() { - line := scanner.Text() - if line == "---" { - break - } - parts := bytes.SplitN([]byte(line), []byte(":"), 2) - if len(parts) != 2 { - return nil, errors.New("invalid front matter format") - } - key := string(bytes.TrimSpace(parts[0])) - value := string(bytes.TrimSpace(parts[1])) - frontMatter[key] = value - } - - if err := scanner.Err(); err != nil { - return nil, err - } - - return frontMatter, nil -} - -func ExtractTags(file []byte ) ([]string, error) { - r := bytes.NewReader(file) - frontMatter, err := parseFrontMatter(r) +func ParseTags(file []byte) ([]string, error) { + fm, err := parseFrontMatter(file) if err != nil { return nil, err } - tagsStr, ok := frontMatter["tags"] - if !ok { - return nil, nil + return fm.Tags, nil +} + +func parseFrontMatter(file []byte) (*FrontMatter, error) { + content := bytes.TrimSpace(file) + if bytes.HasPrefix(content, []byte("---")) { + content = bytes.TrimSpace(content[3:]) + } else { + return nil, errors.New("missing front matter delimiter") } - - var tags []string - err = json.Unmarshal([]byte(tagsStr), &tags) - if err != nil { + + if idx := bytes.Index(content, []byte("---")); idx != -1 { + content = bytes.TrimSpace(content[:idx]) + } + + var fm FrontMatter + if err := yaml.Unmarshal(content, &fm); err != nil { return nil, err } - return tags, nil + return &fm, nil } diff --git a/core/handlers/create_note_handler.go b/core/handlers/create_note_handler.go index 45eadd4..1fe121d 100644 --- a/core/handlers/create_note_handler.go +++ b/core/handlers/create_note_handler.go @@ -37,7 +37,7 @@ func (h CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (any return nil, err } - tags, err := frontmatter.ExtractTags(data) + tags, err := frontmatter.ParseTags(data) if err != nil { return nil, err } diff --git a/go.mod b/go.mod index ceb31eb..32bea65 100644 --- a/go.mod +++ b/go.mod @@ -3,14 +3,21 @@ module github.com/KristianJBorgwarth/dendrite.daemon go 1.26 require ( + github.com/stretchr/testify v1.11.1 + modernc.org/sqlite v1.47.0 +) + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect github.com/dustin/go-humanize v1.0.1 // indirect github.com/google/uuid v1.6.0 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/ncruces/go-strftime v1.0.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect golang.org/x/sys v0.42.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect modernc.org/libc v1.70.0 // indirect modernc.org/mathutil v1.7.1 // indirect modernc.org/memory v1.11.0 // indirect - modernc.org/sqlite v1.47.0 // indirect ) diff --git a/go.sum b/go.sum index 988cc37..7508947 100644 --- a/go.sum +++ b/go.sum @@ -1,21 +1,61 @@ +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/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= +github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e h1:ijClszYn+mADRFY17kjQEVQ1XRhq2/JR1M3sGqeJoxs= +github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e/go.mod h1:boTsfXsheKC2y+lKOCMpSfarhxDeIzfZG1jqGcPl3cA= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k= +github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/ncruces/go-strftime v1.0.0 h1:HMFp8mLCTPp341M/ZnA4qaf7ZlsbTc+miZjCLOFAw7w= github.com/ncruces/go-strftime v1.0.0/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= +github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= +github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= +golang.org/x/mod v0.33.0 h1:tHFzIWbBifEmbwtGz65eaWyGiGZatSrT9prnU8DbVL8= +golang.org/x/mod v0.33.0/go.mod h1:swjeQEj+6r7fODbD2cqrnje9PnziFuw4bmLbBZFrQ5w= +golang.org/x/sync v0.19.0 h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4= +golang.org/x/sync v0.19.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.42.0 h1:omrd2nAlyT5ESRdCLYdm3+fMfNFE/+Rf4bDIQImRJeo= golang.org/x/sys v0.42.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= +golang.org/x/tools v0.42.0 h1:uNgphsn75Tdz5Ji2q36v/nsFSfR/9BRFvqhGBaJGd5k= +golang.org/x/tools v0.42.0/go.mod h1:Ma6lCIwGZvHK6XtgbswSoWroEkhugApmsXyrUmBhfr0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +modernc.org/cc/v4 v4.27.1 h1:9W30zRlYrefrDV2JE2O8VDtJ1yPGownxciz5rrbQZis= +modernc.org/cc/v4 v4.27.1/go.mod h1:uVtb5OGqUKpoLWhqwNQo/8LwvoiEBLvZXIQ/SmO6mL0= +modernc.org/ccgo/v4 v4.32.0 h1:hjG66bI/kqIPX1b2yT6fr/jt+QedtP2fqojG2VrFuVw= +modernc.org/ccgo/v4 v4.32.0/go.mod h1:6F08EBCx5uQc38kMGl+0Nm0oWczoo1c7cgpzEry7Uc0= +modernc.org/fileutil v1.4.0 h1:j6ZzNTftVS054gi281TyLjHPp6CPHr2KCxEXjEbD6SM= +modernc.org/fileutil v1.4.0/go.mod h1:EqdKFDxiByqxLk8ozOxObDSfcVOv/54xDs/DUHdvCUU= +modernc.org/gc/v2 v2.6.5 h1:nyqdV8q46KvTpZlsw66kWqwXRHdjIlJOhG6kxiV/9xI= +modernc.org/gc/v2 v2.6.5/go.mod h1:YgIahr1ypgfe7chRuJi2gD7DBQiKSLMPgBQe9oIiito= +modernc.org/gc/v3 v3.1.2 h1:ZtDCnhonXSZexk/AYsegNRV1lJGgaNZJuKjJSWKyEqo= +modernc.org/gc/v3 v3.1.2/go.mod h1:HFK/6AGESC7Ex+EZJhJ2Gni6cTaYpSMmU/cT9RmlfYY= +modernc.org/goabi0 v0.2.0 h1:HvEowk7LxcPd0eq6mVOAEMai46V+i7Jrj13t4AzuNks= +modernc.org/goabi0 v0.2.0/go.mod h1:CEFRnnJhKvWT1c1JTI3Avm+tgOWbkOu5oPA8eH8LnMI= modernc.org/libc v1.70.0 h1:U58NawXqXbgpZ/dcdS9kMshu08aiA6b7gusEusqzNkw= modernc.org/libc v1.70.0/go.mod h1:OVmxFGP1CI/Z4L3E0Q3Mf1PDE0BucwMkcXjjLntvHJo= modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU= modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg= modernc.org/memory v1.11.0 h1:o4QC8aMQzmcwCK3t3Ux/ZHmwFPzE6hf2Y5LbkRs+hbI= modernc.org/memory v1.11.0/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw= +modernc.org/opt v0.1.4 h1:2kNGMRiUjrp4LcaPuLY2PzUfqM/w9N23quVwhKt5Qm8= +modernc.org/opt v0.1.4/go.mod h1:03fq9lsNfvkYSfxrfUhZCWPk1lm4cq4N+Bh//bEtgns= +modernc.org/sortutil v1.2.1 h1:+xyoGf15mM3NMlPDnFqrteY07klSFxLElE2PVuWIJ7w= +modernc.org/sortutil v1.2.1/go.mod h1:7ZI3a3REbai7gzCLcotuw9AC4VZVpYMjDzETGsSMqJE= modernc.org/sqlite v1.47.0 h1:R1XyaNpoW4Et9yly+I2EeX7pBza/w+pmYee/0HJDyKk= modernc.org/sqlite v1.47.0/go.mod h1:hWjRO6Tj/5Ik8ieqxQybiEOUXy0NJFNp2tpvVpKlvig= +modernc.org/strutil v1.2.1 h1:UneZBkQA+DX2Rp35KcM69cSsNES9ly8mQWD71HKlOA0= +modernc.org/strutil v1.2.1/go.mod h1:EHkiggD70koQxjVdSBM3JKM7k6L0FbGE5eymy9i3B9A= +modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y= +modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM= diff --git a/test/test_unit/frontmatter_test.go b/test/test_unit/frontmatter_test.go index 858eb5f..b926e96 100644 --- a/test/test_unit/frontmatter_test.go +++ b/test/test_unit/frontmatter_test.go @@ -1,56 +1,32 @@ package frontmatter_test import ( - "strings" "testing" "github.com/KristianJBorgwarth/dendrite.daemon/core/frontmatter" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) -func TestParseFrontMatter(t *testing.T) { - input := `title: My Note -template: default +func TestParseTags_ValidFrontMatter_ReturnsTags(t *testing.T) { + input := `--- +title: My Note tags: ["test", "note"] --- This is the content of the note.` - expected := map[string]string{ - "title": "My Note", - "template": "default", - "tags": `["test", "note"]`, - } + result, err := frontmatter.ParseTags([]byte(input)) - result, err := frontmatter.ParseFrontMatter(strings.NewReader(input)) - if err != nil { - t.Fatalf("unexpected error: %v", err) - } - - for key, expectedValue := range expected { - if value, ok := result[key]; !ok || value != expectedValue { - t.Errorf("expected %s to be %s, got %s", key, expectedValue, value) - } - } + require.NoError(t, err) + assert.Equal(t, []string{"test", "note"}, result) } -func TestExtractTags(t *testing.T) { - frontMatter := map[string]string{ - "tags": `["test", "note"]`, - } +func TestParseTags_MissingDelimiter_ReturnsError(t *testing.T) { + input := `title: My Note +tags: ["test", "note"] +This is the content of the note.` - expected := []string{"test", "note"} + _, err := frontmatter.ParseTags([]byte(input)) - result, err := frontmatter.ExtractTags(frontMatter) - if err != nil { - t.Fatalf("unexpected error: %v", err) - } - - if len(result) != len(expected) { - t.Fatalf("expected %d tags, got %d", len(expected), len(result)) - } - - for i, expectedTag := range expected { - if result[i] != expectedTag { - t.Errorf("expected tag %d to be %s, got %s", i, expectedTag, result[i]) - } - } + assert.ErrorContains(t, err, "missing front matter delimiter") } From 7cdfa6237a14fd00f07ffef7190a3c35c8c25ebe Mon Sep 17 00:00:00 2001 From: Kristian Borgwarth <10348902@pm.me> Date: Thu, 2 Apr 2026 17:13:14 +0200 Subject: [PATCH 5/5] sm(mod): direct require go.mod --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 32bea65..1254b4e 100644 --- a/go.mod +++ b/go.mod @@ -16,7 +16,7 @@ require ( github.com/pmezard/go-difflib v1.0.0 // indirect github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect golang.org/x/sys v0.42.0 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect + gopkg.in/yaml.v3 v3.0.1 modernc.org/libc v1.70.0 // indirect modernc.org/mathutil v1.7.1 // indirect modernc.org/memory v1.11.0 // indirect