From bc8ec89ca411e02e06a7f9eb838c0afa6e585642 Mon Sep 17 00:00:00 2001 From: Michael Brooks Date: Mon, 23 Nov 2026 09:41:05 -0700 Subject: [PATCH 1/7] test: add unit tests for WatchManifest in internal/pkg/platform --- internal/pkg/platform/localserver_test.go | 46 +++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/internal/pkg/platform/localserver_test.go b/internal/pkg/platform/localserver_test.go index a2e5304e..456f2266 100644 --- a/internal/pkg/platform/localserver_test.go +++ b/internal/pkg/platform/localserver_test.go @@ -28,6 +28,7 @@ import ( "github.com/gorilla/websocket" "github.com/radovskyb/watcher" "github.com/slackapi/slack-cli/internal/api" + "github.com/slackapi/slack-cli/internal/config" "github.com/slackapi/slack-cli/internal/hooks" "github.com/slackapi/slack-cli/internal/shared" "github.com/slackapi/slack-cli/internal/shared/types" @@ -508,6 +509,51 @@ func Test_LocalServer_watchAppEvents(t *testing.T) { } } +func Test_LocalServer_WatchManifest(t *testing.T) { + for name, tc := range map[string]struct { + manifestSource config.ManifestSource + manifestErr error + expectedErr error + expectedOutput []string + }{ + "returns the error when the manifest source cannot be read": { + manifestSource: config.ManifestSourceLocal, + manifestErr: slackerror.New(slackerror.ErrInvalidManifestSource), + expectedErr: slackerror.New(slackerror.ErrInvalidManifestSource), + }, + "waits for the context to end when manifest watching is not enabled": { + manifestSource: config.ManifestSourceLocal, + expectedOutput: []string{"Manifest watching is not enabled"}, + }, + "notes a remote manifest source before checking the watch config": { + manifestSource: config.ManifestSourceRemote, + expectedOutput: []string{"Manifest source is remote", "Manifest watching is not enabled"}, + }, + } { + t.Run(name, func(t *testing.T) { + ctx, cancel := context.WithCancel(slackcontext.MockContext(t.Context())) + cancel() + clientsMock := shared.NewClientsMock() + clientsMock.Config.DebugEnabled = true + projectConfigMock := config.NewProjectConfigMock() + projectConfigMock.On("GetManifestSource", mock.Anything).Return(tc.manifestSource, tc.manifestErr) + clientsMock.Config.ProjectConfig = projectConfigMock + clientsMock.AddDefaultMocks() + clients := shared.NewClientFactory(clientsMock.MockClientFactory()) + server := LocalServer{clients: clients} + err := server.WatchManifest(ctx, types.SlackAuth{}, types.App{}) + if tc.expectedErr != nil { + assert.Equal(t, tc.expectedErr, err) + return + } + require.NoError(t, err) + for _, expected := range tc.expectedOutput { + assert.Contains(t, clientsMock.GetStdoutOutput(), expected) + } + }) + } +} + func Test_LocalServer_watchManifestEvents(t *testing.T) { for name, tc := range map[string]struct { cancel bool From c7a650c0e501257a3563608180c6b86728ff67ed Mon Sep 17 00:00:00 2001 From: Michael Brooks Date: Mon, 23 Nov 2026 11:36:01 -0700 Subject: [PATCH 2/7] test: check WatchManifest blocks until the context ends --- internal/pkg/platform/localserver_test.go | 28 +++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/internal/pkg/platform/localserver_test.go b/internal/pkg/platform/localserver_test.go index 456f2266..ae5783de 100644 --- a/internal/pkg/platform/localserver_test.go +++ b/internal/pkg/platform/localserver_test.go @@ -24,6 +24,7 @@ import ( "strings" "sync" "testing" + "time" "github.com/gorilla/websocket" "github.com/radovskyb/watcher" @@ -532,7 +533,7 @@ func Test_LocalServer_WatchManifest(t *testing.T) { } { t.Run(name, func(t *testing.T) { ctx, cancel := context.WithCancel(slackcontext.MockContext(t.Context())) - cancel() + defer cancel() clientsMock := shared.NewClientsMock() clientsMock.Config.DebugEnabled = true projectConfigMock := config.NewProjectConfigMock() @@ -541,12 +542,31 @@ func Test_LocalServer_WatchManifest(t *testing.T) { clientsMock.AddDefaultMocks() clients := shared.NewClientFactory(clientsMock.MockClientFactory()) server := LocalServer{clients: clients} - err := server.WatchManifest(ctx, types.SlackAuth{}, types.App{}) + errChan := make(chan error, 1) + go func() { + errChan <- server.WatchManifest(ctx, types.SlackAuth{}, types.App{}) + }() if tc.expectedErr != nil { - assert.Equal(t, tc.expectedErr, err) + select { + case err := <-errChan: + assert.Equal(t, tc.expectedErr, err) + case <-time.After(time.Second): + require.FailNow(t, "WatchManifest did not return the error") + } return } - require.NoError(t, err) + select { + case err := <-errChan: + require.FailNow(t, "WatchManifest returned before the context ended", "err: %v", err) + case <-time.After(50 * time.Millisecond): + } + cancel() + select { + case err := <-errChan: + require.NoError(t, err) + case <-time.After(time.Second): + require.FailNow(t, "WatchManifest did not return after the context ended") + } for _, expected := range tc.expectedOutput { assert.Contains(t, clientsMock.GetStdoutOutput(), expected) } From 22349130d05041ef9865541d439784856a1cd6a4 Mon Sep 17 00:00:00 2001 From: Michael Brooks Date: Mon, 23 Nov 2026 15:08:01 -0700 Subject: [PATCH 3/7] fix: stop the manifest file watcher when the context ends --- internal/pkg/platform/localserver.go | 18 +++++++++++ internal/pkg/platform/localserver_test.go | 37 +++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/internal/pkg/platform/localserver.go b/internal/pkg/platform/localserver.go index 15468550..5238ae87 100644 --- a/internal/pkg/platform/localserver.go +++ b/internal/pkg/platform/localserver.go @@ -399,9 +399,27 @@ func (r *LocalServer) WatchManifest(ctx context.Context, auth types.SlackAuth, a // Begin watching for manifest file changes go r.watchManifestEvents(ctx, w, isRemoteManifest, auth, app) + // Stop the watcher when the context ends so Start returns + go closeWatcherOnDone(ctx, w) + return w.Start(time.Millisecond * 100) } +// closeWatcherOnDone closes the watcher after it starts and the context ends, draining events and errors until it closes +func closeWatcherOnDone(ctx context.Context, w *watcher.Watcher) { + <-ctx.Done() + w.Wait() + go w.Close() + for { + select { + case <-w.Event: + case <-w.Error: + case <-w.Closed: + return + } + } +} + // watchManifestEvents reinstalls the app on each manifest change until the context is canceled or the watcher closes func (r *LocalServer) watchManifestEvents(ctx context.Context, w *watcher.Watcher, isRemoteManifest bool, auth types.SlackAuth, app types.App) { for { diff --git a/internal/pkg/platform/localserver_test.go b/internal/pkg/platform/localserver_test.go index a2e5304e..3855e1a6 100644 --- a/internal/pkg/platform/localserver_test.go +++ b/internal/pkg/platform/localserver_test.go @@ -24,10 +24,12 @@ import ( "strings" "sync" "testing" + "time" "github.com/gorilla/websocket" "github.com/radovskyb/watcher" "github.com/slackapi/slack-cli/internal/api" + "github.com/slackapi/slack-cli/internal/config" "github.com/slackapi/slack-cli/internal/hooks" "github.com/slackapi/slack-cli/internal/shared" "github.com/slackapi/slack-cli/internal/shared/types" @@ -508,6 +510,41 @@ func Test_LocalServer_watchAppEvents(t *testing.T) { } } +func Test_LocalServer_WatchManifest_returnsWhenTheContextEnds(t *testing.T) { + ctx, cancel := context.WithCancel(slackcontext.MockContext(t.Context())) + defer cancel() + clientsMock := shared.NewClientsMock() + clientsMock.Config.DebugEnabled = true + projectConfigMock := config.NewProjectConfigMock() + projectConfigMock.On("GetManifestSource", mock.Anything).Return(config.ManifestSourceRemote, nil) + clientsMock.Config.ProjectConfig = projectConfigMock + clientsMock.AddDefaultMocks() + clients := shared.NewClientFactory(clientsMock.MockClientFactory()) + cliConfig := hooks.SDKCLIConfig{} + cliConfig.Config.Watch.Manifest = &hooks.ManifestWatchOpts{ + Paths: []string{t.TempDir()}, + FilterRegex: `manifest\.json$`, + } + server := LocalServer{clients: clients, cliConfig: cliConfig} + errChan := make(chan error, 1) + go func() { + errChan <- server.WatchManifest(ctx, types.SlackAuth{}, types.App{}) + }() + select { + case err := <-errChan: + require.FailNow(t, "WatchManifest returned before the context ended", "err: %v", err) + case <-time.After(250 * time.Millisecond): + } + cancel() + select { + case err := <-errChan: + require.NoError(t, err) + case <-time.After(time.Second): + require.FailNow(t, "WatchManifest did not return after the context ended") + } + assert.Contains(t, clientsMock.GetStdoutOutput(), "Watching manifest changes to file paths matching") +} + func Test_LocalServer_watchManifestEvents(t *testing.T) { for name, tc := range map[string]struct { cancel bool From 170f9e03a02c10bec2586efb59c51944a6598344 Mon Sep 17 00:00:00 2001 From: Michael Brooks Date: Mon, 23 Nov 2026 15:22:33 -0700 Subject: [PATCH 4/7] test: cover the drain in closeWatcherOnDone with an unread event --- internal/pkg/platform/localserver.go | 2 -- internal/pkg/platform/localserver_test.go | 28 +++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/internal/pkg/platform/localserver.go b/internal/pkg/platform/localserver.go index 5238ae87..cbec8f92 100644 --- a/internal/pkg/platform/localserver.go +++ b/internal/pkg/platform/localserver.go @@ -399,13 +399,11 @@ func (r *LocalServer) WatchManifest(ctx context.Context, auth types.SlackAuth, a // Begin watching for manifest file changes go r.watchManifestEvents(ctx, w, isRemoteManifest, auth, app) - // Stop the watcher when the context ends so Start returns go closeWatcherOnDone(ctx, w) return w.Start(time.Millisecond * 100) } -// closeWatcherOnDone closes the watcher after it starts and the context ends, draining events and errors until it closes func closeWatcherOnDone(ctx context.Context, w *watcher.Watcher) { <-ctx.Done() w.Wait() diff --git a/internal/pkg/platform/localserver_test.go b/internal/pkg/platform/localserver_test.go index 091751b4..6e09b747 100644 --- a/internal/pkg/platform/localserver_test.go +++ b/internal/pkg/platform/localserver_test.go @@ -21,6 +21,7 @@ import ( "net/http" "net/http/httptest" "os" + "path/filepath" "strings" "sync" "testing" @@ -586,6 +587,33 @@ func Test_LocalServer_WatchManifest(t *testing.T) { } } +func Test_closeWatcherOnDone(t *testing.T) { + // Start blocks sending an event that nobody reads after the context ends, so closing must drain it + dir := t.TempDir() + manifest := filepath.Join(dir, "manifest.json") + require.NoError(t, os.WriteFile(manifest, []byte("{}"), 0o600)) + w := watcher.New() + require.NoError(t, w.AddRecursive(dir)) + later := time.Now().Add(time.Hour) + require.NoError(t, os.Chtimes(manifest, later, later)) + started := make(chan error, 1) + go func() { started <- w.Start(time.Millisecond) }() + ctx, cancel := context.WithCancel(t.Context()) + cancel() + time.Sleep(50 * time.Millisecond) + done := make(chan struct{}) + go func() { + closeWatcherOnDone(ctx, w) + close(done) + }() + select { + case <-done: + case <-time.After(time.Second): + require.FailNow(t, "closeWatcherOnDone did not return with an unread event") + } + require.NoError(t, <-started) +} + func Test_LocalServer_watchManifestEvents(t *testing.T) { for name, tc := range map[string]struct { cancel bool From 911d6dd7a61dbad7d96530289b93c9a2f9bb67bf Mon Sep 17 00:00:00 2001 From: Michael Brooks Date: Mon, 23 Nov 2026 15:31:01 -0700 Subject: [PATCH 5/7] test: set the watch options as a pointer in Test_LocalServer_WatchManifest --- internal/pkg/platform/localserver_test.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/internal/pkg/platform/localserver_test.go b/internal/pkg/platform/localserver_test.go index 6e09b747..a8640603 100644 --- a/internal/pkg/platform/localserver_test.go +++ b/internal/pkg/platform/localserver_test.go @@ -550,9 +550,11 @@ func Test_LocalServer_WatchManifest(t *testing.T) { clients := shared.NewClientFactory(clientsMock.MockClientFactory()) server := LocalServer{clients: clients} if tc.watchManifest { - server.cliConfig.Config.Watch.Manifest = &hooks.ManifestWatchOpts{ - Paths: []string{t.TempDir()}, - FilterRegex: `manifest\.json$`, + server.cliConfig.Config.Watch = &hooks.WatchOpts{ + Manifest: &hooks.ManifestWatchOpts{ + Paths: []string{t.TempDir()}, + FilterRegex: `manifest\.json$`, + }, } } errChan := make(chan error, 1) From 731c50650df3d4550dfd64a02b2000d1e6a332ee Mon Sep 17 00:00:00 2001 From: Michael Brooks Date: Mon, 23 Nov 2026 15:31:50 -0700 Subject: [PATCH 6/7] test: set watch options per case now that a missing watch config shows the hint --- internal/pkg/platform/localserver_test.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/internal/pkg/platform/localserver_test.go b/internal/pkg/platform/localserver_test.go index ae5783de..8cb8536d 100644 --- a/internal/pkg/platform/localserver_test.go +++ b/internal/pkg/platform/localserver_test.go @@ -514,20 +514,28 @@ func Test_LocalServer_WatchManifest(t *testing.T) { for name, tc := range map[string]struct { manifestSource config.ManifestSource manifestErr error + watch *hooks.WatchOpts expectedErr error expectedOutput []string }{ + "asks for watch configuration when the hooks omit it": { + manifestSource: config.ManifestSourceLocal, + expectedOutput: []string{"provide watch configuration"}, + }, "returns the error when the manifest source cannot be read": { manifestSource: config.ManifestSourceLocal, + watch: &hooks.WatchOpts{}, manifestErr: slackerror.New(slackerror.ErrInvalidManifestSource), expectedErr: slackerror.New(slackerror.ErrInvalidManifestSource), }, "waits for the context to end when manifest watching is not enabled": { manifestSource: config.ManifestSourceLocal, + watch: &hooks.WatchOpts{}, expectedOutput: []string{"Manifest watching is not enabled"}, }, "notes a remote manifest source before checking the watch config": { manifestSource: config.ManifestSourceRemote, + watch: &hooks.WatchOpts{}, expectedOutput: []string{"Manifest source is remote", "Manifest watching is not enabled"}, }, } { @@ -542,6 +550,7 @@ func Test_LocalServer_WatchManifest(t *testing.T) { clientsMock.AddDefaultMocks() clients := shared.NewClientFactory(clientsMock.MockClientFactory()) server := LocalServer{clients: clients} + server.cliConfig.Config.Watch = tc.watch errChan := make(chan error, 1) go func() { errChan <- server.WatchManifest(ctx, types.SlackAuth{}, types.App{}) From 928e41533e1cea777035da965c9a37c59aadfd10 Mon Sep 17 00:00:00 2001 From: Michael Brooks Date: Tue, 24 Nov 2026 09:01:50 -0700 Subject: [PATCH 7/7] test: name the closeWatcherOnDone case for the unread event it drains --- internal/pkg/platform/localserver_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/internal/pkg/platform/localserver_test.go b/internal/pkg/platform/localserver_test.go index f8325f55..bdeedb7b 100644 --- a/internal/pkg/platform/localserver_test.go +++ b/internal/pkg/platform/localserver_test.go @@ -598,8 +598,7 @@ func Test_LocalServer_WatchManifest(t *testing.T) { } } -func Test_closeWatcherOnDone(t *testing.T) { - // Start blocks sending an event that nobody reads after the context ends, so closing must drain it +func Test_closeWatcherOnDone_drainsAnUnreadEvent(t *testing.T) { dir := t.TempDir() manifest := filepath.Join(dir, "manifest.json") require.NoError(t, os.WriteFile(manifest, []byte("{}"), 0o600))