修改配置编辑不了的问题
This commit is contained in:
1 parent
22b504e53e
commit
2a2f78c81e
42 files changed
+1082
-178
No files matched your search
@@ -3,7 +3,10 @@ package application
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
@@ -12,10 +15,11 @@ import (
|
||||
|
||||
"git.nianxx.cn/wangxuming/NianAIGC/backend/internal/assets"
|
||||
"git.nianxx.cn/wangxuming/NianAIGC/backend/internal/billing"
|
||||
"git.nianxx.cn/wangxuming/NianAIGC/backend/internal/providers"
|
||||
"git.nianxx.cn/wangxuming/NianAIGC/backend/internal/settings"
|
||||
)
|
||||
|
||||
func TestDefaultSettingsServicePersistsRestartRequiredWithoutPartialHotReload(t *testing.T) {
|
||||
func TestDefaultSettingsServiceAppliesProviderSettingsWithoutRestart(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), ".env.local")
|
||||
key := "IMAGE_GENERATE_ENGINE"
|
||||
service := defaultSettingsService(func(name string) string {
|
||||
@@ -29,7 +33,7 @@ func TestDefaultSettingsServicePersistsRestartRequiredWithoutPartialHotReload(t
|
||||
t.Fatal(err)
|
||||
}
|
||||
payload, ok := value.(settings.Payload)
|
||||
if !ok || !payload.RestartRequired {
|
||||
if !ok || payload.RestartRequired {
|
||||
t.Fatalf("payload=%#v", value)
|
||||
}
|
||||
}
|
||||
@@ -78,6 +82,7 @@ func TestRuntimeHealthDetailsMatchTypeScriptDefaultsAndConfiguredModes(t *testin
|
||||
"VOLCENGINE_ACCESS_KEY_ID": "access",
|
||||
"VOLCENGINE_SECRET_ACCESS_KEY": "secret",
|
||||
"SEEDANCE_API_KEY": "seedance-key",
|
||||
"VIDEO_GENERATE_ENGINE": "seedance",
|
||||
"ZHINIAN_AUTH_REQUIRED": "true",
|
||||
"ZHINIAN_AUTH_SESSION_SECRET": "session-secret",
|
||||
}
|
||||
@@ -95,6 +100,88 @@ func TestRuntimeHealthDetailsMatchTypeScriptDefaultsAndConfiguredModes(t *testin
|
||||
}
|
||||
}
|
||||
|
||||
func TestRuntimeProviderJobBuilderRefreshesDatabaseSettings(t *testing.T) {
|
||||
settingsPath := filepath.Join(t.TempDir(), ".env.local")
|
||||
fallback := func(name string) string {
|
||||
if name == "ZHINIAN_SETTINGS_FILE" {
|
||||
return settingsPath
|
||||
}
|
||||
return ""
|
||||
}
|
||||
repository := &applicationRuntimeSettingsRepository{values: map[string]string{
|
||||
"EVOLINK_API_KEY": "evolink-secret",
|
||||
"EVOLINK_IMAGE_MODEL": "database-image-model",
|
||||
"EVOLINK_IMAGE_QUALITY": "high",
|
||||
"IMAGE_GENERATE_ENGINE": "evolink",
|
||||
}}
|
||||
service := databaseSettingsService(fallback, repository)
|
||||
builder := runtimeProviderJobBuilder{fallback: fallback, settings: service, enforceAvailability: true}
|
||||
command, err := builder.Build(context.Background(), "owner", "", "image.generate", "", map[string]any{"prompt": "draw"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var request providers.Request
|
||||
if err := json.Unmarshal(command.Job.RequestPayload, &request); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if command.Job.Provider != "evolink" || command.Job.ReqKey != "database-image-model" || request.Settings["quality"] != "high" {
|
||||
t.Fatalf("job=%#v request=%#v", command.Job, request)
|
||||
}
|
||||
repository.values = map[string]string{
|
||||
"BAILIAN_API_KEY": "bailian-secret",
|
||||
"BAILIAN_IMAGE_MODEL": "database-bailian-model",
|
||||
"IMAGE_GENERATE_ENGINE": "bailian",
|
||||
}
|
||||
command, err = builder.Build(context.Background(), "owner", "", "image.generate", "", map[string]any{"prompt": "draw again"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if command.Job.Provider != "bailian" || command.Job.ReqKey != "database-bailian-model" {
|
||||
t.Fatalf("refreshed job=%#v", command.Job)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRuntimeProviderResolverUsesLatestDatabaseCredential(t *testing.T) {
|
||||
var authorizations []string
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, request *http.Request) {
|
||||
authorizations = append(authorizations, request.Header.Get("Authorization"))
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"id":"task-1","status":"queued"}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
settingsPath := filepath.Join(t.TempDir(), ".env.local")
|
||||
fallback := func(name string) string {
|
||||
if name == "ZHINIAN_SETTINGS_FILE" {
|
||||
return settingsPath
|
||||
}
|
||||
return ""
|
||||
}
|
||||
repository := &applicationRuntimeSettingsRepository{values: map[string]string{
|
||||
"EVOLINK_API_KEY": "database-secret-1",
|
||||
"EVOLINK_BASE_URL": server.URL,
|
||||
}}
|
||||
service := databaseSettingsService(fallback, repository)
|
||||
resolver := newRuntimeProviderResolver(fallback, service)
|
||||
adapter, err := resolver.Resolve(context.Background(), "evolink")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := adapter.Submit(context.Background(), providers.Request{Capability: "image.generate", Prompt: "draw"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
repository.values["EVOLINK_API_KEY"] = "database-secret-2"
|
||||
adapter, err = resolver.Resolve(context.Background(), "evolink")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := adapter.Submit(context.Background(), providers.Request{Capability: "image.generate", Prompt: "draw again"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(authorizations, []string{"Bearer database-secret-1", "Bearer database-secret-2"}) {
|
||||
t.Fatalf("authorizations=%#v", authorizations)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRuntimeHealthDetailsReportConfiguredProvidersAndImageEngine(t *testing.T) {
|
||||
values := map[string]string{
|
||||
"IMAGE_GENERATE_ENGINE": "bailian",
|
||||
@@ -110,31 +197,6 @@ func TestRuntimeHealthDetailsReportConfiguredProvidersAndImageEngine(t *testing.
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateProductionProviderConfigurationRequiresAllRealCredentials(t *testing.T) {
|
||||
if err := validateProductionProviderConfiguration(func(string) string { return "" }); err == nil {
|
||||
t.Fatal("validateProductionProviderConfiguration() unexpectedly succeeded")
|
||||
} else if !strings.Contains(err.Error(), "EVOLINK_API_KEY") || !strings.Contains(err.Error(), "SEEDANCE_API_KEY") {
|
||||
t.Fatalf("error = %v", err)
|
||||
}
|
||||
values := map[string]string{
|
||||
"VOLCENGINE_ACCESS_KEY_ID": "access",
|
||||
"VOLCENGINE_SECRET_ACCESS_KEY": "secret",
|
||||
"EVOLINK_API_KEY": "evolink",
|
||||
"BAILIAN_API_KEY": "bailian",
|
||||
"SEEDANCE_API_KEY": "seedance",
|
||||
}
|
||||
if err := validateProductionProviderConfiguration(func(name string) string { return values[name] }); err != nil {
|
||||
t.Fatalf("validateProductionProviderConfiguration() error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateProductionProviderConfigurationCanBeSkippedForBootstrap(t *testing.T) {
|
||||
values := map[string]string{"ZHINIAN_ALLOW_UNCONFIGURED_PROVIDERS": "true"}
|
||||
if err := validateProductionProviderConfiguration(func(name string) string { return values[name] }); err != nil {
|
||||
t.Fatalf("validateProductionProviderConfiguration() with bootstrap flag = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProviderUnavailableMessagesIdentifyOnlyMissingCredentials(t *testing.T) {
|
||||
values := map[string]string{
|
||||
"VOLCENGINE_ACCESS_KEY_ID": "access",
|
||||
@@ -214,6 +276,26 @@ func TestPrefixedBlobStoreKeepsApplicationStoragePathStable(t *testing.T) {
|
||||
|
||||
type recordingBlobStore struct{ putKey, readKey, deleteKey string }
|
||||
|
||||
type applicationRuntimeSettingsRepository struct{ values map[string]string }
|
||||
|
||||
func (repository *applicationRuntimeSettingsRepository) LoadRuntimeSettings(context.Context, []string) (map[string]string, error) {
|
||||
values := map[string]string{}
|
||||
for key, value := range repository.values {
|
||||
values[key] = value
|
||||
}
|
||||
return values, nil
|
||||
}
|
||||
|
||||
func (repository *applicationRuntimeSettingsRepository) SaveRuntimeSettings(_ context.Context, values map[string]string) error {
|
||||
if repository.values == nil {
|
||||
repository.values = map[string]string{}
|
||||
}
|
||||
for key, value := range values {
|
||||
repository.values[key] = value
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *recordingBlobStore) Put(_ context.Context, key string, _ io.Reader, _ int64, _ string) (assets.StoredObject, error) {
|
||||
s.putKey = key
|
||||
return assets.StoredObject{Key: key, URL: "https://cdn.example/" + key}, nil
|
||||
|
||||
Reference in new issue
Block a user