feat: publish sanitized custom.97 source

This commit is contained in:
cneicy committed 2026-10-27 20:43:39 +08:00
1 parent 8e6ed0317b
commit 86c0276156
1022 files changed
+69903 -19207

No files matched your search

+90 -7
View File
@@ -233,6 +233,75 @@ func TestExecuteTaskSubmissionPersistsPinnedPluginProvenance(t *testing.T) {
assert.Equal(t, "upstream-private", stored.PrivateData.UpstreamTaskID)
}
// A submit route declaring retainResult: false persists the task row for
// billing but never writes the upstream snapshot for an immediate terminal
// result, while the in-memory task still carries it for the presenter. An
// asynchronous result on the same route is retained because polling and
// retrieval depend on it.
func TestExecuteTaskSubmissionHonorsRouteRetainResult(t *testing.T) {
retainFalse := false
for _, tc := range []struct {
name string
immediate *relaycommon.TaskInfo
wantDiscarded bool
}{
{"immediate success is discarded", &relaycommon.TaskInfo{Status: model.TaskStatusSuccess, Progress: "100%"}, true},
{"immediate failure is discarded", &relaycommon.TaskInfo{Status: model.TaskStatusFailure, Reason: "rejected"}, true},
{"asynchronous result is retained", nil, false},
} {
t.Run(tc.name, func(t *testing.T) {
events := make([]string, 0, 3)
database, _ := openTaskDialectDatabase(t, &model.Task{}, &model.User{}, &model.Channel{})
previousDB := model.DB
model.DB = database
t.Cleanup(func() { model.DB = previousDB })
previousLogConsumeEnabled := common.LogConsumeEnabled
common.LogConsumeEnabled = false
t.Cleanup(func() { common.LogConsumeEnabled = previousLogConsumeEnabled })
c := taskSubmissionTestContext()
c.Set(pluginruntime.ContextKeyPinnedRoute, pluginruntime.PinnedRoute{
Plugin: &pluginruntime.LoadedPlugin{Meta: pluginruntime.Meta{Key: "sync-images"}},
Route: pluginruntime.Route{Method: http.MethodPost, Path: "/sync/images", Type: pluginruntime.RouteTypeSubmit, RetainResult: &retainFalse},
})
info := taskSubmissionRelayInfo(&taskSubmissionTestBilling{events: &events})
upstream := []byte(`{"data":[{"url":"https://cdn.example/a.png"}]}`)
outcome, taskErr := executeTaskSubmissionWith(c, info, func(*gin.Context, *relaycommon.RelayInfo) (*relay.TaskSubmitResult, *dto.TaskError) {
return &relay.TaskSubmitResult{
UpstreamTaskID: "task_public",
Platform: constant.TaskPlatform("sync-images"),
TaskData: upstream,
Immediate: tc.immediate,
}, nil
})
require.Nil(t, taskErr)
require.NotNil(t, outcome)
assert.JSONEq(t, string(upstream), string(outcome.Task.Data), "presenter keeps the in-memory snapshot")
assert.Equal(t, tc.wantDiscarded, outcome.Task.PrivateData.ResultDiscarded)
assert.Equal(t, !tc.wantDiscarded, outcome.Task.ResultRetrievable())
var stored model.Task
require.NoError(t, database.Where("task_id = ?", "task_public").First(&stored).Error)
assert.Equal(t, tc.wantDiscarded, stored.PrivateData.ResultDiscarded)
var nullCount int64
require.NoError(t, database.Model(&model.Task{}).Where("task_id = ? AND data IS NULL", "task_public").Count(&nullCount).Error)
if tc.wantDiscarded {
assert.Equal(t, int64(1), nullCount, "snapshot column must stay NULL")
artifacts, err := projectTaskArtifacts(&stored)
require.NoError(t, err)
assert.Empty(t, artifacts)
} else {
assert.Equal(t, int64(0), nullCount)
assert.JSONEq(t, string(upstream), string(stored.Data))
}
listed := model.TaskGetAllUserTask(1, 0, 10, model.SyncTaskQueryParams{})
require.Len(t, listed, 1)
assert.Empty(t, listed[0].Data, "task lists never select the snapshot column")
})
}
}
func TestExecuteTaskSubmissionRefundsCancellationBeforeDurableBarrier(t *testing.T) {
events := make([]string, 0, 2)
setupTaskSubmissionDatabase(t, true, &events)
@@ -357,17 +426,21 @@ func TestExecuteTaskSubmissionDisconnectAfterDurableInsertDoesNotRefund(t *testi
assert.False(t, c.Writer.Written())
}
// setupTaskSubmissionDatabase opens the dialect selected by
// TEST_TASK_DB_DIALECT (SQLite in memory by default) and records every task
// INSERT in events so tests can assert the reserve → insert → settle order.
// Without migrate the task table does not exist and inserts fail.
func setupTaskSubmissionDatabase(t *testing.T, migrate bool, events *[]string) *gorm.DB {
t.Helper()
previousDB := model.DB
database, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
require.NoError(t, err)
var models []any
if migrate {
models = append(models, &model.Task{})
}
database, _ := openTaskDialectDatabase(t, models...)
require.NoError(t, database.Callback().Create().Before("gorm:create").Register("test:task-submit-order", func(*gorm.DB) {
*events = append(*events, "insert")
}))
if migrate {
require.NoError(t, database.AutoMigrate(&model.Task{}))
}
model.DB = database
t.Cleanup(func() { model.DB = previousDB })
return database
@@ -398,7 +471,13 @@ func taskSubmissionRelayInfo(billing relaycommon.BillingSettler) *relaycommon.Re
// and consume log. Set TEST_TASK_DB_DIALECT plus TEST_MYSQL_DSN or
// TEST_POSTGRES_DSN to exercise the same contract on an external test database.
// Unique table prefixes keep the fixture isolated from all existing tables.
func TestImmediateTaskSettlementDatabase(t *testing.T) {
// openTaskDialectDatabase opens the engine selected by TEST_TASK_DB_DIALECT
// (default SQLite in memory; MySQL and PostgreSQL through TEST_MYSQL_DSN and
// TEST_POSTGRES_DSN) with a unique table prefix, migrates the given models and
// drops them on cleanup. It logs the engine version so database verification
// runs leave a record.
func openTaskDialectDatabase(t *testing.T, models ...any) (*gorm.DB, common.DatabaseType) {
t.Helper()
dialect := common.DatabaseType(os.Getenv("TEST_TASK_DB_DIALECT"))
var driver gorm.Dialector
switch dialect {
@@ -420,7 +499,6 @@ func TestImmediateTaskSettlementDatabase(t *testing.T) {
require.NoError(t, err)
sqlDB.SetMaxOpenConns(1)
t.Cleanup(func() { _ = sqlDB.Close() })
models := []any{&model.User{}, &model.Channel{}, &model.Task{}, &model.Log{}}
require.NoError(t, db.AutoMigrate(models...))
t.Cleanup(func() { require.NoError(t, db.Migrator().DropTable(models...)) })
var version string
@@ -430,6 +508,11 @@ func TestImmediateTaskSettlementDatabase(t *testing.T) {
require.NoError(t, db.Raw("SELECT version()").Scan(&version).Error)
}
t.Logf("database: %s %s", dialect, version)
return db, dialect
}
func TestImmediateTaskSettlementDatabase(t *testing.T) {
db, dialect := openTaskDialectDatabase(t, &model.User{}, &model.Channel{}, &model.Task{}, &model.Log{})
oldDB, oldLogDB := model.DB, model.LOG_DB
oldMain, oldLog := common.MainDatabaseType(), common.LogDatabaseType()
oldRedis, oldMemory, oldBatch, oldConsume, oldExport := common.RedisEnabled, common.MemoryCacheEnabled, common.BatchUpdateEnabled, common.LogConsumeEnabled, common.DataExportEnabled