Add configurable sequence numbering command

This commit is contained in:
2025-11-03 10:59:15 +08:00
parent 843c51e347
commit e6a5c6499b
34 changed files with 1920 additions and 1 deletions

View File

@@ -0,0 +1,70 @@
package integration
import (
"context"
"os"
"path/filepath"
"testing"
"github.com/rogeecn/renamer/internal/history"
"github.com/rogeecn/renamer/internal/sequence"
)
func TestSequenceApplyAndUndo(t *testing.T) {
tmp := t.TempDir()
createIntegrationFile(t, filepath.Join(tmp, "draft.txt"))
createIntegrationFile(t, filepath.Join(tmp, "notes.txt"))
opts := sequence.DefaultOptions()
opts.WorkingDir = tmp
plan, err := sequence.Preview(context.Background(), opts, nil)
if err != nil {
t.Fatalf("preview error: %v", err)
}
if plan.Summary.RenamedCount != 2 {
t.Fatalf("expected 2 planned renames, got %d", plan.Summary.RenamedCount)
}
entry, err := sequence.Apply(context.Background(), opts, plan)
if err != nil {
t.Fatalf("apply error: %v", err)
}
if len(entry.Operations) != 2 {
t.Fatalf("expected 2 recorded operations, got %d", len(entry.Operations))
}
if entry.Command != "sequence" {
t.Fatalf("expected ledger command 'sequence', got %s", entry.Command)
}
if _, err := os.Stat(filepath.Join(tmp, "001_draft.txt")); err != nil {
t.Fatalf("expected renamed file exists: %v", err)
}
if _, err := os.Stat(filepath.Join(tmp, "002_notes.txt")); err != nil {
t.Fatalf("expected renamed file exists: %v", err)
}
if _, err := history.Undo(tmp); err != nil {
t.Fatalf("undo error: %v", err)
}
if _, err := os.Stat(filepath.Join(tmp, "draft.txt")); err != nil {
t.Fatalf("expected original file after undo: %v", err)
}
if _, err := os.Stat(filepath.Join(tmp, "notes.txt")); err != nil {
t.Fatalf("expected original file after undo: %v", err)
}
}
func createIntegrationFile(t *testing.T, path string) {
t.Helper()
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatalf("mkdir %s: %v", path, err)
}
if err := os.WriteFile(path, []byte("data"), 0o644); err != nil {
t.Fatalf("write file %s: %v", path, err)
}
}

View File

@@ -0,0 +1,53 @@
package integration
import (
"context"
"os"
"path/filepath"
"testing"
"github.com/rogeecn/renamer/internal/history"
"github.com/rogeecn/renamer/internal/sequence"
)
func TestSequenceApplyWithNumberPrefix(t *testing.T) {
tmp := t.TempDir()
createIntegrationFile(t, filepath.Join(tmp, "cover.png"))
createIntegrationFile(t, filepath.Join(tmp, "index.png"))
opts := sequence.DefaultOptions()
opts.WorkingDir = tmp
opts.Placement = sequence.PlacementPrefix
opts.Separator = "-"
opts.NumberPrefix = "seq"
plan, err := sequence.Preview(context.Background(), opts, nil)
if err != nil {
t.Fatalf("preview error: %v", err)
}
entry, err := sequence.Apply(context.Background(), opts, plan)
if err != nil {
t.Fatalf("apply error: %v", err)
}
if _, err := os.Stat(filepath.Join(tmp, "seq001-cover.png")); err != nil {
t.Fatalf("expected renamed file exists: %v", err)
}
if _, err := os.Stat(filepath.Join(tmp, "seq002-index.png")); err != nil {
t.Fatalf("expected renamed file exists: %v", err)
}
meta, ok := entry.Metadata["sequence"].(map[string]any)
if !ok {
t.Fatalf("sequence metadata missing")
}
if prefix, ok := meta["prefix"].(string); !ok || prefix != "seq" {
t.Fatalf("expected metadata prefix 'seq', got %#v", meta["prefix"])
}
if _, err := history.Undo(tmp); err != nil {
t.Fatalf("undo error: %v", err)
}
}

View File

@@ -0,0 +1,65 @@
package integration
import (
"context"
"os"
"path/filepath"
"testing"
"github.com/rogeecn/renamer/internal/history"
"github.com/rogeecn/renamer/internal/sequence"
)
func TestSequenceApplyWithStartOffset(t *testing.T) {
tmp := t.TempDir()
createIntegrationFile(t, filepath.Join(tmp, "shotA.exr"))
createIntegrationFile(t, filepath.Join(tmp, "shotB.exr"))
opts := sequence.DefaultOptions()
opts.WorkingDir = tmp
opts.Start = 10
plan, err := sequence.Preview(context.Background(), opts, nil)
if err != nil {
t.Fatalf("preview error: %v", err)
}
expected := []string{"010_shotA.exr", "011_shotB.exr"}
if len(plan.Candidates) != len(expected) {
t.Fatalf("expected %d candidates, got %d", len(expected), len(plan.Candidates))
}
for i, candidate := range plan.Candidates {
if candidate.ProposedPath != expected[i] {
t.Fatalf("candidate %d proposed %s, expected %s", i, candidate.ProposedPath, expected[i])
}
}
entry, err := sequence.Apply(context.Background(), opts, plan)
if err != nil {
t.Fatalf("apply error: %v", err)
}
if len(entry.Operations) != 2 {
t.Fatalf("expected 2 operations, got %d", len(entry.Operations))
}
if _, err := os.Stat(filepath.Join(tmp, "010_shotA.exr")); err != nil {
t.Fatalf("expected renamed file exists: %v", err)
}
if _, err := os.Stat(filepath.Join(tmp, "011_shotB.exr")); err != nil {
t.Fatalf("expected renamed file exists: %v", err)
}
meta, ok := entry.Metadata["sequence"].(map[string]any)
if !ok {
t.Fatalf("sequence metadata missing")
}
if start, ok := meta["start"].(int); !ok || start != 10 {
t.Fatalf("expected metadata start 10, got %#v", meta["start"])
}
if _, err := history.Undo(tmp); err != nil {
t.Fatalf("undo error: %v", err)
}
}

View File

@@ -0,0 +1,59 @@
package integration
import (
"context"
"os"
"path/filepath"
"testing"
"github.com/rogeecn/renamer/internal/history"
"github.com/rogeecn/renamer/internal/sequence"
)
func TestSequenceApplyWithExplicitWidth(t *testing.T) {
tmp := t.TempDir()
createIntegrationFile(t, filepath.Join(tmp, "cutA.mov"))
createIntegrationFile(t, filepath.Join(tmp, "cutB.mov"))
opts := sequence.DefaultOptions()
opts.WorkingDir = tmp
opts.Width = 4
plan, err := sequence.Preview(context.Background(), opts, nil)
if err != nil {
t.Fatalf("preview error: %v", err)
}
if plan.Summary.AppliedWidth != 4 {
t.Fatalf("expected applied width 4, got %d", plan.Summary.AppliedWidth)
}
entry, err := sequence.Apply(context.Background(), opts, plan)
if err != nil {
t.Fatalf("apply error: %v", err)
}
if len(entry.Operations) != 2 {
t.Fatalf("expected 2 operations, got %d", len(entry.Operations))
}
if _, err := os.Stat(filepath.Join(tmp, "0001_cutA.mov")); err != nil {
t.Fatalf("expected renamed file exists: %v", err)
}
if _, err := os.Stat(filepath.Join(tmp, "0002_cutB.mov")); err != nil {
t.Fatalf("expected renamed file exists: %v", err)
}
meta, ok := entry.Metadata["sequence"].(map[string]any)
if !ok {
t.Fatalf("sequence metadata missing")
}
if width, ok := meta["width"].(int); !ok || width != 4 {
t.Fatalf("expected metadata width 4, got %#v", meta["width"])
}
if _, err := history.Undo(tmp); err != nil {
t.Fatalf("undo error: %v", err)
}
}