Share lazy configuration and session acquisition
This commit is contained in:
@@ -307,6 +307,17 @@ semantic checks. Inspection never depends on persisted domain snapshots.
|
||||
|
||||
## Stage 7: Share lazy identity and source acquisition
|
||||
|
||||
**Completed.** Invocation-owned typed selections now acquire pipeline, campaign,
|
||||
party, session, and derived execution context lazily, retaining native results.
|
||||
Exact files and identity assertions are complementary; local errors never trigger
|
||||
remote fallback. Remote session acquisition avoids roster/catalog work, retains
|
||||
storage and cleanup causes, distinguishes committed snapshots, and releases its
|
||||
temporary download through the shared owner. Bootstrap runs once and retains
|
||||
failures. Direct acquisition, identity, discovery, cleanup, pipeline-retention,
|
||||
and prepared-publication round-trip checks and builds pass with Go 1.27.4.
|
||||
Native inspection and command declarations consume this owner in later stages;
|
||||
old CLI invocation tests remain transitional.
|
||||
|
||||
Read: `internal/app/config_loader.go`, `campaign_config_path.go`,
|
||||
`session_config_path.go`, `remote_session.go`, `command_dependencies.go`,
|
||||
and the roadmap's selector and session-acquisition sections.
|
||||
|
||||
@@ -12,9 +12,6 @@ import (
|
||||
func resolveCampaignConfigPath(pipelineCfg *config.PipelineConfig, campaignIDFlag, campaignFileFlag string) (string, error) {
|
||||
campaignID := strings.TrimSpace(campaignIDFlag)
|
||||
campaignFile := strings.TrimSpace(campaignFileFlag)
|
||||
if campaignID != "" && campaignFile != "" {
|
||||
return "", fmt.Errorf("--campaign and --campaign-file are mutually exclusive")
|
||||
}
|
||||
if campaignFile != "" {
|
||||
return filepath.Clean(campaignFile), nil
|
||||
}
|
||||
|
||||
@@ -50,13 +50,11 @@ func TestResolveCampaignConfigPathUsesDefaultCampaignID(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveCampaignConfigPathRejectsCampaignIDAndFile(t *testing.T) {
|
||||
_, err := resolveCampaignConfigPath(&config.PipelineConfig{}, "dilfs", filepath.Join(t.TempDir(), "campaign.yml"))
|
||||
if err == nil {
|
||||
t.Fatal("expected error, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "mutually exclusive") {
|
||||
t.Fatalf("error = %q, want mutual exclusion", err.Error())
|
||||
func TestResolveCampaignConfigPathCombinesIdentityAndFile(t *testing.T) {
|
||||
explicit := filepath.Join(t.TempDir(), "campaign.yml")
|
||||
got, err := resolveCampaignConfigPath(&config.PipelineConfig{}, "dilfs", explicit)
|
||||
if err != nil || got != explicit {
|
||||
t.Fatalf("path=%q error=%v", got, err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/fileops"
|
||||
"go.maximumdirect.net/commandkit"
|
||||
)
|
||||
|
||||
type pipelineCampaignConfig = config.LoadedPipelineCampaign
|
||||
@@ -37,46 +38,50 @@ func retainedCommandConfig(cfg *config.Config) *commandConfig {
|
||||
return &commandConfig{Config: cfg}
|
||||
}
|
||||
|
||||
func loadCommandConfigWithDependencies(ctx context.Context, pipelineFlag, campaignFlag, campaignFileFlag, sessionFlag string, sessionOpts config.SessionLoadOptions, secretsDir *string, deps commandDependencies) (loaded *commandConfig, err error) {
|
||||
deps = normalizeCommandDependencies(deps)
|
||||
func loadCommandConfigWithDependencies(ctx context.Context, pipelineFlag, campaignFlag, campaignFileFlag, sessionFlag string, sessionOpts config.SessionLoadOptions, secretsDir *string, deps commandDependencies) (*commandConfig, error) {
|
||||
selection := configurationSelection{}
|
||||
for _, field := range []struct {
|
||||
value string
|
||||
target *commandkit.Supplied[string]
|
||||
}{
|
||||
{pipelineFlag, &selection.Config}, {campaignFlag, &selection.Campaign}, {campaignFileFlag, &selection.CampaignFile},
|
||||
{sessionFlag, &selection.SessionFile}, {sessionOpts.SessionID, &selection.Session},
|
||||
} {
|
||||
if field.value != "" {
|
||||
*field.target = commandkit.Supplied[string]{Value: field.value, Set: true}
|
||||
}
|
||||
}
|
||||
if secretsDir != nil {
|
||||
selection.SecretsDir = commandkit.Supplied[string]{Value: *secretsDir, Set: true}
|
||||
}
|
||||
if sessionOpts.Overlay != nil {
|
||||
selection.Overlay = commandkit.Supplied[string]{Value: *sessionOpts.Overlay, Set: true}
|
||||
}
|
||||
acquisition := newConfigurationAcquisition(selection, deps)
|
||||
acquisition.previousSessionID = sessionOpts.PreviousSessionID
|
||||
cfg, err := acquisition.Domain(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Join(err, acquisition.Close())
|
||||
}
|
||||
return &commandConfig{Config: cfg, cleanup: acquisition.Close}, nil
|
||||
}
|
||||
|
||||
type acquiredSession struct {
|
||||
Config *config.SessionConfig
|
||||
Path string
|
||||
Source config.SessionSource
|
||||
cleanup func() error
|
||||
}
|
||||
|
||||
func loadRemoteSessionConfig(ctx context.Context, base *config.LoadedPipelineCampaign, sessionOpts config.SessionLoadOptions, deps commandDependencies) (loaded *acquiredSession, err error) {
|
||||
var cleanup func() error
|
||||
defer func() {
|
||||
if err == nil || cleanup == nil {
|
||||
return
|
||||
}
|
||||
if cleanupErr := cleanup(); cleanupErr != nil {
|
||||
err = errors.Join(err, cleanupErr)
|
||||
if err != nil && cleanup != nil {
|
||||
err = errors.Join(err, cleanup())
|
||||
}
|
||||
}()
|
||||
|
||||
base, err := loadPipelineCampaignConfigWithDependencies(pipelineFlag, campaignFlag, campaignFileFlag, config.PipelineLoadOptions{Overlay: sessionOpts.Overlay}, secretsDir, deps)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sessionOpts.Campaign = base.Campaign
|
||||
sessionOpts.CampaignPath = base.CampaignPath
|
||||
|
||||
if explicitSession := strings.TrimSpace(sessionFlag); explicitSession != "" {
|
||||
cfg, err := config.LoadSessionWithPipelineCampaignOptions(*base, explicitSession, sessionOpts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return retainedCommandConfig(cfg), nil
|
||||
}
|
||||
|
||||
discoveredSession, discoveredPath, err := config.DiscoverSession(config.DefaultSessionConfigSearchPaths, sessionOpts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if discoveredSession != nil {
|
||||
cfg, err := config.ResolveLoadedPipelineCampaign(*base, discoveredPath, discoveredSession, config.SessionSource{Source: "session_config", LocalPath: discoveredPath})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return retainedCommandConfig(cfg), nil
|
||||
}
|
||||
|
||||
sessionID := strings.TrimSpace(sessionOpts.SessionID)
|
||||
if sessionID == "" {
|
||||
return nil, missingSessionConfigError(config.DefaultSessionConfigSearchPaths, "remote session loading requires a session_id")
|
||||
@@ -88,24 +93,21 @@ func loadCommandConfigWithDependencies(ctx context.Context, pipelineFlag, campai
|
||||
}
|
||||
sessionPrefix := artifacts.S3SessionPrefix(rootPrefix, config.CampaignID(base.Campaign), sessionID)
|
||||
remoteKey := artifacts.S3SessionConfigKey(sessionPrefix)
|
||||
partialCfg, err := config.ResolveLoadedPipelineCampaign(*base, "", nil, config.SessionSource{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
partialCfg := &config.Config{Pipeline: base.Pipeline, PipelinePath: base.PipelinePath, Campaign: base.Campaign, CampaignPath: base.CampaignPath}
|
||||
store, err := newCommandObjectStoreWithDependencies(ctx, partialCfg, nil, deps)
|
||||
if err != nil {
|
||||
return nil, missingSessionConfigError(config.DefaultSessionConfigSearchPaths, fmt.Sprintf("remote session %q unavailable: %v", remoteKey, err))
|
||||
return nil, fmt.Errorf("remote session %q unavailable: %w", remoteKey, err)
|
||||
}
|
||||
|
||||
sessionInfo, checksum, body, err := findRemoteSessionConfig(ctx, store, sessionPrefix, remoteKey, config.CampaignID(base.Campaign), sessionID)
|
||||
if err != nil {
|
||||
return nil, missingSessionConfigError(config.DefaultSessionConfigSearchPaths, err.Error())
|
||||
return nil, fmt.Errorf("discover remote session: %w", err)
|
||||
}
|
||||
defer body.Close()
|
||||
defer func() { err = errors.Join(err, body.Close()) }()
|
||||
remoteKey = sessionInfo.Key
|
||||
temporary, err := os.CreateTemp("", "narratio-session-*.yml")
|
||||
if err != nil {
|
||||
return nil, missingSessionConfigError(config.DefaultSessionConfigSearchPaths, fmt.Sprintf("remote session %q download failed: %v", remoteKey, err))
|
||||
return nil, fmt.Errorf("remote session %q download failed: %w", remoteKey, err)
|
||||
}
|
||||
sessionTempPath := temporary.Name()
|
||||
cleanup = func() error {
|
||||
@@ -134,23 +136,12 @@ func loadCommandConfigWithDependencies(ctx context.Context, pipelineFlag, campai
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cfg, err := config.ResolveLoadedPipelineCampaign(
|
||||
*base,
|
||||
sessionTempPath,
|
||||
sessionCfg,
|
||||
config.SessionSource{
|
||||
Source: "session_config.s3",
|
||||
LocalPath: sessionTempPath,
|
||||
S3Bucket: s3BucketName(base.Pipeline),
|
||||
S3Key: remoteKey,
|
||||
S3Size: sessionInfo.Size,
|
||||
S3ETag: sessionInfo.ETag,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
source := "session_config.s3"
|
||||
if checksum != "" {
|
||||
source = "session_config.s3.committed_snapshot"
|
||||
}
|
||||
loaded = &commandConfig{Config: cfg, cleanup: cleanup}
|
||||
loaded = &acquiredSession{Config: sessionCfg, Path: sessionTempPath, cleanup: cleanup,
|
||||
Source: config.SessionSource{Source: source, LocalPath: sessionTempPath, S3Bucket: s3BucketName(base.Pipeline), S3Key: remoteKey, S3Size: sessionInfo.Size, S3ETag: sessionInfo.ETag}}
|
||||
cleanup = nil
|
||||
return loaded, nil
|
||||
}
|
||||
@@ -169,9 +160,9 @@ func loadPipelineCampaignConfigWithDependencies(pipelineFlag, campaignFlag, camp
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if selectedID := strings.TrimSpace(campaignFlag); selectedID != "" && strings.TrimSpace(campaignFileFlag) == "" {
|
||||
if selectedID := strings.TrimSpace(campaignFlag); selectedID != "" {
|
||||
if got := config.CampaignID(campaignCfg); got != selectedID {
|
||||
return nil, fmt.Errorf("campaign config %q invalid: campaign_id %q does not match selected campaign %q", resolvedCampaignPath, got, selectedID)
|
||||
return nil, &config.SessionIdentityError{Source: resolvedCampaignPath, Field: "campaign_id", Expected: selectedID, Actual: got}
|
||||
}
|
||||
}
|
||||
loaded, err := config.LoadPipelineCampaign(loadedPipelinePath, pipelineCfg, resolvedCampaignPath, campaignCfg)
|
||||
@@ -236,8 +227,7 @@ func findRemoteSessionConfig(ctx context.Context, store storage.ObjectStore, ses
|
||||
}
|
||||
if selected != nil {
|
||||
if info.Size != selected.Size || strings.TrimSpace(info.ETag) != selected.Generation {
|
||||
_ = body.Close()
|
||||
return storage.ObjectInfo{}, "", nil, fmt.Errorf("remote session %q differs from committed object identity", remoteKey)
|
||||
return storage.ObjectInfo{}, "", nil, errors.Join(fmt.Errorf("remote session %q differs from committed object identity", remoteKey), body.Close())
|
||||
}
|
||||
return info, selected.SHA256, body, nil
|
||||
}
|
||||
|
||||
259
internal/app/configuration_acquisition.go
Normal file
259
internal/app/configuration_acquisition.go
Normal file
@@ -0,0 +1,259 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||||
"go.maximumdirect.net/commandkit"
|
||||
)
|
||||
|
||||
// configurationAcquisition belongs to one parsed selection, including one side
|
||||
// of a comparison. It acquires only reached documents and retains native results.
|
||||
type configurationAcquisition struct {
|
||||
selection configurationSelection
|
||||
deps commandDependencies
|
||||
pipeline *config.PipelineConfig
|
||||
pipelinePath string
|
||||
campaign *config.CampaignConfig
|
||||
campaignPath string
|
||||
party *config.ResolvedParty
|
||||
session *acquiredSession
|
||||
previousSessionID string
|
||||
domain *config.Config
|
||||
}
|
||||
|
||||
func newConfigurationAcquisition(selection configurationSelection, deps commandDependencies) *configurationAcquisition {
|
||||
return &configurationAcquisition{selection: selection, deps: normalizeCommandDependencies(deps)}
|
||||
}
|
||||
|
||||
func selectedString(value commandkit.Supplied[string]) *string {
|
||||
if !value.Set {
|
||||
return nil
|
||||
}
|
||||
result := value.Value
|
||||
return &result
|
||||
}
|
||||
|
||||
func checkConfigurationSelection(selection configurationSelection) error {
|
||||
for _, field := range []struct {
|
||||
name string
|
||||
value commandkit.Supplied[string]
|
||||
}{
|
||||
{"config", selection.Config}, {"campaign", selection.Campaign},
|
||||
{"campaign-file", selection.CampaignFile}, {"session", selection.Session},
|
||||
{"session-file", selection.SessionFile}, {"party-file", selection.PartyFile},
|
||||
{"secrets-dir", selection.SecretsDir},
|
||||
} {
|
||||
if field.value.Set && strings.TrimSpace(field.value.Value) == "" {
|
||||
return &inputError{message: fmt.Sprintf("--%s requires a non-empty value", field.name)}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *configurationAcquisition) bootstrap(ctx context.Context) error {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := checkConfigurationSelection(a.selection); err != nil {
|
||||
return err
|
||||
}
|
||||
if !a.deps.secretsBootstrap.selectionSet {
|
||||
a.deps.secretsBootstrap.selection = selectedString(a.selection.SecretsDir)
|
||||
a.deps.secretsBootstrap.selectionSet = true
|
||||
}
|
||||
return a.deps.secretsBootstrap.load(a.deps.loadSecretsDir, a.deps.diagnostics)
|
||||
}
|
||||
|
||||
func (a *configurationAcquisition) Pipeline(ctx context.Context) (*config.PipelineConfig, error) {
|
||||
if err := a.bootstrap(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if a.pipeline != nil {
|
||||
return a.pipeline, nil
|
||||
}
|
||||
path, cfg, err := loadPipelineConfigWithDependencies(a.selection.Config.Value,
|
||||
config.PipelineLoadOptions{Overlay: selectedString(a.selection.Overlay)},
|
||||
selectedString(a.selection.SecretsDir), a.deps)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
a.pipeline, a.pipelinePath = cfg, path
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func (a *configurationAcquisition) Campaign(ctx context.Context) (*config.CampaignConfig, error) {
|
||||
if err := a.bootstrap(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if a.campaign != nil {
|
||||
return a.campaign, nil
|
||||
}
|
||||
path := a.selection.CampaignFile.Value
|
||||
if !a.selection.CampaignFile.Set {
|
||||
pipeline, err := a.Pipeline(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
path, err = resolveCampaignConfigPath(pipeline, a.selection.Campaign.Value, "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
path, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cfg, err := config.LoadCampaign(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if a.selection.Campaign.Set && config.CampaignID(cfg) != a.selection.Campaign.Value {
|
||||
return nil, &config.SessionIdentityError{Source: path, Field: "campaign_id", Expected: a.selection.Campaign.Value, Actual: config.CampaignID(cfg)}
|
||||
}
|
||||
a.campaign, a.campaignPath = cfg, path
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func (a *configurationAcquisition) Party(ctx context.Context) (*config.ResolvedParty, error) {
|
||||
if err := a.bootstrap(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if a.party != nil {
|
||||
return a.party, nil
|
||||
}
|
||||
var campaign *config.CampaignConfig
|
||||
if !a.selection.PartyFile.Set {
|
||||
var err error
|
||||
campaign, err = a.Campaign(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
party, err := config.ResolveParty(a.campaignPath, campaign, selectedString(a.selection.PartyFile))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
a.party = &party
|
||||
return a.party, nil
|
||||
}
|
||||
|
||||
func (a *configurationAcquisition) Session(ctx context.Context) (*config.SessionConfig, error) {
|
||||
if err := a.bootstrap(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if a.session != nil {
|
||||
return a.session.Config, nil
|
||||
}
|
||||
campaign, err := a.Campaign(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
base := &config.LoadedPipelineCampaign{Campaign: campaign, CampaignPath: a.campaignPath}
|
||||
opts := config.SessionLoadOptions{Campaign: campaign, CampaignPath: a.campaignPath, SessionID: a.selection.Session.Value, PreviousSessionID: a.previousSessionID}
|
||||
// Local inspection needs no pipeline or derived catalog. Remote discovery
|
||||
// acquires storage configuration only after local absence is established.
|
||||
if a.selection.SessionFile.Set {
|
||||
path, err := filepath.Abs(a.selection.SessionFile.Value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
session, err := config.LoadSessionWithOptions(path, opts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
a.session = &acquiredSession{Config: session, Path: path, Source: config.SessionSource{Source: "session_config", LocalPath: path}}
|
||||
return session, nil
|
||||
}
|
||||
session, path, err := config.DiscoverSession(config.DefaultSessionConfigSearchPaths, opts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if session != nil {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
a.session = &acquiredSession{Config: session, Path: path, Source: config.SessionSource{Source: "session_config", LocalPath: path}}
|
||||
return session, nil
|
||||
}
|
||||
if a.selection.Session.Value == "" {
|
||||
return nil, missingSessionConfigError(config.DefaultSessionConfigSearchPaths, "remote session loading requires a session identity")
|
||||
}
|
||||
pipeline, err := a.Pipeline(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
base.Pipeline, base.PipelinePath = pipeline, a.pipelinePath
|
||||
loaded, err := loadRemoteSessionConfig(ctx, base, opts, a.deps)
|
||||
if err != nil {
|
||||
if loaded != nil && loaded.cleanup != nil {
|
||||
err = errors.Join(err, loaded.cleanup())
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
a.session = loaded
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, errors.Join(err, a.Close())
|
||||
}
|
||||
return loaded.Config, nil
|
||||
}
|
||||
|
||||
func (a *configurationAcquisition) Domain(ctx context.Context) (*config.Config, error) {
|
||||
if err := a.bootstrap(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if a.domain != nil {
|
||||
return a.domain, nil
|
||||
}
|
||||
pipeline, err := a.Pipeline(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
campaign, err := a.Campaign(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
session, err := a.Session(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
party, err := a.Party(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
base, err := config.LoadPipelineCampaignWithParty(a.pipelinePath, pipeline, a.campaignPath, campaign, *party)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cfg, err := config.ResolveLoadedPipelineCampaign(base, a.session.Path, session, a.session.Source)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
a.domain = cfg
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func (a *configurationAcquisition) Close() error {
|
||||
if a.session == nil || a.session.cleanup == nil {
|
||||
return nil
|
||||
}
|
||||
cleanup := a.session.cleanup
|
||||
a.session.cleanup = nil
|
||||
return cleanup()
|
||||
}
|
||||
198
internal/app/configuration_acquisition_test.go
Normal file
198
internal/app/configuration_acquisition_test.go
Normal file
@@ -0,0 +1,198 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/adapters/storage"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||||
"go.maximumdirect.net/commandkit"
|
||||
configkit "go.maximumdirect.net/configkit"
|
||||
)
|
||||
|
||||
func suppliedSelection(value string) commandkit.Supplied[string] {
|
||||
return commandkit.Supplied[string]{Value: value, Set: true}
|
||||
}
|
||||
|
||||
func TestConfigurationAcquisitionLoadsOnlyReachedDocuments(t *testing.T) {
|
||||
_, campaignPath, _ := writeValidConfigFiles(t, t.TempDir())
|
||||
pipelineLoads, bootstrapCalls := 0, 0
|
||||
deps := commandDependencies{
|
||||
loadPipelineConfig: func(string, config.PipelineLoadOptions) (*config.PipelineConfig, error) {
|
||||
pipelineLoads++
|
||||
return nil, errors.New("unavailable pipeline")
|
||||
},
|
||||
loadSecretsDir: func(string) (*configkit.SecretsReport, error) {
|
||||
bootstrapCalls++
|
||||
return nil, nil
|
||||
},
|
||||
}
|
||||
selection := configurationSelection{
|
||||
CampaignFile: suppliedSelection(campaignPath),
|
||||
Campaign: suppliedSelection("sample-campaign"),
|
||||
SecretsDir: suppliedSelection(t.TempDir()),
|
||||
}
|
||||
acquired := newConfigurationAcquisition(selection, deps)
|
||||
campaign, err := acquired.Campaign(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
repeated, err := acquired.Campaign(context.Background())
|
||||
if err != nil || repeated != campaign {
|
||||
t.Fatalf("campaign was not retained: %v", err)
|
||||
}
|
||||
if campaign.ConfigKitResult() == nil {
|
||||
t.Fatal("missing native campaign result")
|
||||
}
|
||||
party, err := acquired.Party(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if party.Canonical.ConfigKitResult() == nil {
|
||||
t.Fatal("missing native party result")
|
||||
}
|
||||
if pipelineLoads != 0 || bootstrapCalls != 1 {
|
||||
t.Fatalf("pipeline loads=%d bootstrap calls=%d", pipelineLoads, bootstrapCalls)
|
||||
}
|
||||
explicit := newConfigurationAcquisition(configurationSelection{PartyFile: suppliedSelection(party.Source.Path)}, deps)
|
||||
if _, err := explicit.Party(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if pipelineLoads != 0 {
|
||||
t.Fatal("explicit party acquired pipeline")
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigurationAcquisitionIdentityAndExplicitEmpty(t *testing.T) {
|
||||
_, campaignPath, _ := writeValidConfigFiles(t, t.TempDir())
|
||||
acquired := newConfigurationAcquisition(configurationSelection{
|
||||
CampaignFile: suppliedSelection(campaignPath), Campaign: suppliedSelection("other-campaign"),
|
||||
}, commandDependencies{})
|
||||
_, err := acquired.Campaign(context.Background())
|
||||
var identity *config.SessionIdentityError
|
||||
if !errors.As(err, &identity) || identity.Field != "campaign_id" {
|
||||
t.Fatalf("identity error=%v", err)
|
||||
}
|
||||
acquired = newConfigurationAcquisition(configurationSelection{CampaignFile: suppliedSelection("")}, commandDependencies{})
|
||||
_, err = acquired.Campaign(context.Background())
|
||||
var input *inputError
|
||||
if !errors.As(err, &input) {
|
||||
t.Fatalf("empty explicit selection error=%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigurationAcquisitionRetainsBootstrapFailure(t *testing.T) {
|
||||
failure := errors.New("bootstrap unavailable")
|
||||
calls := 0
|
||||
acquired := newConfigurationAcquisition(configurationSelection{SecretsDir: suppliedSelection(t.TempDir())}, commandDependencies{
|
||||
loadSecretsDir: func(string) (*configkit.SecretsReport, error) { calls++; return nil, failure },
|
||||
})
|
||||
for range 2 {
|
||||
if _, err := acquired.Pipeline(context.Background()); !errors.Is(err, failure) {
|
||||
t.Fatalf("bootstrap error=%v", err)
|
||||
}
|
||||
}
|
||||
if calls != 1 {
|
||||
t.Fatalf("bootstrap calls=%d", calls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionAcquisitionUsesExactFileWithoutPipelineOrParty(t *testing.T) {
|
||||
_, campaign, session := writeValidConfigFiles(t, t.TempDir())
|
||||
acquired := newConfigurationAcquisition(configurationSelection{
|
||||
CampaignFile: suppliedSelection(campaign), SessionFile: suppliedSelection(session),
|
||||
}, commandDependencies{loadPipelineConfig: func(string, config.PipelineLoadOptions) (*config.PipelineConfig, error) {
|
||||
t.Fatal("local session inspection acquired pipeline")
|
||||
return nil, nil
|
||||
}})
|
||||
got, err := acquired.Session(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got.SessionID != "2026-05-03" || got.ConfigKitResult() == nil || acquired.party != nil {
|
||||
t.Fatalf("session=%#v party=%#v", got, acquired.party)
|
||||
}
|
||||
for _, file := range []string{session, filepath.Join(t.TempDir(), "missing.yml")} {
|
||||
selected := acquired.selection
|
||||
selected.SessionFile = suppliedSelection(file)
|
||||
selected.Session = suppliedSelection("wrong-identity")
|
||||
next := newConfigurationAcquisition(selected, acquired.deps)
|
||||
if _, err := next.Session(context.Background()); err == nil {
|
||||
t.Fatal("explicit selection error was ignored")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionAcquisitionRemoteIsReadOnlyAndReleasesDownload(t *testing.T) {
|
||||
pipeline, campaign, _ := writeValidConfigFiles(t, t.TempDir())
|
||||
fake := &storage.FakeBackend{}
|
||||
seedRemoteSessionConfig(t, fake, "2026-05-03", "session_id: 2026-05-03\ninputs: {audio_s3: {prefix: audio/}}\n")
|
||||
setSessionConfigSearchPaths(t, []string{filepath.Join(t.TempDir(), "absent.yml")})
|
||||
deps, calls := remoteSessionLoaderDependencies(fake)
|
||||
loads := 0
|
||||
deps.loadPipelineConfig = func(path string, opts config.PipelineLoadOptions) (*config.PipelineConfig, error) {
|
||||
loads++
|
||||
return config.LoadPipelineWithOptions(path, opts)
|
||||
}
|
||||
acquired := newConfigurationAcquisition(configurationSelection{Config: suppliedSelection(pipeline), CampaignFile: suppliedSelection(campaign), Session: suppliedSelection("2026-05-03")}, deps)
|
||||
got, err := acquired.Session(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if acquired.party != nil || acquired.domain != nil || got.ConfigKitResult() == nil {
|
||||
t.Fatal("session inspection derived domain state or lost native result")
|
||||
}
|
||||
if _, err := acquired.Session(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if loads != 1 || *calls != 1 {
|
||||
t.Fatalf("pipeline loads=%d storage calls=%d", loads, *calls)
|
||||
}
|
||||
temporary := acquired.session.Path
|
||||
if _, err := os.Stat(temporary); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := acquired.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := acquired.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := os.Stat(temporary); !errors.Is(err, os.ErrNotExist) {
|
||||
t.Fatalf("download remains: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionAcquisitionInvalidDiscoveryNeverReadsRemote(t *testing.T) {
|
||||
_, campaign, _ := writeValidConfigFiles(t, t.TempDir())
|
||||
invalid := filepath.Join(t.TempDir(), "invalid.yml")
|
||||
if err := os.WriteFile(invalid, []byte("unexpected: value\n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
setSessionConfigSearchPaths(t, []string{invalid})
|
||||
acquired := newConfigurationAcquisition(configurationSelection{CampaignFile: suppliedSelection(campaign), Session: suppliedSelection("2026-05-03")}, commandDependencies{loadPipelineConfig: func(string, config.PipelineLoadOptions) (*config.PipelineConfig, error) {
|
||||
t.Fatal("invalid local discovery triggered remote acquisition")
|
||||
return nil, nil
|
||||
}})
|
||||
if _, err := acquired.Session(context.Background()); err == nil {
|
||||
t.Fatal("invalid local content accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionAcquisitionPreservesRemoteFailureCause(t *testing.T) {
|
||||
pipeline, campaign, _ := writeValidConfigFiles(t, t.TempDir())
|
||||
setSessionConfigSearchPaths(t, []string{filepath.Join(t.TempDir(), "absent.yml")})
|
||||
failure := errors.New("storage unavailable")
|
||||
acquired := newConfigurationAcquisition(configurationSelection{Config: suppliedSelection(pipeline), CampaignFile: suppliedSelection(campaign), Session: suppliedSelection("2026-05-03")}, commandDependencies{
|
||||
newObjectStore: func(context.Context, *config.Config) (storage.ObjectStore, error) { return nil, failure },
|
||||
})
|
||||
if _, err := acquired.Session(context.Background()); !errors.Is(err, failure) {
|
||||
t.Fatalf("remote failure cause=%v", err)
|
||||
}
|
||||
if err := acquired.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
@@ -16,14 +16,19 @@ type secretsBootstrap struct {
|
||||
selection *string
|
||||
selectionSet bool
|
||||
called bool
|
||||
err error
|
||||
}
|
||||
|
||||
var conventionalSecretsDirectory = "/usr/local/etc/narratio/secrets"
|
||||
|
||||
func (b *secretsBootstrap) load(loader func(string) (*configkit.SecretsReport, error), diagnostics io.Writer) error {
|
||||
if b == nil || b.called {
|
||||
func (b *secretsBootstrap) load(loader func(string) (*configkit.SecretsReport, error), diagnostics io.Writer) (err error) {
|
||||
if b == nil {
|
||||
return nil
|
||||
}
|
||||
if b.called {
|
||||
return b.err
|
||||
}
|
||||
defer func() { b.err = err }()
|
||||
b.called = true
|
||||
directory, required, err := selectedSecretsDirectory(b.selection)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user