Add configurable sequence numbering command
This commit is contained in:
76
tests/contract/sequence_placement_test.go
Normal file
76
tests/contract/sequence_placement_test.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package contract
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/rogeecn/renamer/internal/sequence"
|
||||
)
|
||||
|
||||
func TestSequencePreviewWithPrefixPlacement(t *testing.T) {
|
||||
tmp := t.TempDir()
|
||||
|
||||
createFile(t, filepath.Join(tmp, "cover.png"))
|
||||
createFile(t, filepath.Join(tmp, "index.png"))
|
||||
|
||||
opts := sequence.DefaultOptions()
|
||||
opts.WorkingDir = tmp
|
||||
opts.Start = 10
|
||||
opts.Placement = sequence.PlacementPrefix
|
||||
opts.Separator = "-"
|
||||
|
||||
plan, err := sequence.Preview(context.Background(), opts, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("preview error: %v", err)
|
||||
}
|
||||
|
||||
expected := []string{"010-cover.png", "011-index.png"}
|
||||
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])
|
||||
}
|
||||
}
|
||||
|
||||
if plan.Config.Start != 10 {
|
||||
t.Fatalf("expected config start 10, got %d", plan.Config.Start)
|
||||
}
|
||||
if plan.Config.Placement != sequence.PlacementPrefix {
|
||||
t.Fatalf("expected prefix placement in config")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSequencePreviewWithNumberPrefixLabel(t *testing.T) {
|
||||
tmp := t.TempDir()
|
||||
|
||||
createFile(t, filepath.Join(tmp, "cover.png"))
|
||||
createFile(t, filepath.Join(tmp, "index.png"))
|
||||
|
||||
ops := sequence.DefaultOptions()
|
||||
ops.WorkingDir = tmp
|
||||
ops.Placement = sequence.PlacementPrefix
|
||||
ops.Separator = "-"
|
||||
ops.NumberPrefix = "seq"
|
||||
|
||||
plan, err := sequence.Preview(context.Background(), ops, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("preview error: %v", err)
|
||||
}
|
||||
|
||||
expected := []string{"seq001-cover.png", "seq002-index.png"}
|
||||
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])
|
||||
}
|
||||
}
|
||||
|
||||
if warnings := plan.Summary.Warnings; len(warnings) != 0 {
|
||||
t.Fatalf("expected no warnings, got %#v", warnings)
|
||||
}
|
||||
}
|
||||
53
tests/contract/sequence_preview_test.go
Normal file
53
tests/contract/sequence_preview_test.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package contract
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/rogeecn/renamer/internal/sequence"
|
||||
)
|
||||
|
||||
func TestSequencePreviewDefaultNumbering(t *testing.T) {
|
||||
tmp := t.TempDir()
|
||||
|
||||
createFile(t, filepath.Join(tmp, "draft.txt"))
|
||||
createFile(t, filepath.Join(tmp, "notes.txt"))
|
||||
createFile(t, filepath.Join(tmp, "plan.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.TotalCandidates != 3 {
|
||||
t.Fatalf("expected 3 candidates, got %d", plan.Summary.TotalCandidates)
|
||||
}
|
||||
if plan.Summary.RenamedCount != 3 {
|
||||
t.Fatalf("expected 3 renamed entries, got %d", plan.Summary.RenamedCount)
|
||||
}
|
||||
|
||||
expected := []string{"001_draft.txt", "002_notes.txt", "003_plan.txt"}
|
||||
if len(plan.Candidates) != 3 {
|
||||
t.Fatalf("expected 3 planned candidates, got %d", 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])
|
||||
}
|
||||
}
|
||||
|
||||
if plan.Summary.AppliedWidth != 3 {
|
||||
t.Fatalf("expected applied width 3, got %d", plan.Summary.AppliedWidth)
|
||||
}
|
||||
|
||||
if plan.Config.Start != 1 {
|
||||
t.Fatalf("expected start 1, got %d", plan.Config.Start)
|
||||
}
|
||||
if plan.Config.Placement != sequence.PlacementPrefix {
|
||||
t.Fatalf("expected prefix placement, got %s", plan.Config.Placement)
|
||||
}
|
||||
}
|
||||
39
tests/contract/sequence_width_test.go
Normal file
39
tests/contract/sequence_width_test.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package contract
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/rogeecn/renamer/internal/sequence"
|
||||
)
|
||||
|
||||
func TestSequencePreviewWithExplicitWidth(t *testing.T) {
|
||||
tmp := t.TempDir()
|
||||
|
||||
createFile(t, filepath.Join(tmp, "cutA.mov"))
|
||||
createFile(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)
|
||||
}
|
||||
|
||||
expected := []string{"0001_cutA.mov", "0002_cutB.mov"}
|
||||
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])
|
||||
}
|
||||
}
|
||||
|
||||
if plan.Summary.AppliedWidth != 4 {
|
||||
t.Fatalf("expected applied width 4, got %d", plan.Summary.AppliedWidth)
|
||||
}
|
||||
}
|
||||
70
tests/integration/sequence_flow_test.go
Normal file
70
tests/integration/sequence_flow_test.go
Normal 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)
|
||||
}
|
||||
}
|
||||
53
tests/integration/sequence_prefix_test.go
Normal file
53
tests/integration/sequence_prefix_test.go
Normal 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)
|
||||
}
|
||||
}
|
||||
65
tests/integration/sequence_start_test.go
Normal file
65
tests/integration/sequence_start_test.go
Normal 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)
|
||||
}
|
||||
}
|
||||
59
tests/integration/sequence_width_test.go
Normal file
59
tests/integration/sequence_width_test.go
Normal 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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user