Merge pull request #6 from KristianJBorgwarth/feat/integration-testing

ref: changed frontmatter parsing to use gopkg/yaml
This commit is contained in:
Kristian 2026-04-02 17:14:08 +02:00 committed by GitHub
commit ae99233e4b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 207 additions and 138 deletions

2
core/files/doc.go Normal file
View file

@ -0,0 +1,2 @@
// Package files provides utilities for working with files and directories.
package files

View file

@ -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
}

View file

@ -1,55 +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(frontMatter map[string]string) ([]string, error) {
tagsStr, ok := frontMatter["tags"]
if !ok {
return nil, errors.New("tags not found in front matter")
}
var tags []string
err := json.Unmarshal([]byte(tagsStr), &tags)
func ParseTags(file []byte) ([]string, error) {
fm, err := parseFrontMatter(file)
if err != nil {
return nil, err
}
return tags, 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")
}
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 &fm, nil
}

View file

@ -1,14 +1,12 @@
package handlers
import (
"bytes"
"context"
"database/sql"
"encoding/json"
"os"
"github.com/KristianJBorgwarth/dendrite.daemon/core/frontmatter"
"github.com/KristianJBorgwarth/dendrite.daemon/core/rpc"
"github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories"
)
@ -27,26 +25,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.ParseTags(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 +56,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
}

View file

@ -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)
}

View file

@ -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, &params); 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
}

View file

@ -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"`
}

View file

@ -1,4 +1,4 @@
// 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 (
@ -7,6 +7,7 @@ import (
"encoding/json"
"fmt"
"io"
"github.com/KristianJBorgwarth/dendrite.daemon/core/handlers"
"github.com/KristianJBorgwarth/dendrite.daemon/core/rpc"
)
@ -55,23 +56,31 @@ func (s *Server) handle(w io.Writer, req rpc.Request) {
Code: -32601,
Message: "method not found",
})
return
}
return
}
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.respond(w, req.ID, result, err)
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: result,
Result: resultJSON,
Error: err,
}
@ -80,5 +89,5 @@ func (s *Server) respond(w io.Writer, id *int, result any, err *rpc.Error) {
func (s *Server) write(w io.Writer, resp rpc.Response) {
data, _ := json.Marshal(resp)
fmt.Fprintln(w, string(data))
fmt.Fprintln(w, string(data))
}

View file

@ -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
}

9
go.mod
View file

@ -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
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
)

40
go.sum
View file

@ -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=

View file

@ -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,14 @@ func TestCreateNoteHandlerOnSucess(t *testing.T) {
request := CreateTestRequest("createNote", 1, requestParamsBytes)
// Act
response, rpcError = handler.Handle(TestContext, request.Params)
response, err := handler.Handle(Fixture.TestContext, request.Params)
// Assert
if err != nil {
t.Fatalf("handler returned an error: %v", err)
}
if response != nil {
t.Fatalf("expected response to be nil, got: %v", response)
}
}

View file

@ -2,27 +2,51 @@ package integration_test
import (
"context"
"database/sql"
"encoding/json"
"os"
"path/filepath"
"testing"
"github.com/KristianJBorgwarth/dendrite.daemon/core/rpc"
"github.com/KristianJBorgwarth/dendrite.daemon/persistence"
_ "modernc.org/sqlite"
)
var DbPath string = os.TempDir() + "/dendrite_test_vault"
var TestContext = context.Background()
type DBFixture struct {
DB *sql.DB
DBPath string
TestContext context.Context
}
func TestMain(m *testing.M) {
func NewDBFixture() *DBFixture {
vaultPath := os.TempDir()
err := persistence.InitializeIndex(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(DbPath)
os.RemoveAll(Fixture.DBPath)
os.Exit(code)
}

View file

@ -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")
}