summaryrefslogtreecommitdiff
path: root/internal/engine/session_test.go
diff options
context:
space:
mode:
authorLukasz Kasprzak <lukas@labunix.xyz>2026-09-16 00:57:03 +0200
committerLukasz Kasprzak <lukas@labunix.xyz>2026-09-16 00:57:03 +0200
commitd6a280d87a274abc8d9cab9956d2af1c845f83d1 (patch)
tree3b3790c3ddb906147ccc35fd7a284efdf616f8c8 /internal/engine/session_test.go
parent86e55e31f6905ae997619aa095706674e2ffe623 (diff)
downloadkrino-d6a280d87a274abc8d9cab9956d2af1c845f83d1.tar.gz
krino-d6a280d87a274abc8d9cab9956d2af1c845f83d1.zip
the engine owns a run: lock, log, run id, claims
Diffstat (limited to 'internal/engine/session_test.go')
-rw-r--r--internal/engine/session_test.go119
1 files changed, 119 insertions, 0 deletions
diff --git a/internal/engine/session_test.go b/internal/engine/session_test.go
new file mode 100644
index 0000000..ece69dd
--- /dev/null
+++ b/internal/engine/session_test.go
@@ -0,0 +1,119 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+package engine
+
+import (
+ "context"
+ "os"
+ "path/filepath"
+ "testing"
+ "time"
+)
+
+// twoOverwritingDirs builds a and b, each holding x.pdf, both moving it to
+// ~/Out with (on-conflict overwrite).
+func twoOverwritingDirs(t *testing.T, h string) *Engine {
+ t.Helper()
+ old := time.Now().Add(-2 * time.Hour)
+ dirs := map[string]string{}
+ for _, n := range []string{"a", "b"} {
+ p := filepath.Join(h, n, "x.pdf")
+ if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(p, []byte("from "+n), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ os.Chtimes(p, old, old)
+ dirs[n] = "(path \"~/" + n + "\")\n(on-conflict overwrite)\n(rule \"out\" (move \"~/Out\"))\n"
+ }
+ main := writeConfig(t, h, `(include "a" "b")`, dirs)
+ e, errs := Load(main)
+ if len(errs) > 0 {
+ t.Fatal(errs)
+ }
+ return e
+}
+
+// TestSessionKeepsOnlyAppliedDestinationsClaimed: one session covers a whole
+// run - its log, run id and claims - and after applying a directory only the
+// paths its files ended up at stay claimed, so a later directory's overwrite
+// takes a free name instead of trashing this run's own result (spec §7.4).
+func TestSessionKeepsOnlyAppliedDestinationsClaimed(t *testing.T) {
+ h := sandbox(t)
+ e := twoOverwritingDirs(t, h)
+ s, err := e.NewSession(false)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer s.Close()
+ if s.Run() == "" {
+ t.Error("a real session has no run id")
+ }
+ for _, d := range e.Dirs {
+ dp, err := s.Plan(context.Background(), d)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if _, err := s.Apply(context.Background(), dp, map[string]bool{"x.pdf": true}); err != nil {
+ t.Fatal(err)
+ }
+ }
+ for rel, want := range map[string]string{"Out/x.pdf": "from a", "Out/x_1.pdf": "from b"} {
+ if b, err := os.ReadFile(filepath.Join(h, rel)); err != nil || string(b) != want {
+ t.Errorf("%s: %q, %v; want %q", rel, b, err, want)
+ }
+ }
+ if entries, _ := os.ReadDir(filepath.Join(h, ".local", "share", "Trash", "files")); len(entries) != 0 {
+ t.Errorf("the Trash holds %d entries; nothing should have been displaced", len(entries))
+ }
+}
+
+// TestDrySessionWritesNoLog: a dry session opens no log, so a dry run never
+// creates the state directory's krino.log (spec §11).
+func TestDrySessionWritesNoLog(t *testing.T) {
+ h := sandbox(t)
+ e := twoOverwritingDirs(t, h)
+ s, err := e.NewSession(true)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer s.Close()
+ if s.Run() != "" {
+ t.Errorf("a dry session took a run id: %q", s.Run())
+ }
+ if _, err := s.Plan(context.Background(), e.Dirs[0]); err != nil {
+ t.Fatal(err)
+ }
+ if _, err := os.Stat(e.Config.LogFile()); !os.IsNotExist(err) {
+ t.Errorf("a dry session touched the log: %v", err)
+ }
+}
+
+// TestSessionLockIsTheDirectorysOwn: the session takes the same lock a
+// second krino waits for, and releasing it lets the next one in.
+func TestSessionLockIsTheDirectorysOwn(t *testing.T) {
+ h := sandbox(t)
+ e := twoOverwritingDirs(t, h)
+ s, err := e.NewSession(true)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer s.Close()
+ l, err := s.Lock(context.Background(), e.Dirs[0], false)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if _, err := s.Lock(context.Background(), e.Dirs[0], false); err == nil {
+ t.Error("the directory was locked twice")
+ }
+ if err := l.Release(); err != nil {
+ t.Fatal(err)
+ }
+ l2, err := s.Lock(context.Background(), e.Dirs[0], false)
+ if err != nil {
+ t.Errorf("the lock was not released: %v", err)
+ } else {
+ l2.Release()
+ }
+}