Files
ai_academy/internal/application/service/datasource_resync_test.go
T
runny 9058149576
Build & Publish (dsh-academy) / End-to-end inside dsh (latest, advisory) (push) Skipped
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Frontend / Docker multi-stage image (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-academy) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-academy) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-academy) / Publish to npm (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Helm / chart (push) Canceled after 0s
refactor: 项目更名 WeKnora → Academy
- 全量替换代码/配置/文档中的项目名(保留上游 GitHub 链接、weixin 文档域名、Docker Hub 镜像名)
- 重命名 32 个含项目名的文件/目录;Go module 路径改为 academy(含 client/cli 子模块)
- docreader proto 描述符采用等长替换,保持序列化字节布局有效
- Dockerfile.app 构建网络修复: browserskill 离线产物通道、PyPI/apt 国内镜像
- 运行验证: go build ✓ docker compose up ✓ 健康检查 ✓
2026-11-13 20:52:51 +08:00

223 lines
8.3 KiB
Go

package service
import (
"context"
"encoding/json"
"errors"
"testing"
"time"
"academy/internal/datasource"
"academy/internal/types"
"github.com/hibiken/asynq"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// ─────────────────────────────────────────────────────────────────────────────
// Feishu deep-adaptation legacy upgrade: legacy feishu data sources carry a
// one-shot Settings["resync_required"] marker (stamped by migration 000122 /
// sqlite 000040). The sync entry upgrades such a sync — even a scheduled
// incremental one — to a full pass and clears the marker on success.
// ─────────────────────────────────────────────────────────────────────────────
// resyncStreamConnector records the start cursor each FetchStream received, so
// tests can assert whether the sync ran full (nil cursor) or incremental.
type resyncStreamConnector struct {
startCursors []*types.SyncCursor
failFetch bool
}
func (c *resyncStreamConnector) Type() string { return types.ConnectorTypeFeishu }
func (c *resyncStreamConnector) Validate(context.Context, *types.DataSourceConfig) error {
return nil
}
func (c *resyncStreamConnector) ListResources(
context.Context, *types.DataSourceConfig, string,
) ([]types.Resource, error) {
return nil, nil
}
func (c *resyncStreamConnector) ResolveResourceAncestors(
context.Context, *types.DataSourceConfig, []string,
) ([]string, error) {
return nil, nil
}
func (c *resyncStreamConnector) FetchAll(
context.Context, *types.DataSourceConfig, []string,
) ([]types.FetchedItem, error) {
return nil, nil
}
func (c *resyncStreamConnector) FetchIncremental(
context.Context, *types.DataSourceConfig, *types.SyncCursor,
) ([]types.FetchedItem, *types.SyncCursor, error) {
return nil, nil, nil
}
func (c *resyncStreamConnector) FetchStream(
_ context.Context, _ *types.DataSourceConfig, cursor *types.SyncCursor, _ datasource.StreamHandler,
) (*types.SyncCursor, error) {
c.startCursors = append(c.startCursors, cursor)
if c.failFetch {
return nil, errors.New("fetch failed")
}
return &types.SyncCursor{ConnectorCursor: map[string]interface{}{"space_node_times": map[string]interface{}{}}}, nil
}
// resyncDSRepo captures Config-writing Update calls (marker clear).
type resyncDSRepo struct {
kbDeleteDSRepo
updates int
configs []types.JSON
syncStates int
}
func (r *resyncDSRepo) Update(_ context.Context, ds *types.DataSource) error {
r.updates++
r.configs = append(r.configs, ds.Config)
return nil
}
func (r *resyncDSRepo) UpdateSyncState(_ context.Context, _ *types.DataSource) error {
r.syncStates++
return nil
}
// newResyncHarness wires a DataSourceService around the streaming fake for a
// feishu data source whose stored Config carries the given Settings.
func newResyncHarness(
t *testing.T, settings map[string]interface{}, conn *resyncStreamConnector,
) (*DataSourceService, *types.DataSource, *resyncDSRepo, *types.SyncLog) {
t.Helper()
cfg := &types.DataSourceConfig{
Type: types.ConnectorTypeFeishu,
Credentials: map[string]interface{}{"app_id": "cli-x", "app_secret": "sec"},
ResourceIDs: []string{"space1"},
Settings: settings,
}
configJSON, err := cfg.ToJSON()
require.NoError(t, err)
ds := &types.DataSource{
ID: "ds-resync",
TenantID: 1,
KnowledgeBaseID: "kb-1",
Name: "Feishu Legacy",
Type: types.ConnectorTypeFeishu,
Config: configJSON,
SyncMode: types.SyncModeIncremental,
Status: types.DataSourceStatusActive,
}
syncLog := &types.SyncLog{
ID: "log-resync",
DataSourceID: ds.ID,
TenantID: ds.TenantID,
Status: types.SyncLogStatusRunning,
StartedAt: time.Now().UTC(),
}
dsRepo := &resyncDSRepo{kbDeleteDSRepo: *newKBDeleteDSRepo(ds.KnowledgeBaseID, ds)}
registry := datasource.NewConnectorRegistry()
require.NoError(t, registry.Register(conn))
svc := &DataSourceService{
dsRepo: dsRepo,
syncLogRepo: &processSyncSyncLogRepo{
logs: map[string]*types.SyncLog{syncLog.ID: syncLog},
},
kbService: &processSyncKBService{
kb: &types.KnowledgeBase{ID: ds.KnowledgeBaseID, TenantID: ds.TenantID},
},
connectorRegistry: registry,
tenantRepo: &processSyncTenantRepo{tenant: &types.Tenant{ID: ds.TenantID}},
tagService: &processSyncTagService{},
}
return svc, ds, dsRepo, syncLog
}
func runResyncSync(t *testing.T, svc *DataSourceService, ds *types.DataSource, syncLog *types.SyncLog) *types.SyncLog {
t.Helper()
payload, err := json.Marshal(types.DataSourceSyncPayload{
DataSourceID: ds.ID,
TenantID: ds.TenantID,
SyncLogID: syncLog.ID,
})
require.NoError(t, err)
require.NoError(t, svc.ProcessSync(context.Background(), asynq.NewTask(types.TypeDataSourceSync, payload)))
return svc.syncLogRepo.(*processSyncSyncLogRepo).logs[syncLog.ID]
}
// settingsOf decodes a stored Config blob and returns its Settings map.
func settingsOf(t *testing.T, blob types.JSON) map[string]interface{} {
t.Helper()
var raw struct {
Settings map[string]interface{} `json:"settings"`
}
require.NoError(t, json.Unmarshal(blob, &raw))
return raw.Settings
}
// A data source armed with the migration's resync_required marker runs FULL
// even though it is incremental, and the marker is cleared once the run
// succeeds.
func TestProcessSync_ResyncMarkerRunsFullOnceAndClears(t *testing.T) {
conn := &resyncStreamConnector{}
svc, ds, dsRepo, syncLog := newResyncHarness(t, map[string]interface{}{"resync_required": true}, conn)
log := runResyncSync(t, svc, ds, syncLog)
assert.Equal(t, types.SyncLogStatusSuccess, log.Status)
// Full upgrade: the incremental sync started with no cursor.
require.Len(t, conn.startCursors, 1)
assert.Nil(t, conn.startCursors[0], "resync_required must upgrade the incremental sync to a full pass")
// One Config write: clearing the marker on success.
require.Equal(t, 1, dsRepo.updates)
final := settingsOf(t, dsRepo.configs[0])
assert.NotContains(t, final, "resync_required", "marker must be cleared after a successful upgrade")
}
// A data source without the marker stays incremental: no config writes.
func TestProcessSync_NoMarkerStaysIncremental(t *testing.T) {
conn := &resyncStreamConnector{}
svc, ds, dsRepo, syncLog := newResyncHarness(t, map[string]interface{}{"parse_mode": "export"}, conn)
ds.LastSyncCursor = makeConnectorCursor(t, map[string]map[string]string{"space1": {"nt1": "100"}})
log := runResyncSync(t, svc, ds, syncLog)
assert.Equal(t, types.SyncLogStatusSuccess, log.Status)
require.Len(t, conn.startCursors, 1)
assert.NotNil(t, conn.startCursors[0], "no marker must keep the sync incremental")
assert.Equal(t, 0, dsRepo.updates, "no backfill or marker write may happen when parse_mode exists")
}
// A failed upgraded sync keeps the marker so the next run retries the full
// upgrade instead of silently continuing incremental.
func TestProcessSync_FailedResyncKeepsMarker(t *testing.T) {
conn := &resyncStreamConnector{failFetch: true}
svc, ds, dsRepo, syncLog := newResyncHarness(t, map[string]interface{}{"resync_required": true}, conn)
payload, err := json.Marshal(types.DataSourceSyncPayload{
DataSourceID: ds.ID,
TenantID: ds.TenantID,
SyncLogID: syncLog.ID,
})
require.NoError(t, err)
assert.Error(t, svc.ProcessSync(context.Background(), asynq.NewTask(types.TypeDataSourceSync, payload)))
// No config writes on the failed run: the marker stays armed.
assert.Equal(t, 0, dsRepo.updates)
}
// resyncRequired tolerates the encodings the marker may be written in.
func TestResyncRequired_Encodings(t *testing.T) {
assert.False(t, resyncRequired(nil))
assert.False(t, resyncRequired(&types.DataSourceConfig{}))
assert.False(t, resyncRequired(&types.DataSourceConfig{Settings: map[string]interface{}{"resync_required": false}}))
assert.False(t, resyncRequired(&types.DataSourceConfig{Settings: map[string]interface{}{"resync_required": "no"}}))
assert.True(t, resyncRequired(&types.DataSourceConfig{Settings: map[string]interface{}{"resync_required": true}}))
assert.True(t, resyncRequired(&types.DataSourceConfig{Settings: map[string]interface{}{"resync_required": "true"}}))
}