From 0e46e3bfb10aba2c2060c6e34f6dee714755469d Mon Sep 17 00:00:00 2001 From: kranthi Date: Sun, 22 Nov 2026 09:27:19 -0700 Subject: [PATCH 1/2] feat(sync): videos have their own section in .uigraph.yaml A video was a docs: entry with fileType: walkthrough, a field that only existed to undo the wrong nesting. Add a top-level videos: list of paths or globs (default .uigraph/videos/*.video.yaml) that syncs through the docs path, named by each video's title. Files the config names itself must be videos; the default glob skips other files. docs: entries with fileType walkthrough still work and are not duplicated. The API still stores a video as a doc with fileType walkthrough, so nothing server-side changes. Co-Authored-By: Claude Sonnet 5.5 --- pkg/config/config.go | 15 +++++++ pkg/config/videos.go | 83 ++++++++++++++++++++++++++++++++++++ pkg/config/videos_test.go | 89 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 187 insertions(+) create mode 100644 pkg/config/videos.go create mode 100644 pkg/config/videos_test.go diff --git a/pkg/config/config.go b/pkg/config/config.go index 77fa86b..be372f3 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -31,6 +31,7 @@ type Config struct { Queries []QueryRef `yaml:"queries,omitempty"` QueryFiles []string `yaml:"queryFiles,omitempty"` Docs []DocRef `yaml:"docs,omitempty"` + Videos []string `yaml:"videos,omitempty"` Maps []MapRef `yaml:"maps,omitempty"` ML []MLProjectRef `yaml:"ml,omitempty"` CostTags []CostTag `yaml:"costTags,omitempty"` @@ -416,6 +417,20 @@ func Load(path string) (*Config, error) { cfg.Queries = append(cfg.Queries, f.Queries...) } + videos, err := videoDocs(filepath.Dir(path), cfg.Videos) + if err != nil { + return nil, err + } + listed := map[string]bool{} + for _, d := range cfg.Docs { + listed[filepath.Clean(d.Path)] = true + } + for _, d := range videos { + if !listed[filepath.Clean(d.Path)] { + cfg.Docs = append(cfg.Docs, d) + } + } + return &cfg, nil } diff --git a/pkg/config/videos.go b/pkg/config/videos.go new file mode 100644 index 0000000..0a3bd25 --- /dev/null +++ b/pkg/config/videos.go @@ -0,0 +1,83 @@ +package config + +import ( + "fmt" + "os" + "path/filepath" + "sort" + "strings" + + "gopkg.in/yaml.v3" +) + +// WalkthroughFileType is the doc file type a video's YAML is stored under. A repo does not +// write it any more: videos have their own top-level section. ("video" is an uploaded video file.) +const WalkthroughFileType = "walkthrough" + +// DefaultVideoGlob is what the videos section covers when the config does not list any. +const DefaultVideoGlob = ".uigraph/videos/*.video.yaml" + +// videoDocs turns the videos section into docs, named by each video's title, so videos sync +// through the same path as other files. Entries are paths or globs relative to the config file. +// Without a section, the default glob applies and files that are not videos are skipped; a file +// the config names itself must be a video, or the sync stops with a clear error. +func videoDocs(root string, entries []string) ([]DocRef, error) { + explicit := len(entries) > 0 + if !explicit { + entries = []string{DefaultVideoGlob} + } + seen := map[string]bool{} + var docs []DocRef + for i, entry := range entries { + pattern := entry + if !filepath.IsAbs(pattern) { + pattern = filepath.Join(root, pattern) + } + paths, err := filepath.Glob(pattern) + if err != nil { + return nil, fmt.Errorf("videos[%d] is not a valid path or pattern: %s", i, entry) + } + sort.Strings(paths) + if explicit && len(paths) == 0 { + return nil, fmt.Errorf("videos[%d] matches no file: %s", i, entry) + } + for _, p := range paths { + if seen[p] { + continue + } + seen[p] = true + doc, ok, err := videoDoc(p) + if err != nil { + return nil, err + } + if !ok { + if explicit { + return nil, fmt.Errorf("videos[%d] is not a video file (it has no top-level \"walkthrough:\" key): %s", i, p) + } + continue + } + docs = append(docs, doc) + } + } + return docs, nil +} + +func videoDoc(path string) (DocRef, bool, error) { + b, err := os.ReadFile(path) + if err != nil { + return DocRef{}, false, fmt.Errorf("could not read video file %s: %v", path, err) + } + var head struct { + Walkthrough string `yaml:"walkthrough"` + Title string `yaml:"title"` + Description string `yaml:"description"` + } + if yaml.Unmarshal(b, &head) != nil || head.Walkthrough == "" { + return DocRef{}, false, nil + } + name := head.Title + if name == "" { + name = strings.TrimSuffix(filepath.Base(path), ".video.yaml") + } + return DocRef{Name: name, Path: path, FileType: WalkthroughFileType, Description: head.Description}, true, nil +} diff --git a/pkg/config/videos_test.go b/pkg/config/videos_test.go new file mode 100644 index 0000000..183c7e5 --- /dev/null +++ b/pkg/config/videos_test.go @@ -0,0 +1,89 @@ +package config + +import ( + "os" + "path/filepath" + "strings" + "testing" +) + +func videoRepo(t *testing.T, config string, files map[string]string) string { + t.Helper() + root := t.TempDir() + for name, body := range files { + p := filepath.Join(root, name) + if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(p, []byte(body), 0o644); err != nil { + t.Fatal(err) + } + } + cfg := filepath.Join(root, ".uigraph.yaml") + if err := os.WriteFile(cfg, []byte("version: 1\nservice:\n name: Svc\n"+config), 0o644); err != nil { + t.Fatal(err) + } + return cfg +} + +const loginVideo = "walkthrough: login\ntitle: How login works\ndescription: Sign-in, start to end.\nscenes: [{say: hi}]\n" + +func TestVideosAreFoundWithoutAnySection(t *testing.T) { + cfg, err := Load(videoRepo(t, "", map[string]string{ + ".uigraph/videos/login.video.yaml": loginVideo, + ".uigraph/videos/plain.video.yaml": "title: Not a video\nscenes: []\n", + })) + if err != nil { + t.Fatal(err) + } + if len(cfg.Docs) != 1 || cfg.Docs[0].Name != "How login works" || cfg.Docs[0].FileType != WalkthroughFileType || + cfg.Docs[0].Description != "Sign-in, start to end." { + t.Fatalf("docs: %+v", cfg.Docs) + } +} + +func TestVideosSectionListsPathsAndGlobs(t *testing.T) { + cfg, err := Load(videoRepo(t, "videos:\n - tours/*.video.yaml\n - extra/one.yaml\n", map[string]string{ + "tours/a.video.yaml": loginVideo, + "tours/b.video.yaml": "walkthrough: b\nscenes: []\n", + "extra/one.yaml": "walkthrough: one\ntitle: One\nscenes: []\n", + ".uigraph/videos/ignored.video.yaml": loginVideo, // not listed, so not synced + })) + if err != nil { + t.Fatal(err) + } + var names []string + for _, d := range cfg.Docs { + names = append(names, d.Name) + } + if got := strings.Join(names, ","); got != "How login works,b,One" { + t.Fatalf("docs = %s", got) + } +} + +func TestVideosSectionErrors(t *testing.T) { + _, err := Load(videoRepo(t, "videos:\n - nope/*.video.yaml\n", nil)) + if err == nil || !strings.Contains(err.Error(), "videos[0] matches no file") { + t.Fatalf("missing file: %v", err) + } + _, err = Load(videoRepo(t, "videos:\n - a.yaml\n", map[string]string{"a.yaml": "title: nope\n"})) + if err == nil || !strings.Contains(err.Error(), "is not a video file") { + t.Fatalf("not a video: %v", err) + } +} + +func TestVideoAlsoListedAsDocIsNotDuplicated(t *testing.T) { + root := videoRepo(t, "", map[string]string{".uigraph/videos/login.video.yaml": loginVideo}) + dir := filepath.Dir(root) + p := filepath.Join(dir, ".uigraph/videos/login.video.yaml") + if err := os.WriteFile(root, []byte("version: 1\nservice:\n name: Svc\ndocs:\n - name: Login\n path: "+p+"\n fileType: walkthrough\n"), 0o644); err != nil { + t.Fatal(err) + } + cfg, err := Load(root) + if err != nil { + t.Fatal(err) + } + if len(cfg.Docs) != 1 || cfg.Docs[0].Name != "Login" { + t.Fatalf("docs: %+v", cfg.Docs) + } +} From 05285569232dc677a453fafb59275b9d720e484a Mon Sep 17 00:00:00 2001 From: kranthi Date: Sun, 22 Nov 2026 10:42:25 -0700 Subject: [PATCH 2/2] feat(sync): list videos by path, with no default folder or checks Match how diagrams and docs are listed: videos: is the path of each video's YAML, anywhere in the repo, and nothing is discovered. Drop the default glob and the check for a top-level walkthrough: key. Co-Authored-By: Claude Sonnet 5.5 --- pkg/config/videos.go | 82 ++++++++++----------------------------- pkg/config/videos_test.go | 64 +++++++++++------------------- 2 files changed, 42 insertions(+), 104 deletions(-) diff --git a/pkg/config/videos.go b/pkg/config/videos.go index 0a3bd25..08e6d29 100644 --- a/pkg/config/videos.go +++ b/pkg/config/videos.go @@ -2,82 +2,40 @@ package config import ( "fmt" - "os" "path/filepath" - "sort" "strings" - - "gopkg.in/yaml.v3" ) // WalkthroughFileType is the doc file type a video's YAML is stored under. A repo does not -// write it any more: videos have their own top-level section. ("video" is an uploaded video file.) +// write it: videos have their own top-level section. ("video" is an uploaded video file.) const WalkthroughFileType = "walkthrough" -// DefaultVideoGlob is what the videos section covers when the config does not list any. -const DefaultVideoGlob = ".uigraph/videos/*.video.yaml" - -// videoDocs turns the videos section into docs, named by each video's title, so videos sync -// through the same path as other files. Entries are paths or globs relative to the config file. -// Without a section, the default glob applies and files that are not videos are skipped; a file -// the config names itself must be a video, or the sync stops with a clear error. +// videoDocs turns the videos section into docs so videos sync through the same path as other +// files. Each entry is the path of a video's YAML, relative to the config file, like the paths of +// diagrams and docs. A video is named by its title, or by its file name when it has none. func videoDocs(root string, entries []string) ([]DocRef, error) { - explicit := len(entries) > 0 - if !explicit { - entries = []string{DefaultVideoGlob} - } - seen := map[string]bool{} var docs []DocRef for i, entry := range entries { - pattern := entry - if !filepath.IsAbs(pattern) { - pattern = filepath.Join(root, pattern) + if strings.TrimSpace(entry) == "" { + return nil, fmt.Errorf("videos[%d] is empty", i) } - paths, err := filepath.Glob(pattern) - if err != nil { - return nil, fmt.Errorf("videos[%d] is not a valid path or pattern: %s", i, entry) + path := entry + if !filepath.IsAbs(path) { + path = filepath.Join(root, path) } - sort.Strings(paths) - if explicit && len(paths) == 0 { - return nil, fmt.Errorf("videos[%d] matches no file: %s", i, entry) + var head struct { + Title string `yaml:"title"` + Description string `yaml:"description"` } - for _, p := range paths { - if seen[p] { - continue - } - seen[p] = true - doc, ok, err := videoDoc(p) - if err != nil { - return nil, err - } - if !ok { - if explicit { - return nil, fmt.Errorf("videos[%d] is not a video file (it has no top-level \"walkthrough:\" key): %s", i, p) - } - continue - } - docs = append(docs, doc) + if err := readYAML(path, fmt.Sprintf("videos[%d] file", i), &head); err != nil { + return nil, err } + name := head.Title + if name == "" { + base := filepath.Base(path) + name = strings.TrimSuffix(strings.TrimSuffix(base, filepath.Ext(base)), ".video") + } + docs = append(docs, DocRef{Name: name, Path: path, FileType: WalkthroughFileType, Description: head.Description}) } return docs, nil } - -func videoDoc(path string) (DocRef, bool, error) { - b, err := os.ReadFile(path) - if err != nil { - return DocRef{}, false, fmt.Errorf("could not read video file %s: %v", path, err) - } - var head struct { - Walkthrough string `yaml:"walkthrough"` - Title string `yaml:"title"` - Description string `yaml:"description"` - } - if yaml.Unmarshal(b, &head) != nil || head.Walkthrough == "" { - return DocRef{}, false, nil - } - name := head.Title - if name == "" { - name = strings.TrimSuffix(filepath.Base(path), ".video.yaml") - } - return DocRef{Name: name, Path: path, FileType: WalkthroughFileType, Description: head.Description}, true, nil -} diff --git a/pkg/config/videos_test.go b/pkg/config/videos_test.go index 183c7e5..f852b45 100644 --- a/pkg/config/videos_test.go +++ b/pkg/config/videos_test.go @@ -26,64 +26,44 @@ func videoRepo(t *testing.T, config string, files map[string]string) string { return cfg } -const loginVideo = "walkthrough: login\ntitle: How login works\ndescription: Sign-in, start to end.\nscenes: [{say: hi}]\n" - -func TestVideosAreFoundWithoutAnySection(t *testing.T) { - cfg, err := Load(videoRepo(t, "", map[string]string{ - ".uigraph/videos/login.video.yaml": loginVideo, - ".uigraph/videos/plain.video.yaml": "title: Not a video\nscenes: []\n", - })) - if err != nil { - t.Fatal(err) - } - if len(cfg.Docs) != 1 || cfg.Docs[0].Name != "How login works" || cfg.Docs[0].FileType != WalkthroughFileType || - cfg.Docs[0].Description != "Sign-in, start to end." { - t.Fatalf("docs: %+v", cfg.Docs) - } -} - -func TestVideosSectionListsPathsAndGlobs(t *testing.T) { - cfg, err := Load(videoRepo(t, "videos:\n - tours/*.video.yaml\n - extra/one.yaml\n", map[string]string{ - "tours/a.video.yaml": loginVideo, - "tours/b.video.yaml": "walkthrough: b\nscenes: []\n", - "extra/one.yaml": "walkthrough: one\ntitle: One\nscenes: []\n", - ".uigraph/videos/ignored.video.yaml": loginVideo, // not listed, so not synced +func TestVideosSectionListsPaths(t *testing.T) { + cfg, err := Load(videoRepo(t, "videos:\n - tours/checkout.yaml\n - login.video.yaml\n - plain.yml\n", map[string]string{ + "tours/checkout.yaml": "title: How checkout works\ndescription: Cart to receipt.\nscenes: []\n", + "login.video.yaml": "scenes: []\n", + "plain.yml": "title: Plain\n", + "other.video.yaml": "title: Not listed\n", // not listed, so not synced })) if err != nil { t.Fatal(err) } var names []string for _, d := range cfg.Docs { + if d.FileType != WalkthroughFileType { + t.Errorf("%s has file type %q", d.Name, d.FileType) + } names = append(names, d.Name) } - if got := strings.Join(names, ","); got != "How login works,b,One" { + if got := strings.Join(names, ","); got != "How checkout works,login,Plain" { t.Fatalf("docs = %s", got) } -} - -func TestVideosSectionErrors(t *testing.T) { - _, err := Load(videoRepo(t, "videos:\n - nope/*.video.yaml\n", nil)) - if err == nil || !strings.Contains(err.Error(), "videos[0] matches no file") { - t.Fatalf("missing file: %v", err) - } - _, err = Load(videoRepo(t, "videos:\n - a.yaml\n", map[string]string{"a.yaml": "title: nope\n"})) - if err == nil || !strings.Contains(err.Error(), "is not a video file") { - t.Fatalf("not a video: %v", err) + if cfg.Docs[0].Description != "Cart to receipt." { + t.Errorf("description = %q", cfg.Docs[0].Description) } } -func TestVideoAlsoListedAsDocIsNotDuplicated(t *testing.T) { - root := videoRepo(t, "", map[string]string{".uigraph/videos/login.video.yaml": loginVideo}) - dir := filepath.Dir(root) - p := filepath.Join(dir, ".uigraph/videos/login.video.yaml") - if err := os.WriteFile(root, []byte("version: 1\nservice:\n name: Svc\ndocs:\n - name: Login\n path: "+p+"\n fileType: walkthrough\n"), 0o644); err != nil { - t.Fatal(err) - } - cfg, err := Load(root) +func TestNoVideosSectionSyncsNoVideos(t *testing.T) { + cfg, err := Load(videoRepo(t, "", map[string]string{".uigraph/videos/login.video.yaml": "title: Login\n"})) if err != nil { t.Fatal(err) } - if len(cfg.Docs) != 1 || cfg.Docs[0].Name != "Login" { + if len(cfg.Docs) != 0 { t.Fatalf("docs: %+v", cfg.Docs) } } + +func TestVideoThatDoesNotExistIsAnError(t *testing.T) { + _, err := Load(videoRepo(t, "videos:\n - nope.yaml\n", nil)) + if err == nil || !strings.Contains(err.Error(), "videos[0] file not found") { + t.Fatalf("err = %v", err) + } +}