feat(sources): 数据源配置收敛成 dws 导入 + PP 导入;「去掉来源」= 归档下架(不删数据)
build / build (push) Successful in 58s

主人要求:去掉入口,统一成 dws 导入与 PP 导入;其他来源连数据一起下架,之后用 dws 重新获取。

界面(web):
- 「数据源配置」只剩两块:**dws 导入(钉钉)**(按当前钉钉账号列出可访问的知识库与钉盘空间)
  与 **PP 导入**(PP 资料下载中心)。上一步加的「显示 AList/钉钉知识库来源」折叠开关删除。
- 来源卡片新增管理员可见的「去掉来源」(带确认弹窗,说明是归档下架、数据保留可恢复)。
- 侧栏文案改成"只留两个入口";发现面板标题改为「dws 导入(钉钉)」。

后端(归档语义,可逆):
- 迁移 000047:source_connections 增 archived_at / archived_by + 只覆盖未归档行的部分索引。
- store:ArchiveSourceConnection / RestoreSourceConnection(幂等;归档同时停用并清空
  next_refresh_at,避免被手工改回后立刻带着旧调度跑)。列表、目录、计划刷新(claimDue)、
  手动扫描(triggerBlock/inspect)四类查询一律排除归档行。
- 注册路径(dws 重新获取):RegisterDingtalkSpace 的 ON CONFLICT 清掉归档标记 ——
  重新登记同一个空间**原地复活同一行**,不产生重复,历史快照仍属于同一行。
- HTTP:POST /api/v1/source-connections/{id}/archive|restore(管理员专属,写意图头
  source-archive / source-restore + Idempotency-Key;404 区分"不存在"与"已是目标状态");
  openapi 同步两条契约。

验证:
- 集成测试(真 postgres):归档从列表消失但 source_items 一行不少、重复归档不漂移归档时间与人、
  未知 ID 报 ErrSourceConnectionNotFound、归档来源不能再被扫描、恢复后回到列表但保持停用;
  dws 重新登记同一空间复活同一行。
- httpapi:写意图/幂等键门禁、200/403/404/500、错误体不回显内部细节、classifyRole 管理员专属。
- 迁移护栏:000047 结构 + 四类 SQL 必须含 archived_at is null(漏一条就是入口没去掉)。
- make verify 15/15。
This commit is contained in:
姚章浩
2026-10-14 12:36:32 +08:00
parent 64b32c2da6
commit 2c7265efc4
20 changed files with 1426 additions and 281 deletions
+3
View File
@@ -11,6 +11,9 @@
### Changed
- 「系统设置 → 数据源配置」**收敛成两个入口**:**dws 导入**(按当前钉钉账号列出可访问的知识库与钉盘空间)与 **PP 导入**。AList(已由钉盘文件替代)、静态登记的钉钉知识库、本地手工导入与 PP legacy 都**不再出现在这一页**;已接入的这类来源用**归档**下架(`POST /api/v1/source-connections/{id}/archive`,管理员、幂等,带写意图头与幂等键):从列表、来源与目录、计划刷新与手动扫描里消失,**目录快照、发布记录与审计数据一行不删**。恢复用 `…/restore`(保持停用、不自动带回调度),或让 dws 重新登记同一来源 —— 钉钉空间登记是确定性 ID + `ON CONFLICT`,**原地复活同一行、不产生重复**。卡片上新增「去掉来源」按钮(管理员,带确认弹窗)。
- 迁移 `000047_source_connection_archive`:新增 `archived_at` / `archived_by` 与只覆盖未归档行的部分索引;列表、目录、计划刷新、手动扫描四类查询一律排除归档行(漏一条就等于入口没去掉)。
- 「系统设置 → 数据源配置」默认只列 PP 资料与「可加入的来源」:AList 来源(已由钉盘文件替代)与静态登记的钉钉知识库来源(改为按当前钉钉账号发现)**不再默认列出**,收进「显示 AList/钉钉知识库来源(N)」折叠开关 —— **来源一个都没删**,展开后照旧可改刷新周期与启停,「来源与目录」里的内容不变。手动导入来源(本地手工验收)不在这次范围内,照旧显示。
### Fixed
+102
View File
@@ -944,6 +944,108 @@
}
}
},
"/api/v1/source-connections/{id}/archive": {
"parameters": [
{
"$ref": "#/components/parameters/ID"
}
],
"post": {
"operationId": "archiveSourceConnection",
"summary": "归档下架来源(去掉入口;不物理删除)",
"description": "把来源从数据源配置、来源与目录、计划刷新与手动扫描里摘掉:目录快照、发布记录与审计数据原样保留;恢复只需再次登记同一来源(钉钉空间登记是确定性 ID,原地复活同一行)。幂等:重复归档不改归档时间与归档人。仅管理员可写。",
"tags": [
"governance"
],
"parameters": [
{
"$ref": "#/components/parameters/IdempotencyKey"
},
{
"in": "header",
"name": "X-Governance-Action",
"required": true,
"schema": {
"type": "string",
"enum": [
"source-archive"
]
}
}
],
"responses": {
"200": {
"$ref": "#/components/responses/Envelope"
},
"400": {
"$ref": "#/components/responses/BadRequest"
},
"403": {
"$ref": "#/components/responses/Forbidden"
},
"404": {
"$ref": "#/components/responses/NotFound"
},
"500": {
"$ref": "#/components/responses/InternalError"
},
"503": {
"$ref": "#/components/responses/ServiceUnavailable"
}
}
}
},
"/api/v1/source-connections/{id}/restore": {
"parameters": [
{
"$ref": "#/components/parameters/ID"
}
],
"post": {
"operationId": "restoreSourceConnection",
"summary": "撤销来源归档(保持停用,调度不自动恢复)",
"description": "归档的反向操作:来源重新出现在列表里,但保持停用且不带调度,需要管理员再显式启用。幂等:未归档时视为成功;连接不存在返回 404。仅管理员可写。",
"tags": [
"governance"
],
"parameters": [
{
"$ref": "#/components/parameters/IdempotencyKey"
},
{
"in": "header",
"name": "X-Governance-Action",
"required": true,
"schema": {
"type": "string",
"enum": [
"source-restore"
]
}
}
],
"responses": {
"200": {
"$ref": "#/components/responses/Envelope"
},
"400": {
"$ref": "#/components/responses/BadRequest"
},
"403": {
"$ref": "#/components/responses/Forbidden"
},
"404": {
"$ref": "#/components/responses/NotFound"
},
"500": {
"$ref": "#/components/responses/InternalError"
},
"503": {
"$ref": "#/components/responses/ServiceUnavailable"
}
}
}
},
"/api/v1/source-connections/{id}/scan": {
"parameters": [
{
+6
View File
@@ -117,6 +117,12 @@ func classifyRole(method, path string) string {
if strings.Contains(path, "/settings/revisions") {
return roleAdmin
}
// 「去掉来源」(归档下架 / 恢复)会把一个来源从目录、计划刷新与扫描里摘掉,
// 并改变后续可见与可发布的来源集合:与改设置修订同级,写只能管理员。
if strings.HasPrefix(path, "/api/v1/source-connections/") &&
(strings.HasSuffix(path, "/archive") || strings.HasSuffix(path, "/restore")) {
return roleAdmin
}
// The DingTalk scan session replaces the shared browser credentials:
// admin-only, like every other credential surface. The QR image is a
// login artifact, so even polling it stays admin-only. Space
+6
View File
@@ -394,6 +394,12 @@ func NewHandler(config Config) (http.Handler, error) {
mux.HandleFunc("POST /api/v1/source-connections/{id}/settings/revisions/{revision_id}/activate", func(w http.ResponseWriter, r *http.Request) {
activateSourceSettingsRevision(w, r, config)
})
mux.HandleFunc("POST /api/v1/source-connections/{id}/archive", func(w http.ResponseWriter, r *http.Request) {
archiveSourceConnection(w, r, config)
})
mux.HandleFunc("POST /api/v1/source-connections/{id}/restore", func(w http.ResponseWriter, r *http.Request) {
restoreSourceConnection(w, r, config)
})
mux.HandleFunc("GET /api/v1/review-candidates", func(w http.ResponseWriter, r *http.Request) {
listReviewCandidates(w, r, config.Store)
})
@@ -0,0 +1,79 @@
package httpapi
import (
"context"
"errors"
"net/http"
"time"
"knowledge-governance/internal/platform/postgres"
)
// 「去掉来源」= 归档下架(不是物理删除)。管理员专用、幂等。
//
// 归档后来源从数据源配置、来源与目录、计划刷新与手动扫描里全部消失,但目录快照、
// 发布记录与审计数据原样保留 —— 恢复用 /restore,或让 dws 重新登记同一空间
// (钉钉空间登记是确定性 id + ON CONFLICT,会原地复活同一行,不产生重复)。
//
// 为什么不给物理删除:source_items / source_catalog_snapshots / test_publications /
// release_executions 都以 ON DELETE RESTRICT 引用来源连接。硬删要么失败,要么
// 连带毁掉发布与审计记录。
func archiveSourceConnection(w http.ResponseWriter, r *http.Request, config Config) {
mutateSourceConnectionArchive(w, r, config, true)
}
func restoreSourceConnection(w http.ResponseWriter, r *http.Request, config Config) {
mutateSourceConnectionArchive(w, r, config, false)
}
// sourceConnectionArchiver 是本路由需要的最小能力面:归档与恢复各一个写操作。
type sourceConnectionArchiver interface {
ArchiveSourceConnection(ctx context.Context, connectionID, actor string, now time.Time) error
RestoreSourceConnection(ctx context.Context, connectionID, actor string, now time.Time) error
}
func mutateSourceConnectionArchive(w http.ResponseWriter, r *http.Request, config Config, archive bool) {
if _, ok := requireSessionIdentity(w, r); !ok {
return
}
// 写意图与幂等键与其它来源设置写操作同规格:没有意图头就不写。
wantIntent := "source-archive"
if !archive {
wantIntent = "source-restore"
}
if r.Header.Get("X-Governance-Action") != wantIntent {
writeJSON(w, http.StatusForbidden, map[string]string{"error": "source archive write intent is required"})
return
}
operationID, ok := requireOperationID(w, r)
if !ok {
return
}
connectionID := r.PathValue("id")
if !validCatalogID(connectionID) {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid source connection"})
return
}
store, ok := config.Store.(sourceConnectionArchiver)
if !ok {
writeJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "source archive is not wired"})
return
}
var err error
if archive {
err = store.ArchiveSourceConnection(r.Context(), connectionID, actorFor(r), config.Now().UTC())
} else {
err = store.RestoreSourceConnection(r.Context(), connectionID, actorFor(r), config.Now().UTC())
}
switch {
case errors.Is(err, postgres.ErrSourceConnectionNotFound):
writeJSON(w, http.StatusNotFound, map[string]string{"error": "source connection not found"})
return
case err != nil:
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "could not change source archive state"})
return
}
writeJSON(w, http.StatusOK, map[string]any{
"id": connectionID, "archived": archive, "operation_id": operationID,
})
}
@@ -0,0 +1,126 @@
package httpapi
import (
"context"
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
postgresstore "knowledge-governance/internal/platform/postgres"
)
// 复用既有 fakeJobStore 提供 Store 接口的其余方法;这里只关心归档两个写操作。
type fakeSourceArchiver struct {
*fakeJobStore
archivedID string
archivedActor string
restoredID string
restoredActor string
err error
}
func (f *fakeSourceArchiver) ArchiveSourceConnection(ctx context.Context, connectionID, actor string, now time.Time) error {
f.archivedID, f.archivedActor = connectionID, actor
return f.err
}
func (f *fakeSourceArchiver) RestoreSourceConnection(ctx context.Context, connectionID, actor string, now time.Time) error {
f.restoredID, f.restoredActor = connectionID, actor
return f.err
}
func archiveRequest(t *testing.T, path, intent, key string) *http.Request {
t.Helper()
request := httptest.NewRequest(http.MethodPost, path, nil)
if intent != "" {
request.Header.Set("X-Governance-Action", intent)
}
if key != "" {
request.Header.Set("Idempotency-Key", key)
}
return request.WithContext(context.WithValue(request.Context(), sessionKey{}, sessionIdentity{Username: "yaozhanghao", Role: roleAdmin}))
}
func TestSourceConnectionArchiveHandler(t *testing.T) {
store := &fakeSourceArchiver{fakeJobStore: &fakeJobStore{}}
handler, err := NewHandler(Config{Store: store, Now: func() time.Time { return time.Date(2026, 10, 14, 4, 0, 0, 0, time.UTC) }})
if err != nil {
t.Fatalf("NewHandler() error = %v", err)
}
// 写意图头缺失 ⇒ 拒绝(与其它来源设置写操作同规格)。
noIntent := httptest.NewRecorder()
handler.ServeHTTP(noIntent, archiveRequest(t, "/api/v1/source-connections/conn-1/archive", "", "op-1"))
if noIntent.Code != http.StatusForbidden {
t.Fatalf("intent gate = %d, want 403", noIntent.Code)
}
// 幂等键缺失 ⇒ 400(不允许"没键的重试")。
noKey := httptest.NewRecorder()
handler.ServeHTTP(noKey, archiveRequest(t, "/api/v1/source-connections/conn-1/archive", "source-archive", ""))
if noKey.Code != http.StatusBadRequest {
t.Fatalf("idempotency gate = %d, want 400", noKey.Code)
}
// 归档成功:把连接 ID 与操作人交给存储层。
ok := httptest.NewRecorder()
handler.ServeHTTP(ok, archiveRequest(t, "/api/v1/source-connections/conn-1/archive", "source-archive", "op-archive-1"))
if ok.Code != http.StatusOK || store.archivedID != "conn-1" || store.archivedActor != "yaozhanghao" {
t.Fatalf("archive = %d id=%s actor=%s body=%s", ok.Code, store.archivedID, store.archivedActor, ok.Body.String())
}
if !strings.Contains(ok.Body.String(), `"archived":true`) || strings.Contains(ok.Body.String(), "secret") {
t.Fatalf("archive body = %s", ok.Body.String())
}
// 恢复走另一条意图头。
unknownIntent := httptest.NewRecorder()
handler.ServeHTTP(unknownIntent, archiveRequest(t, "/api/v1/source-connections/conn-1/restore", "source-archive", "op-restore-1"))
if unknownIntent.Code != http.StatusForbidden {
t.Fatalf("restore intent gate = %d, want 403", unknownIntent.Code)
}
restored := httptest.NewRecorder()
handler.ServeHTTP(restored, archiveRequest(t, "/api/v1/source-connections/conn-1/restore", "source-restore", "op-restore-1"))
if restored.Code != http.StatusOK || store.restoredID != "conn-1" || store.restoredActor != "yaozhanghao" {
t.Fatalf("restore = %d id=%s actor=%s", restored.Code, store.restoredID, store.restoredActor)
}
// 未知连接 ⇒ 404(与"已经处于目标状态"区分开)。
store.err = postgresstore.ErrSourceConnectionNotFound
missing := httptest.NewRecorder()
handler.ServeHTTP(missing, archiveRequest(t, "/api/v1/source-connections/conn-missing/archive", "source-archive", "op-2"))
if missing.Code != http.StatusNotFound {
t.Fatalf("unknown connection = %d, want 404", missing.Code)
}
// 存储层故障 ⇒ 500,且不得回显内部细节。
store.err = errors.New("boom: password=hunter2")
failure := httptest.NewRecorder()
handler.ServeHTTP(failure, archiveRequest(t, "/api/v1/source-connections/conn-1/archive", "source-archive", "op-3"))
if failure.Code != http.StatusInternalServerError || strings.Contains(failure.Body.String(), "hunter2") {
t.Fatalf("store failure = %d body=%s", failure.Code, failure.Body.String())
}
// 非法 ID ⇒ 400(超长 ID;带路径分隔符的会被 mux 规范化,不在这里测)。
badID := httptest.NewRecorder()
handler.ServeHTTP(badID, archiveRequest(t, "/api/v1/source-connections/"+strings.Repeat("a", 300)+"/archive", "source-archive", "op-4"))
if badID.Code != http.StatusBadRequest {
t.Fatalf("invalid id = %d, want 400", badID.Code)
}
}
// 「去掉来源」是管理员动作:工程号与只读号都不行。
func TestSourceConnectionArchiveRolePolicy(t *testing.T) {
for _, tt := range []struct {
method, path, want string
}{
{http.MethodPost, "/api/v1/source-connections/conn-1/archive", roleAdmin},
{http.MethodPost, "/api/v1/source-connections/conn-1/restore", roleAdmin},
{http.MethodGet, "/api/v1/source-connections", roleViewer},
{http.MethodPost, "/api/v1/source-connections/conn-1/scan", roleEngineer},
} {
if got := classifyRole(tt.method, tt.path); got != tt.want {
t.Fatalf("classifyRole(%s %s) = %s, want %s", tt.method, tt.path, got, tt.want)
}
}
}
@@ -80,6 +80,9 @@ INSERT INTO source_connections (
) VALUES ($1, 'dingtalk', $2, $3, '', TRUE, '{}'::jsonb, '{}'::jsonb, $4, $4, $5)
ON CONFLICT (id) DO UPDATE
SET name = EXCLUDED.name, base_url = EXCLUDED.base_url,
-- 归档过的来源被重新登记(dws 重新获取)时原地复活:清掉归档标记,不建重复行 ——
-- 目录快照与历史仍属于同一行。
archived_at = NULL, archived_by = '',
updated_at = EXCLUDED.updated_at, updated_by = EXCLUDED.updated_by`,
connectionID, in.Name, locator, in.Now.UTC(), "dingtalk-registration")
if err != nil {
@@ -0,0 +1,7 @@
DROP INDEX IF EXISTS idx_source_connections_active;
ALTER TABLE source_connections
DROP COLUMN IF EXISTS archived_by;
ALTER TABLE source_connections
DROP COLUMN IF EXISTS archived_at;
@@ -0,0 +1,20 @@
-- 「去掉来源」= 归档下架,而不是物理删除。
--
-- 归档后这个来源会从所有列表与调度里消失:数据源配置不再列它、来源与目录不再出现
-- 它的快照、计划刷新不会再拉它、手动扫描会被拒。目录快照、发布记录与审计数据
-- **原样保留**;恢复只需把 archived_at 置空,或让 dws 重新获取同一个空间
-- (钉钉空间登记用确定性 id + ON CONFLICT,会原地复活同一行,不会产生重复)。
--
-- 为什么不物理删除:source_items / source_catalog_snapshots / test_publications /
-- release_* 都以 ON DELETE RESTRICT 引用它,硬删要么失败,要么连带毁掉发布与审计
-- 记录 —— 而这两样正是治理平台存在的理由。
ALTER TABLE source_connections
ADD COLUMN IF NOT EXISTS archived_at TIMESTAMPTZ;
ALTER TABLE source_connections
ADD COLUMN IF NOT EXISTS archived_by VARCHAR(255) NOT NULL DEFAULT '';
-- 列表、调度、目录都只关心未归档的行:部分索引让这些查询继续走索引。
CREATE INDEX IF NOT EXISTS idx_source_connections_active
ON source_connections (source_type, name)
WHERE archived_at IS NULL;
+23 -2
View File
@@ -11,8 +11,8 @@ func TestEmbeddedMigrationsAreVersionSorted(t *testing.T) {
if err != nil {
t.Fatalf("loadMigrations() error = %v", err)
}
if len(migrations) != 46 {
t.Fatalf("migration count = %d, want 46", len(migrations))
if len(migrations) != 47 {
t.Fatalf("migration count = %d, want 47", len(migrations))
}
if migrations[0].Version != 1 || !strings.Contains(migrations[0].UpSQL, "CREATE TABLE IF NOT EXISTS jobs") {
t.Fatalf("migration 1 = %#v", migrations[0])
@@ -125,6 +125,27 @@ func TestEmbeddedMigrationsAreVersionSorted(t *testing.T) {
if !strings.Contains(migrations[45].DownSQL, "DROP TABLE IF EXISTS version_history_commits") {
t.Fatalf("migration 46 down = %#v", migrations[45].DownSQL)
}
// 000047:「去掉来源」= 归档下架而不是物理删除(内容、发布与审计都靠 ON DELETE
// RESTRICT 引用来源连接,硬删要么失败要么毁证据)。归档标记 + 只覆盖未归档行的部分索引。
if migrations[46].Version != 47 || !strings.Contains(migrations[46].UpSQL, "ADD COLUMN IF NOT EXISTS archived_at") ||
!strings.Contains(migrations[46].UpSQL, "ADD COLUMN IF NOT EXISTS archived_by") ||
!strings.Contains(migrations[46].UpSQL, "WHERE archived_at IS NULL") {
t.Fatalf("migration 47 = %#v", migrations[46])
}
if !strings.Contains(migrations[46].DownSQL, "DROP COLUMN IF EXISTS archived_at") {
t.Fatalf("migration 47 down = %#v", migrations[46].DownSQL)
}
// 归档行必须在每一条「来源可见 / 可调度」的 SQL 里被排除;漏一条就等于入口没去掉。
for name, query := range map[string]string{
"listSourceConnectionsSQL": listSourceConnectionsSQL,
"claimDueSourceRefreshesSQL": claimDueSourceRefreshesSQL,
"triggerPPSourceScanSQL": triggerPPSourceScanSQL,
"inspectPPSourceScanBlockSQL": inspectPPSourceScanBlockSQL,
} {
if !strings.Contains(strings.ToLower(query), "archived_at is null") {
t.Errorf("%s must exclude archived connections", name)
}
}
}
func TestProcessingConfirmationMigrationSeparatesPlanningFromUpload(t *testing.T) {
+2 -1
View File
@@ -35,7 +35,7 @@ JOIN LATERAL (
LIMIT 1
) job ON TRUE
LEFT JOIN source_catalog_heads head ON head.source_connection_id=connection.id
WHERE connection.enabled AND head.source_connection_id IS NULL
WHERE connection.enabled AND connection.archived_at IS NULL AND head.source_connection_id IS NULL
ORDER BY job.created_at DESC, connection.id`)
if err != nil {
return nil, fmt.Errorf("list catalog scan progress: %w", err)
@@ -90,6 +90,7 @@ LEFT JOIN LATERAL (
ORDER BY job.completed_at DESC
LIMIT 1
) refresh ON TRUE
WHERE connection.archived_at IS NULL
ORDER BY CASE connection.source_type WHEN 'dingtalk' THEN 1 WHEN 'pp' THEN 2 WHEN 'alist' THEN 3 ELSE 4 END,
snapshot.source_label, connection.id`)
if err != nil {
@@ -0,0 +1,223 @@
package postgres
import (
"context"
"errors"
"os"
"testing"
"time"
"github.com/jackc/pgx/v5/pgxpool"
)
// 「去掉来源」的语义护栏:归档之后来源从列表里消失、不能再被手动扫描,
// 但**内容与审计数据一行都不少**;恢复/重新登记都能原地复活,且不产生重复行。
// 这些性质就是"可以不物理删除"的全部理由,所以逐条钉死。
func TestSourceConnectionArchiveHidesFromListsAndKeepsData(t *testing.T) {
databaseURL := isolatedIntegrationDatabaseURL(t, os.Getenv("TEST_DATABASE_URL"))
if databaseURL == "" {
t.Skip("TEST_DATABASE_URL is not set")
}
ctx := context.Background()
pool, err := pgxpool.New(ctx, databaseURL)
if err != nil {
t.Fatal(err)
}
defer pool.Close()
if err := ApplyMigrations(ctx, pool); err != nil {
t.Fatalf("ApplyMigrations() error = %v", err)
}
store := NewStore(pool)
now := time.Date(2026, 10, 14, 4, 0, 0, 0, time.UTC)
if _, err := pool.Exec(ctx, `
INSERT INTO source_connections (id, source_type, name, enabled, created_at, updated_at)
VALUES ('conn-keep-pp', 'pp', 'PP资料下载中心', TRUE, $1, $1),
('conn-drop-alist', 'alist', '国内工业', TRUE, $1, $1),
('conn-drop-pp', 'pp', 'PP legacy', TRUE, $1, $1)`, now); err != nil {
t.Fatal(err)
}
if _, err := pool.Exec(ctx, `
INSERT INTO source_items (id, source_connection_id, stable_external_id, canonical_uri_hash,
title, item_type, visibility, visibility_origin, source_change, created_at, updated_at)
VALUES ('item-drop-1', 'conn-drop-alist', 'ext-1', repeat('a', 64), '要被归档的文档',
'file', 'internal', 'configured_scope', 'unchanged', $1, $1)`, now); err != nil {
t.Fatal(err)
}
page, err := store.ListSourceConnections(ctx)
if err != nil {
t.Fatalf("ListSourceConnections() error = %v", err)
}
if len(page.Items) != 3 {
t.Fatalf("前置状态:want 3 sources, got %d", len(page.Items))
}
if err := store.ArchiveSourceConnection(ctx, "conn-drop-alist", "yaozhanghao", now); err != nil {
t.Fatalf("ArchiveSourceConnection() error = %v", err)
}
page, err = store.ListSourceConnections(ctx)
if err != nil {
t.Fatal(err)
}
if len(page.Items) != 2 {
t.Fatalf("归档后仍列出:%#v", page.Items)
}
for _, item := range page.Items {
if item.ID == "conn-drop-alist" {
t.Fatalf("归档的来源不该出现在列表里:%#v", item)
}
}
// 内容数据必须原样保留:这是"归档而不是删除"的核心承诺。
var keptItems int
var archivedAt *time.Time
var archivedBy string
if err := pool.QueryRow(ctx, `SELECT COUNT(*)::int FROM source_items WHERE source_connection_id='conn-drop-alist'`).Scan(&keptItems); err != nil {
t.Fatal(err)
}
if keptItems != 1 {
t.Fatalf("归档不该动内容数据:source_items=%d, want 1", keptItems)
}
if err := pool.QueryRow(ctx, `SELECT archived_at, archived_by FROM source_connections WHERE id='conn-drop-alist'`).Scan(&archivedAt, &archivedBy); err != nil {
t.Fatal(err)
}
if archivedAt == nil || !archivedAt.Equal(now) || archivedBy != "yaozhanghao" {
t.Fatalf("归档证据 = %v / %q", archivedAt, archivedBy)
}
// 幂等:重复归档不改归档时间与归档人(谁先归档就以谁为准)。
if err := store.ArchiveSourceConnection(ctx, "conn-drop-alist", "someone-else", now.Add(time.Hour)); err != nil {
t.Fatalf("重复归档该幂等成功:%v", err)
}
var again *time.Time
var againBy string
if err := pool.QueryRow(ctx, `SELECT archived_at, archived_by FROM source_connections WHERE id='conn-drop-alist'`).Scan(&again, &againBy); err != nil {
t.Fatal(err)
}
if again == nil || !again.Equal(now) || againBy != "yaozhanghao" {
t.Fatalf("重复归档漂移了归档证据 = %v / %q", again, againBy)
}
// 未知连接要能和"已经是目标状态"区分开。
if err := store.ArchiveSourceConnection(ctx, "conn-missing", "yaozhanghao", now); !errors.Is(err, ErrSourceConnectionNotFound) {
t.Fatalf("未知连接归档 = %v, want ErrSourceConnectionNotFound", err)
}
if err := store.RestoreSourceConnection(ctx, "conn-missing", "yaozhanghao", now); !errors.Is(err, ErrSourceConnectionNotFound) {
t.Fatalf("未知连接恢复 = %v, want ErrSourceConnectionNotFound", err)
}
// 归档来源不能再被手动扫描(否则"去掉入口"只是界面假象)。
if _, err := store.TriggerPPSourceScan(ctx, "conn-drop-alist", now); !errors.Is(err, ErrSourceConnectionNotScannable) {
t.Fatalf("归档来源扫描 = %v, want ErrSourceConnectionNotScannable", err)
}
if err := store.ArchiveSourceConnection(ctx, "conn-drop-pp", "yaozhanghao", now); err != nil {
t.Fatal(err)
}
if _, err := store.TriggerPPSourceScan(ctx, "conn-drop-pp", now); !errors.Is(err, ErrSourceConnectionNotScannable) {
t.Fatalf("归档的 PP 来源扫描 = %v, want ErrSourceConnectionNotScannable", err)
}
// 恢复:回到列表,但保持停用、调度为空 —— 归档时清掉的调度不悄悄回来。
if err := store.RestoreSourceConnection(ctx, "conn-drop-alist", "yaozhanghao", now); err != nil {
t.Fatalf("RestoreSourceConnection() error = %v", err)
}
page, err = store.ListSourceConnections(ctx)
if err != nil {
t.Fatal(err)
}
// conn-drop-pp 仍然归档中,只有 conn-drop-alist 回来了:2 = 3 - 仍归档的 1。
if len(page.Items) != 2 {
t.Fatalf("恢复后 want 2 sources, got %d", len(page.Items))
}
var restoredSeen bool
for _, item := range page.Items {
if item.ID == "conn-drop-pp" {
t.Fatalf("仍在归档中的来源不该出现:%#v", item)
}
if item.ID == "conn-drop-alist" {
restoredSeen = true
}
}
if !restoredSeen {
t.Fatalf("恢复的来源没有回到列表:%#v", page.Items)
}
var enabled bool
var nextRefresh *time.Time
var restoredArchived *time.Time
if err := pool.QueryRow(ctx, `SELECT enabled, next_refresh_at, archived_at FROM source_connections WHERE id='conn-drop-alist'`).
Scan(&enabled, &nextRefresh, &restoredArchived); err != nil {
t.Fatal(err)
}
if enabled || nextRefresh != nil || restoredArchived != nil {
t.Fatalf("恢复状态 = enabled:%v next:%v archived:%v", enabled, nextRefresh, restoredArchived)
}
// 未归档时恢复幂等成功(不是错误)。
if err := store.RestoreSourceConnection(ctx, "conn-drop-alist", "yaozhanghao", now); err != nil {
t.Fatalf("重复恢复该幂等成功:%v", err)
}
}
// 「等下用 dws 重新获取」必须原地复活同一个来源,而不是留下一堆重复行:
// 钉钉空间登记用确定性 id + ON CONFLICT,归档标记要在登记时被清掉。
func TestRegisterDingtalkSpaceRevivesArchivedConnection(t *testing.T) {
databaseURL := isolatedIntegrationDatabaseURL(t, os.Getenv("TEST_DATABASE_URL"))
if databaseURL == "" {
t.Skip("TEST_DATABASE_URL is not set")
}
ctx := context.Background()
pool, err := pgxpool.New(ctx, databaseURL)
if err != nil {
t.Fatal(err)
}
defer pool.Close()
if err := ApplyMigrations(ctx, pool); err != nil {
t.Fatalf("ApplyMigrations() error = %v", err)
}
store := NewStore(pool)
now := time.Date(2026, 10, 14, 4, 0, 0, 0, time.UTC)
input := RegisterDingtalkSpaceInput{
RootKey: "revive-key", Name: "ATOM知识库", SpaceID: "space-revive", RootID: "root-revive", Now: now,
}
first, err := store.RegisterDingtalkSpace(ctx, input)
if err != nil {
t.Fatalf("RegisterDingtalkSpace() error = %v", err)
}
if err := store.ArchiveSourceConnection(ctx, first, "yaozhanghao", now); err != nil {
t.Fatal(err)
}
page, err := store.ListSourceConnections(ctx)
if err != nil {
t.Fatal(err)
}
if len(page.Items) != 0 {
t.Fatalf("归档后不该列出:%#v", page.Items)
}
second, err := store.RegisterDingtalkSpace(ctx, input)
if err != nil {
t.Fatalf("重新登记失败:%v", err)
}
if second != first {
t.Fatalf("重新登记产生了新行:%s → %s", first, second)
}
page, err = store.ListSourceConnections(ctx)
if err != nil {
t.Fatal(err)
}
if len(page.Items) != 1 || page.Items[0].ID != first {
t.Fatalf("重新获取后应当原地复活且不重复:%#v", page.Items)
}
var archivedAt *time.Time
var archivedBy string
var rows int
if err := pool.QueryRow(ctx, `SELECT COUNT(*)::int FROM source_connections WHERE id = $1`, first).Scan(&rows); err != nil {
t.Fatal(err)
}
if err := pool.QueryRow(ctx, `SELECT archived_at, archived_by FROM source_connections WHERE id = $1`, first).Scan(&archivedAt, &archivedBy); err != nil {
t.Fatal(err)
}
if rows != 1 || archivedAt != nil || archivedBy != "" {
t.Fatalf("复活状态 = rows:%d archived:%v by:%q", rows, archivedAt, archivedBy)
}
}
@@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"strings"
"time"
"knowledge-governance/internal/governance"
@@ -47,6 +48,7 @@ LEFT JOIN LATERAL (
ORDER BY created_at DESC, id DESC
LIMIT 1
) scan ON TRUE
WHERE c.archived_at IS NULL
GROUP BY c.id, c.source_type, c.name, c.enabled, c.schedule, c.last_health, c.updated_at, c.next_refresh_at,
scan.id, scan.state, scan.observed_items, scan.unchanged_items,
scan.added_items, scan.updated_items, scan.over_limit_items,
@@ -158,7 +160,7 @@ WITH due AS (
FROM source_connections AS c
LEFT JOIN source_connection_revisions AS r
ON r.id = c.current_revision_id AND r.state = 'active'
WHERE c.enabled AND c.source_type <> 'manual'
WHERE c.enabled AND c.source_type <> 'manual' AND c.archived_at IS NULL
AND c.next_refresh_at IS NOT NULL AND c.next_refresh_at <= $1
ORDER BY c.next_refresh_at, c.id
FOR UPDATE OF c SKIP LOCKED
@@ -225,3 +227,64 @@ func NextRefreshAtFor(enabled bool, scheduleRaw []byte, from time.Time) (*time.T
next := from.UTC().Add(interval)
return &next, nil
}
// ArchiveSourceConnection 把来源连接从平台上「去掉入口」:置 archived_at 之后它会
// 从数据源配置、来源与目录、计划刷新与扫描里全部消失,但行、目录快照、发布记录与
// 审计原样保留 —— 恢复(RestoreSourceConnection 或重新登记同一来源)即可原地复活。
//
// 幂等:已归档时不改写归档时间与归档人,只返回成功(重复点击不漂移语义)。同时停用并
// 清空 next_refresh_at:即使有人手工把 archived_at 改回来,也不会带着旧调度立刻跑起来。
func (s *Store) ArchiveSourceConnection(ctx context.Context, connectionID, actor string, now time.Time) error {
if strings.TrimSpace(connectionID) == "" || now.IsZero() {
return errors.New("archive source connection: id and time are required")
}
if strings.TrimSpace(actor) == "" {
return errors.New("archive source connection: actor is required")
}
tag, err := s.db.Exec(ctx, `
UPDATE source_connections
SET archived_at = COALESCE(archived_at, $2),
archived_by = CASE WHEN archived_at IS NULL THEN $3 ELSE archived_by END,
enabled = FALSE,
next_refresh_at = NULL,
updated_at = $2,
updated_by = $3
WHERE id = $1`, connectionID, now.UTC(), actor)
if err != nil {
return fmt.Errorf("archive source connection: %w", err)
}
if tag.RowsAffected() == 0 {
return ErrSourceConnectionNotFound
}
return nil
}
// RestoreSourceConnection 撤销归档:来源重新出现在列表里,但**保持停用**,需要管理员
// 再显式启用(归档时被清掉的调度不会悄悄回来)。幂等:未归档时视为成功;只有连接真的
// 不存在才报 ErrSourceConnectionNotFound。
func (s *Store) RestoreSourceConnection(ctx context.Context, connectionID, actor string, now time.Time) error {
if strings.TrimSpace(connectionID) == "" || now.IsZero() {
return errors.New("restore source connection: id and time are required")
}
if strings.TrimSpace(actor) == "" {
return errors.New("restore source connection: actor is required")
}
tag, err := s.db.Exec(ctx, `
UPDATE source_connections
SET archived_at = NULL, archived_by = '', updated_at = $2, updated_by = $3
WHERE id = $1 AND archived_at IS NOT NULL`, connectionID, now.UTC(), actor)
if err != nil {
return fmt.Errorf("restore source connection: %w", err)
}
if tag.RowsAffected() > 0 {
return nil
}
var exists bool
if err := s.db.QueryRow(ctx, `SELECT EXISTS(SELECT 1 FROM source_connections WHERE id = $1)`, connectionID).Scan(&exists); err != nil {
return fmt.Errorf("restore source connection: %w", err)
}
if !exists {
return ErrSourceConnectionNotFound
}
return nil
}
+2 -2
View File
@@ -28,7 +28,7 @@ const triggerPPSourceScanSQL = `
WITH target AS (
SELECT id, source_type
FROM source_connections
WHERE id = $1 AND source_type = 'pp'
WHERE id = $1 AND source_type = 'pp' AND archived_at IS NULL
), active AS (
SELECT 1
FROM source_scans
@@ -65,7 +65,7 @@ SELECT connection.source_type,
AND scan.state IN ('pending', 'running')
)
FROM source_connections AS connection
WHERE connection.id = $1
WHERE connection.id = $1 AND connection.archived_at IS NULL
`
const startSourceScanSQL = `
+5 -3
View File
@@ -5,8 +5,10 @@ import { DingtalkSourceDiscovery } from './DingtalkSourceDiscovery'
describe('DingtalkSourceDiscovery', () => {
it('renders the discovery shell without any file-format filter', () => {
const html = renderToStaticMarkup(<DingtalkSourceDiscovery registeredNames={['ATOM知识库']} onRegistered={vi.fn()} />)
for (const text of ['可加入的来源', '刷新列表', '加入为来源(选中 0)', '已是来源']) {
const html = renderToStaticMarkup(
<DingtalkSourceDiscovery registeredNames={['ATOM知识库']} onRegistered={vi.fn()} />
)
for (const text of ['dws 导入(钉钉)', '刷新列表', '加入为来源(选中 0)', '已是来源']) {
expect(html).toContain(text)
}
// 需求(2026-10-10):数据源配置这里不再做文件格式筛选——格式归目录级标注决定。
@@ -14,4 +16,4 @@ describe('DingtalkSourceDiscovery', () => {
expect(html).not.toContain(removed)
}
})
})
})
+103 -47
View File
@@ -2,7 +2,12 @@ import { useCallback, useEffect, useState } from 'react'
import { DingtalkRequestError, registerDingtalkSpace } from './dingtalkSession'
import { DingtalkSessionControl } from './DingtalkSessionControl'
import { discoveryCandidates, fetchAvailableDingtalkSources, type AvailableDingtalkSources, type DiscoveryCandidate } from './dingtalkSources'
import {
discoveryCandidates,
fetchAvailableDingtalkSources,
type AvailableDingtalkSources,
type DiscoveryCandidate,
} from './dingtalkSources'
// 「系统设置 → 数据源配置」里的数据源发现面板(管理员):
// ① 列出当前钉钉账号(dws)能看到的钉钉知识库;
@@ -11,7 +16,13 @@ import { discoveryCandidates, fetchAvailableDingtalkSources, type AvailableDingt
// ④ 登记需要有效的钉钉扫码会话:会话失效时直接弹二维码,扫完自动重试本次登记。
// 这里不显示文件格式筛选:格式由目录级标注决定,不在这张卡上做。
export function DingtalkSourceDiscovery({ registeredNames, onRegistered }: { registeredNames: string[]; onRegistered: () => void }) {
export function DingtalkSourceDiscovery({
registeredNames,
onRegistered,
}: {
registeredNames: string[]
onRegistered: () => void
}) {
const [list, setList] = useState<AvailableDingtalkSources | null>(null)
const [loading, setLoading] = useState(false)
const [loadError, setLoadError] = useState<string | null>(null)
@@ -22,7 +33,8 @@ export function DingtalkSourceDiscovery({ registeredNames, onRegistered }: { reg
const [loginRequired, setLoginRequired] = useState(false)
const load = useCallback(async (signal?: AbortSignal) => {
setLoading(true); setLoadError(null)
setLoading(true)
setLoadError(null)
try {
const next = await fetchAvailableDingtalkSources(signal)
setList(next)
@@ -47,10 +59,13 @@ export function DingtalkSourceDiscovery({ registeredNames, onRegistered }: { reg
const knowledgeBases = candidates.filter((item) => item.kind === 'knowledge_base')
const driveSpaces = candidates.filter((item) => item.kind === 'drive_space')
const selected = candidates.filter((item) => checked.includes(item.key))
const toggle = (key: string) => setChecked((current) => current.includes(key) ? current.filter((item) => item !== key) : [...current, key])
const toggle = (key: string) =>
setChecked((current) => (current.includes(key) ? current.filter((item) => item !== key) : [...current, key]))
const submit = async (retryAfterLogin = false) => {
setBusy(true); setFailure(null); setMessage(null)
setBusy(true)
setFailure(null)
setMessage(null)
const added: string[] = []
const failed: string[] = []
let needLogin = false
@@ -89,49 +104,90 @@ export function DingtalkSourceDiscovery({ registeredNames, onRegistered }: { reg
const renderRow = (candidate: DiscoveryCandidate) => {
const already = candidate.alreadyRegistered
return <li key={candidate.key} className={already ? 'discovery-row registered' : 'discovery-row'}>
<label>
<input type="checkbox" aria-label={`选择${candidate.name}`} checked={checked.includes(candidate.key)} disabled={already || busy} onChange={() => toggle(candidate.key)} />
<span><strong>{candidate.name}</strong><small>{candidate.detail}</small></span>
</label>
{already ? <span className="safe-badge">已是来源</span> : null}
</li>
return (
<li key={candidate.key} className={already ? 'discovery-row registered' : 'discovery-row'}>
<label>
<input
type="checkbox"
aria-label={`选择${candidate.name}`}
checked={checked.includes(candidate.key)}
disabled={already || busy}
onChange={() => toggle(candidate.key)}
/>
<span>
<strong>{candidate.name}</strong>
<small>{candidate.detail}</small>
</span>
</label>
{already ? <span className="safe-badge">已是来源</span> : null}
</li>
)
}
return <section className="dingtalk-discovery" aria-label="可加入的数据源">
<div className="dingtalk-discovery-heading">
<div>
<strong>可加入的来源</strong>
<small>按当前钉钉账号列出能访问的知识库与钉盘空间;勾选后加入为来源,首次目录扫描自动排队。</small>
return (
<section className="dingtalk-discovery" aria-label="可加入的数据源">
<div className="dingtalk-discovery-heading">
<div>
<strong>dws 导入(钉钉)</strong>
<small>按当前钉钉账号列出能访问的知识库与钉盘空间;勾选后加入为来源,首次目录扫描自动排队。</small>
</div>
<button type="button" className="secondary" onClick={() => void load()} disabled={loading}>
{loading ? '正在列举…' : '刷新列表'}
</button>
</div>
<button type="button" className="secondary" onClick={() => void load()} disabled={loading}>{loading ? '正在列举…' : '刷新列表'}</button>
</div>
{loadError ? <p className="error-message" role="alert">{loadError}</p> : null}
{list ? <div className="dingtalk-discovery-groups">
<div className="discovery-group">
<h4>知识库({knowledgeBases.length})</h4>
{list.knowledge_notice ? <small className="empty-inline">知识库列表可能不全:{list.knowledge_notice}</small> : null}
{knowledgeBases.length ? <ul>{knowledgeBases.map(renderRow)}</ul> : <small className="empty-inline">当前账号下没有可访问的知识库。</small>}
{loadError ? (
<p className="error-message" role="alert">
{loadError}
</p>
) : null}
{list ? (
<div className="dingtalk-discovery-groups">
<div className="discovery-group">
<h4>知识库({knowledgeBases.length})</h4>
{list.knowledge_notice ? (
<small className="empty-inline">知识库列表可能不全:{list.knowledge_notice}</small>
) : null}
{knowledgeBases.length ? (
<ul>{knowledgeBases.map(renderRow)}</ul>
) : (
<small className="empty-inline">当前账号下没有可访问的知识库。</small>
)}
</div>
<div className="discovery-group">
<h4>钉盘({driveSpaces.length})</h4>
{list.drive_notice ? <small className="empty-inline">钉盘这次列不出来:{list.drive_notice}</small> : null}
{!list.drive_notice && !driveSpaces.length ? (
<small className="empty-inline">当前账号下没有可见的钉盘空间。</small>
) : null}
{driveSpaces.length ? <ul>{driveSpaces.map(renderRow)}</ul> : null}
</div>
</div>
) : null}
<div className="dingtalk-discovery-actions">
<button type="button" disabled={!selected.length || busy} onClick={() => void submit()}>
{busy ? '正在加入…' : `加入为来源(选中 ${selected.length})`}
</button>
<small>勾选的知识库会登记为来源连接;已登记的条目标为「已是来源」,不会重复登记。</small>
</div>
<div className="discovery-group">
<h4>钉盘({driveSpaces.length})</h4>
{list.drive_notice ? <small className="empty-inline">钉盘这次列不出来:{list.drive_notice}</small> : null}
{!list.drive_notice && !driveSpaces.length ? <small className="empty-inline">当前账号下没有可见的钉盘空间。</small> : null}
{driveSpaces.length ? <ul>{driveSpaces.map(renderRow)}</ul> : null}
</div>
</div> : null}
<div className="dingtalk-discovery-actions">
<button type="button" disabled={!selected.length || busy} onClick={() => void submit()}>{busy ? '正在加入…' : `加入为来源(选中 ${selected.length})`}</button>
<small>勾选的知识库会登记为来源连接;已登记的条目标为「已是来源」,不会重复登记。</small>
</div>
{message ? <p className="success-message" role="status">{message}</p> : null}
{failure ? <p className="error-message" role="alert">{failure}</p> : null}
{loginRequired ? <DingtalkSessionControl
autoStart
hideTrigger
space={selected[0]?.url}
onSuccess={resumeAfterLogin}
onClose={() => setLoginRequired(false)}
/> : null}
</section>
}
{message ? (
<p className="success-message" role="status">
{message}
</p>
) : null}
{failure ? (
<p className="error-message" role="alert">
{failure}
</p>
) : null}
{loginRequired ? (
<DingtalkSessionControl
autoStart
hideTrigger
space={selected[0]?.url}
onSuccess={resumeAfterLogin}
onClose={() => setLoginRequired(false)}
/>
) : null}
</section>
)
}
+60 -47
View File
@@ -1,6 +1,6 @@
import { renderToStaticMarkup } from 'react-dom/server'
import { describe, expect, it, vi } from 'vitest'
import { SystemSettingsView, splitSourceConnections, visibleSaveMessage } from './SystemSettings'
import { SystemSettingsView, ppSourceConnections, visibleSaveMessage } from './SystemSettings'
import { AuthContext } from './auth'
import type { MonitorOverview } from './monitor'
import type { CatalogSource } from './sourceCatalog'
@@ -193,18 +193,16 @@ describe('SystemSettingsView', () => {
'刷新策略',
'设置服务已启用',
'当前活动版本',
'端点与凭据',
'文档工程师可调整',
'管理员设置已锁定',
])
expect(html).toContain(text)
// 需求(2026-10-14):数据源配置默认只列 PP 资料 + 「可加入的来源」;AList 与钉钉知识库来源
// 不删除,收进折叠开关,展开后才渲染(原来它们默认就在列表里)。
// 折叠开关自己的文案里就含“钉钉知识库”,所以只核对来源卡片才有的标记。
for (const folded of ['公司级技术支持知识库', '专用浏览器扫码会话', '直连 API Token'])
expect(html).not.toContain(folded)
expect(html).toContain('显示 AList/钉钉知识库来源(1)')
expect(html).toContain('这 1 个已接入来源未删除,数据不变')
// 需求(2026-10-14 收敛):数据源配置只剩两个入口 —— dws 导入(按当前钉钉账号发现)
// 与 PP 导入;AList/静态钉钉知识库等来源类型不再出现在这里(也没有折叠开关兜着)。
for (const hidden of ['公司级技术支持知识库', '专用浏览器扫码会话', '显示 AList/钉钉知识库来源'])
expect(html).not.toContain(hidden)
expect(html).toContain('dws 导入')
expect(html).toContain('PP 导入')
// 需求(2026-10-10):数据源配置不再做文件格式筛选(格式由目录级标注决定)。
for (const removed of ['文件格式筛选', '未勾选的一律不取件', '保存保留格式', '恢复继承上级规则'])
expect(html).not.toContain(removed)
@@ -293,13 +291,11 @@ describe('SystemSettingsView', () => {
expect(servicesHtml).toContain('端点探测:暂无真实探测结果')
// V1: left-nav section controls, per-source schedule editing, danger modal & history affordances.
expect(html).toContain('settings-nav')
expect(html).toContain('查看修订历史')
expect(html).toContain('保存并启用')
expect(html).toContain('停用中(不入队)')
// 卡片级调度/停用按钮仍在,只是默认只对可见来源渲染(钉钉/AList 卡片已折起)。
for (const foldedControl of ['下次自动刷新', '保存并启用调度', '停用来源'])
expect(html).not.toContain(foldedControl)
expect(html).toContain('本地手工导入来源不支持定时刷新')
// 非 PP 来源在这里根本不渲染卡片:连它们卡片上的调度、停用、修订入口一起消失。
for (const gone of ['查看修订历史', '保存并启用调度', '停用来源', '下次自动刷新']) expect(html).not.toContain(gone)
expect(html).toContain('本页只列 PP 来源')
// 手工来源(本地手工验收)也不再列在这里,它的专属提示自然不出现。
expect(html).not.toContain('本地手工导入来源不支持定时刷新')
// dead buttons removed
expect(html).not.toContain('添加内容源')
expect(html).not.toContain('管理员编辑')
@@ -537,38 +533,48 @@ describe('SystemSettingsView 数据源配置的默认清单', () => {
next_refresh_at: null,
},
]
const view = () =>
const view = (role: 'admin' | 'engineer' = 'admin') =>
renderToStaticMarkup(
<SystemSettingsView
sources={[]}
connections={withPP}
monitor={monitor}
settings={settings}
productionEnabled={false}
routingCapabilities={{ internal_enabled: true, external_enabled: true }}
loading={false}
saving={false}
error={null}
saveMessage={null}
onSave={vi.fn()}
onSourcesChanged={vi.fn()}
onRefresh={vi.fn()}
/>
<AuthContext.Provider value={authValue(role)}>
<SystemSettingsView
sources={[]}
connections={withPP}
monitor={monitor}
settings={settings}
productionEnabled={false}
routingCapabilities={{ internal_enabled: true, external_enabled: true }}
loading={false}
saving={false}
error={null}
saveMessage={null}
onSave={vi.fn()}
onSourcesChanged={vi.fn()}
onRefresh={vi.fn()}
/>
</AuthContext.Provider>
)
it('默认只列 PP 资料:AList 与钉钉知识库来源折起来但没删', () => {
it('只有两个入口:PP 来源列在 PP 导入下,AList/钉钉知识库/手工都不出现', () => {
const html = view()
expect(html).toContain('PP资料下载中心')
expect(html).toContain('PP 资料下载中心')
expect(html).not.toContain('国内工业')
expect(html).not.toContain('ATOM知识库')
expect(html).toContain('显示 AList/钉钉知识库来源(2)')
// 登记路径说清楚:新来源一律走「可加入的来源」;格式筛选与旧的新增入口继续不存在。
expect(html).toContain('可加入的来源')
expect(html).toContain('钉盘文件已替代 AList')
// 三个非 PP 来源一个都不该渲染,也没有任何折叠开关。
for (const hidden of ['国内工业', 'ATOM知识库', '本地手工验收', '显示 AList', '折叠'])
expect(html).not.toContain(hidden)
expect(html).toContain('dws 导入(钉钉)')
expect(html).toContain('PP 导入')
expect(html).toContain('去掉来源</button>')
expect(html).toContain('端点与凭据')
// PP 来源的卡片操作照旧:改刷新周期、启停、看修订历史。
for (const kept of ['查看修订历史', '保存并启用调度', '停用来源', '自动刷新周期']) expect(html).toContain(kept)
for (const removed of ['+ 新增钉钉知识库', '文件格式筛选']) expect(html).not.toContain(removed)
// 文档工程师看不到 dws 导入面板与「去掉来源」:两者都是管理员动作。
const engineer = view('engineer')
expect(engineer).not.toContain('dws 导入(钉钉)')
expect(engineer).not.toContain('去掉来源</button>')
expect(engineer).toContain('PP 导入')
})
})
describe('splitSourceConnections(数据源配置的默认清单规则)', () => {
describe('ppSourceConnections(数据源配置的清单规则)', () => {
const rows: SourceConnectionListItem[] = [
{
id: 'a',
@@ -614,13 +620,20 @@ describe('splitSourceConnections(数据源配置的默认清单规则)', ()
updated_at: '2026-09-09T09:30:32Z',
next_refresh_at: null,
},
{
id: 'e',
source_type: 'pp',
name: 'PP legacy',
enabled: false,
schedule_configured: false,
health_status: 'unknown',
item_count: 5,
updated_at: '2026-09-09T09:30:32Z',
next_refresh_at: null,
},
]
it('默认折起 AList 与钉钉知识库,展开后原样返回(顺序不变、一个不少)', () => {
const folded = splitSourceConnections(rows, false)
expect(folded.visible.map((item) => item.id)).toEqual(['a', 'd'])
expect(folded.legacy.map((item) => item.id)).toEqual(['b', 'c'])
const expanded = splitSourceConnections(rows, true)
expect(expanded.visible.map((item) => item.id)).toEqual(['a', 'b', 'c', 'd'])
expect(expanded.legacy.length).toBe(2)
it('只放行 PP 来源,顺序不变,其余类型一条都不放行', () => {
expect(ppSourceConnections(rows).map((item) => item.id)).toEqual(['a', 'e'])
expect(ppSourceConnections([])).toEqual([])
})
})
+87 -53
View File
@@ -14,6 +14,7 @@ import { fetchCatalogRoutingCapabilities, fetchCatalogSources } from './sourceCa
import type { CatalogRoutingCapabilities, CatalogSource } from './sourceCatalog'
import {
activateSourceSettingsRevision,
archiveSourceConnection,
createSourceSettingsRevision,
fetchSourceConnectionList,
fetchSourceSettingsRevisions,
@@ -67,13 +68,13 @@ const sourceTypeLabels: Record<string, string> = {
manual: '本地手工导入',
}
// 已由钉盘文件替代 / 改为按当前钉钉账号发现的来源类型:数据不删,只是在「数据源配置」默认折起来。
export const legacySourceTypes = ['alist', 'dingtalk'] as const
export const splitSourceConnections = (connections: SourceConnectionListItem[], showLegacy: boolean) => {
const legacy = connections.filter((item) => (legacySourceTypes as readonly string[]).includes(item.source_type))
const legacyIDs = new Set(legacy.map((item) => item.id))
return { legacy, visible: showLegacy ? connections : connections.filter((item) => !legacyIDs.has(item.id)) }
}
// 「数据源配置」只保留两个入口:**dws 导入**(按当前钉钉账号发现知识库与钉盘空间)
// 与 **PP 导入**。AList 已由钉盘文件替代、钉钉知识库一律走当前账号发现、本地手工与
// PP legacy 也下架了:本页只列 PP 来源。下架用归档(archived_at)实现,不删数据 ——
// 需要时用 dws 重新登记同名来源即原地复活(见 postgres.RegisterDingtalkSpace)。
export const ppSourceConnections = (connections: SourceConnectionListItem[]) =>
connections.filter((item) => item.source_type === 'pp')
const sourceAuthLabels: Record<string, string> = {
dingtalk: '专用浏览器扫码会话',
pp: '公开只读访问',
@@ -163,8 +164,27 @@ function SourceSettingsCard({
const [showHistory, setShowHistory] = useState(false)
const [history, setHistory] = useState<SourceSettingsRevision[] | null>(null)
const [pendingDisable, setPendingDisable] = useState(false)
// 「去掉来源」= 归档下架:先弹确认,再调后端(后端会再校验一次管理员权限)。
const [pendingArchive, setPendingArchive] = useState(false)
const scheduled = connection.source_type !== 'manual'
// 归档下架:来源从列表、目录、调度与扫描里消失,但数据不删;重新登记同一来源即复活。
const archive = async () => {
setBusy(true)
setFailure(null)
setMessage(null)
try {
await archiveSourceConnection(connection.id)
setMessage(`「${connection.name}」已归档下架:列表、来源与目录、计划刷新里都不再出现,数据保留可恢复。`)
setPendingArchive(false)
onSourcesChanged()
} catch (cause) {
setFailure(cause instanceof Error ? cause.message : '去掉来源失败')
} finally {
setBusy(false)
}
}
const submit = async (nextEnabled: boolean) => {
setBusy(true)
setMessage(null)
@@ -263,9 +283,16 @@ function SourceSettingsCard({
</div>
)}
{connection.source_type === 'dingtalk' && isAdmin ? <DingtalkSessionControl /> : null}
<button className="link" onClick={() => void loadHistory()}>
{showHistory ? '收起修订历史' : '查看修订历史'}
</button>
<div className="source-schedule-links">
<button className="link" onClick={() => void loadHistory()}>
{showHistory ? '收起修订历史' : '查看修订历史'}
</button>
{isAdmin ? (
<button className="link danger" disabled={busy} onClick={() => setPendingArchive(true)}>
去掉来源
</button>
) : null}
</div>
</div>
{message ? (
<p className="success-message" role="status">
@@ -318,6 +345,26 @@ function SourceSettingsCard({
</div>
</div>
) : null}
{pendingArchive ? (
<div className="modal-backdrop" role="dialog" aria-modal="true">
<div className="modal-card">
<h3>去掉「{connection.name}」?</h3>
<p>
去掉 =
归档下架:该来源会从数据源配置、来源与目录、计划刷新与手动扫描里摘掉。目录快照、发布记录与审计数据全部保留,
随时可恢复(用 dws 重新登记同一来源即原地复活)。此操作会先由后端再次校验管理员权限。
</p>
<div className="modal-actions">
<button className="danger" disabled={busy} onClick={() => void archive()}>
{busy ? '正在归档…' : '确认去掉'}
</button>
<button className="secondary" disabled={busy} onClick={() => setPendingArchive(false)}>
取消
</button>
</div>
</div>
</div>
) : null}
</article>
)
}
@@ -355,13 +402,8 @@ export function SystemSettingsView({
)
const [dirty, setDirty] = useState(false)
// 「数据源配置」默认只列 PP 资料:AList 已被钉盘文件替代、钉钉知识库一律走当前账号发现
//(见下方「可加入的来源」)。这 9 个已接入来源**不删除**,收进折叠开关里,随时可展开管理。
const [showLegacySources, setShowLegacySources] = useState(false)
const { legacy: legacyConnections, visible: visibleConnections } = splitSourceConnections(
connections,
showLegacySources
)
// 本页只列 PP 来源;dws 导入由下面的发现面板负责(其余类型已归档下架,未删除)。
const ppConnections = ppSourceConnections(connections)
useEffect(() => {
if (!settings) return
@@ -458,8 +500,8 @@ export function SystemSettingsView({
<div>
<h2>数据源配置</h2>
<p>
新来源只从下方「可加入的来源」勾选登记(按当前钉钉账号列出可访问的知识库与钉盘空间,钉盘文件已替代
AList);本页保留 PP 资料。逐来源设置自动刷新周期或保持仅手动,保存会先生成修订、验证后原子启用。
只留两个入口:<strong>dws 导入</strong>(按当前钉钉账号列出可访问的知识库与钉盘空间)与
<strong>PP 导入</strong>。逐来源设置自动刷新周期或保持仅手动,保存会先生成修订、验证后原子启用。
</p>
</div>
</div>
@@ -469,41 +511,33 @@ export function SystemSettingsView({
onRegistered={onSourcesChanged}
/>
) : null}
{legacyConnections.length ? (
<div className="settings-legacy-toggle">
<button type="button" className="secondary" onClick={() => setShowLegacySources((value) => !value)}>
{showLegacySources
? `收起 AList/钉钉知识库来源(${legacyConnections.length})`
: `显示 AList/钉钉知识库来源(${legacyConnections.length})`}
</button>
<small>
{showLegacySources
? '这些来源已在两处保留:本列表可改刷新周期与启停,「来源与目录」里照旧浏览它们的内容。'
: `AList 来源已由钉盘文件替代,钉钉知识库来源改为按当前账号发现;这 ${legacyConnections.length} 个已接入来源未删除,数据不变。`}
</small>
<div className="settings-source-group" id="settings-sources-pp">
<div className="settings-section-heading sub">
<div>
<h3>PP 导入</h3>
<p>
公开只读的 PP 资料下载中心。这里的「去掉来源」是归档下架:从列表、来源与目录、计划刷新里摘掉,
但目录快照与发布记录不删,随时可恢复。
</p>
</div>
</div>
) : null}
{visibleConnections.length ? (
<div className="settings-source-list">
{visibleConnections.map((connection) => (
<SourceSettingsCard
key={connection.id}
connection={connection}
onSourcesChanged={onSourcesChanged}
/>
))}
{sources.length ? null : (
<small className="empty-inline">尚无快照数据;快照将在来源首次刷新完成后出现。</small>
)}
</div>
) : legacyConnections.length ? (
<div className="empty-state">
本页只列 PP 资料;AList/钉钉知识库来源已收在上方开关里(共 {legacyConnections.length}{' '}
个,未删除)。
</div>
) : (
<div className="empty-state">尚无可显示的来源连接。</div>
)}
{ppConnections.length ? (
<div className="settings-source-list">
{ppConnections.map((connection) => (
<SourceSettingsCard
key={connection.id}
connection={connection}
onSourcesChanged={onSourcesChanged}
/>
))}
{sources.length ? null : (
<small className="empty-inline">尚无快照数据;快照将在来源首次刷新完成后出现。</small>
)}
</div>
) : (
<div className="empty-state">本页只列 PP 来源;现在还没有已接入的 PP 来源。</div>
)}
</div>
</section>
) : null}
{section === 'services' ? (
+287 -67
View File
@@ -2,6 +2,7 @@ import { afterEach, describe, expect, it, vi } from 'vitest'
import {
activateSourceSettingsRevision,
archiveSourceConnection,
createSourceSettingsRevision,
fetchSourceConnectionList,
fetchSourceConnections,
@@ -11,6 +12,7 @@ import {
parseSourceConnectionsResponse,
parseSourceScanDetailResponse,
parseSourceScansResponse,
restoreSourceConnection,
triggerSourceScan,
} from './sourceConnections'
@@ -19,31 +21,65 @@ afterEach(() => vi.unstubAllGlobals())
describe('parseSourceConnectionsResponse', () => {
it('normalizes only the safe source connection projection', () => {
const result = parseSourceConnectionsResponse({
data: [{
id: 'conn-safe', source_type: 'alist', name: 'AList只读源', enabled: true,
schedule_configured: true, health_status: 'healthy', item_count: 42,
updated_at: '2026-09-06T01:00:00Z', base_url: 'https://secret-origin.example', secret_ref: 'secret-name',
last_scan: {
id: 'scan-1', state: 'completed', observed_items: 42, unchanged_items: 40,
added_items: 1, updated_items: 1, over_limit_items: 0, missing_items: 0,
planned_uploads: 1, error_code: '', created_at: '2026-09-06T00:59:00Z',
completed_at: '2026-09-06T01:00:00Z', snapshot_sha256: 'must-strip', previous_revision: 'must-strip',
data: [
{
id: 'conn-safe',
source_type: 'alist',
name: 'AList只读源',
enabled: true,
schedule_configured: true,
health_status: 'healthy',
item_count: 42,
updated_at: '2026-09-06T01:00:00Z',
base_url: 'https://secret-origin.example',
secret_ref: 'secret-name',
last_scan: {
id: 'scan-1',
state: 'completed',
observed_items: 42,
unchanged_items: 40,
added_items: 1,
updated_items: 1,
over_limit_items: 0,
missing_items: 0,
planned_uploads: 1,
error_code: '',
created_at: '2026-09-06T00:59:00Z',
completed_at: '2026-09-06T01:00:00Z',
snapshot_sha256: 'must-strip',
previous_revision: 'must-strip',
},
},
}],
],
summary: { total: 1, enabled: 1, disabled: 0, healthy: 1, degraded: 0, unhealthy: 0, unknown: 0 },
})
expect(result.items).toEqual([{
id: 'conn-safe', source_type: 'alist', name: 'AList只读源', enabled: true,
schedule_configured: true, health_status: 'healthy', item_count: 42,
updated_at: '2026-09-06T01:00:00Z',
last_scan: {
id: 'scan-1', state: 'completed', observed_items: 42, unchanged_items: 40,
added_items: 1, updated_items: 1, over_limit_items: 0, missing_items: 0,
planned_uploads: 1, error_code: '', created_at: '2026-09-06T00:59:00Z',
completed_at: '2026-09-06T01:00:00Z',
expect(result.items).toEqual([
{
id: 'conn-safe',
source_type: 'alist',
name: 'AList只读源',
enabled: true,
schedule_configured: true,
health_status: 'healthy',
item_count: 42,
updated_at: '2026-09-06T01:00:00Z',
last_scan: {
id: 'scan-1',
state: 'completed',
observed_items: 42,
unchanged_items: 40,
added_items: 1,
updated_items: 1,
over_limit_items: 0,
missing_items: 0,
planned_uploads: 1,
error_code: '',
created_at: '2026-09-06T00:59:00Z',
completed_at: '2026-09-06T01:00:00Z',
},
},
}])
])
expect(result.summary.total).toBe(1)
expect(result.items[0]).not.toHaveProperty('base_url')
expect(result.items[0]).not.toHaveProperty('secret_ref')
@@ -66,7 +102,7 @@ describe('fetchSourceConnections', () => {
expect(fetchMock).toHaveBeenCalledWith(
'/api/v1/source-connections',
expect.objectContaining({ headers: { Accept: 'application/json' } }),
expect.objectContaining({ headers: { Accept: 'application/json' } })
)
})
})
@@ -76,54 +112,120 @@ describe('triggerSourceScan', () => {
const fetchMock = vi.fn().mockResolvedValue({
ok: true,
json: async () => ({
id: 'scan-safe', source_connection_id: 'conn/a', source_type: 'pp', state: 'pending',
created_at: '2026-09-06T01:00:00Z', snapshot_sha256: 'must-strip',
id: 'scan-safe',
source_connection_id: 'conn/a',
source_type: 'pp',
state: 'pending',
created_at: '2026-09-06T01:00:00Z',
snapshot_sha256: 'must-strip',
}),
})
vi.stubGlobal('fetch', fetchMock)
const scan = await triggerSourceScan('conn/a')
expect(fetchMock).toHaveBeenCalledWith('/api/v1/source-connections/conn%2Fa/scan', expect.objectContaining({ method: 'POST' }))
expect(fetchMock).toHaveBeenCalledWith(
'/api/v1/source-connections/conn%2Fa/scan',
expect.objectContaining({ method: 'POST' })
)
expect(scan).toEqual({
id: 'scan-safe', source_connection_id: 'conn/a', source_type: 'pp', state: 'pending', created_at: '2026-09-06T01:00:00Z',
id: 'scan-safe',
source_connection_id: 'conn/a',
source_type: 'pp',
state: 'pending',
created_at: '2026-09-06T01:00:00Z',
})
})
})
describe('source scan history and details', () => {
const scan = {
id: 'scan-safe', state: 'completed', observed_items: 2, unchanged_items: 0,
added_items: 1, updated_items: 0, over_limit_items: 0, missing_items: 1,
planned_uploads: 1, error_code: '', created_at: '2026-09-06T06:00:00Z', completed_at: '2026-09-06T06:01:00Z',
snapshot_sha256: 'must-be-dropped', stable_external_id: 'must-be-dropped',
id: 'scan-safe',
state: 'completed',
observed_items: 2,
unchanged_items: 0,
added_items: 1,
updated_items: 0,
over_limit_items: 0,
missing_items: 1,
planned_uploads: 1,
error_code: '',
created_at: '2026-09-06T06:00:00Z',
completed_at: '2026-09-06T06:01:00Z',
snapshot_sha256: 'must-be-dropped',
stable_external_id: 'must-be-dropped',
}
it('strictly parses safe history and detail projections', () => {
expect(parseSourceScansResponse({ data: [scan], total: 1, limit: 20, offset: 0 })).toEqual({
items: [{
id: 'scan-safe', state: 'completed', observed_items: 2, unchanged_items: 0,
added_items: 1, updated_items: 0, over_limit_items: 0, missing_items: 1,
planned_uploads: 1, error_code: '', created_at: '2026-09-06T06:00:00Z', completed_at: '2026-09-06T06:01:00Z',
}], total: 1, limit: 20, offset: 0,
items: [
{
id: 'scan-safe',
state: 'completed',
observed_items: 2,
unchanged_items: 0,
added_items: 1,
updated_items: 0,
over_limit_items: 0,
missing_items: 1,
planned_uploads: 1,
error_code: '',
created_at: '2026-09-06T06:00:00Z',
completed_at: '2026-09-06T06:01:00Z',
},
],
total: 1,
limit: 20,
offset: 0,
})
expect(parseSourceScanDetailResponse({ data: {
...scan,
changes: [{
id: 'chg-safe', source_item_id: 'si-safe', change_type: 'updated', observed_size_bytes: 42,
created_at: '2026-09-06T06:01:00Z', previous_revision: 'must-be-dropped', observed_revision: 'must-be-dropped',
}],
}})).toEqual({
id: 'scan-safe', state: 'completed', observed_items: 2, unchanged_items: 0,
added_items: 1, updated_items: 0, over_limit_items: 0, missing_items: 1,
planned_uploads: 1, error_code: '', created_at: '2026-09-06T06:00:00Z', completed_at: '2026-09-06T06:01:00Z',
changes: [{ id: 'chg-safe', source_item_id: 'si-safe', change_type: 'updated', observed_size_bytes: 42, created_at: '2026-09-06T06:01:00Z' }],
expect(
parseSourceScanDetailResponse({
data: {
...scan,
changes: [
{
id: 'chg-safe',
source_item_id: 'si-safe',
change_type: 'updated',
observed_size_bytes: 42,
created_at: '2026-09-06T06:01:00Z',
previous_revision: 'must-be-dropped',
observed_revision: 'must-be-dropped',
},
],
},
})
).toEqual({
id: 'scan-safe',
state: 'completed',
observed_items: 2,
unchanged_items: 0,
added_items: 1,
updated_items: 0,
over_limit_items: 0,
missing_items: 1,
planned_uploads: 1,
error_code: '',
created_at: '2026-09-06T06:00:00Z',
completed_at: '2026-09-06T06:01:00Z',
changes: [
{
id: 'chg-safe',
source_item_id: 'si-safe',
change_type: 'updated',
observed_size_bytes: 42,
created_at: '2026-09-06T06:01:00Z',
},
],
})
})
it('requests connection-scoped history and details', async () => {
const fetchMock = vi.fn()
.mockResolvedValueOnce(new Response(JSON.stringify({ data: [scan], total: 1, limit: 20, offset: 0 }), { status: 200 }))
const fetchMock = vi
.fn()
.mockResolvedValueOnce(
new Response(JSON.stringify({ data: [scan], total: 1, limit: 20, offset: 0 }), { status: 200 })
)
.mockResolvedValueOnce(new Response(JSON.stringify({ data: { ...scan, changes: [] } }), { status: 200 }))
vi.stubGlobal('fetch', fetchMock)
await fetchSourceScans('conn / pp')
@@ -135,47 +237,165 @@ describe('source scan history and details', () => {
describe('V1 source settings client', () => {
it('projects the connection list without sensitive fields', async () => {
const fetchMock = vi.fn().mockResolvedValue(new Response(JSON.stringify({
data: [{ id: 'conn-1', source_type: 'alist', name: 'A', enabled: true, schedule_configured: true,
health_status: 'healthy', item_count: 3, updated_at: '2026-09-12T01:00:00Z', next_refresh_at: '2026-09-12T09:00:00Z',
base_url: 'https://strip.me', secret_ref: 'strip-too' }],
}), { status: 200 }))
const fetchMock = vi.fn().mockResolvedValue(
new Response(
JSON.stringify({
data: [
{
id: 'conn-1',
source_type: 'alist',
name: 'A',
enabled: true,
schedule_configured: true,
health_status: 'healthy',
item_count: 3,
updated_at: '2026-09-12T01:00:00Z',
next_refresh_at: '2026-09-12T09:00:00Z',
base_url: 'https://strip.me',
secret_ref: 'strip-too',
},
],
}),
{ status: 200 }
)
)
vi.stubGlobal('fetch', fetchMock)
const items = await fetchSourceConnectionList()
expect(items).toEqual([{ id: 'conn-1', source_type: 'alist', name: 'A', enabled: true, schedule_configured: true,
health_status: 'healthy', item_count: 3, updated_at: '2026-09-12T01:00:00Z', next_refresh_at: '2026-09-12T09:00:00Z' }])
expect(items).toEqual([
{
id: 'conn-1',
source_type: 'alist',
name: 'A',
enabled: true,
schedule_configured: true,
health_status: 'healthy',
item_count: 3,
updated_at: '2026-09-12T01:00:00Z',
next_refresh_at: '2026-09-12T09:00:00Z',
},
])
expect(JSON.stringify(items)).not.toContain('strip')
})
it('rejects malformed connection items', async () => {
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(new Response(JSON.stringify({
data: [{ id: 'x' }],
}), { status: 200 })))
vi.stubGlobal(
'fetch',
vi.fn().mockResolvedValue(
new Response(
JSON.stringify({
data: [{ id: 'x' }],
}),
{ status: 200 }
)
)
)
await expect(fetchSourceConnectionList()).rejects.toThrow('invalid source connection item')
})
it('creates and activates revisions with intent + idempotency headers and surfaces operation', async () => {
const fetchMock = vi.fn()
.mockResolvedValueOnce(new Response(JSON.stringify({ data: { id: 'scr-2' }, operation: 'created' }), { status: 201 }))
.mockResolvedValueOnce(new Response(JSON.stringify({ data: { connection_id: 'c', revision_id: 'scr-2' }, operation: 'replayed' }), { status: 200 }))
const fetchMock = vi
.fn()
.mockResolvedValueOnce(
new Response(JSON.stringify({ data: { id: 'scr-2' }, operation: 'created' }), { status: 201 })
)
.mockResolvedValueOnce(
new Response(JSON.stringify({ data: { connection_id: 'c', revision_id: 'scr-2' }, operation: 'replayed' }), {
status: 200,
})
)
vi.stubGlobal('fetch', fetchMock)
expect(await createSourceSettingsRevision('conn/1', { source_type: 'alist', name: 'A', endpoint: '', credential_ref: '', enabled: true, schedule: { refresh_interval_seconds: 3600 } })).toEqual({ operation: 'created', id: 'scr-2' })
expect(
await createSourceSettingsRevision('conn/1', {
source_type: 'alist',
name: 'A',
endpoint: '',
credential_ref: '',
enabled: true,
schedule: { refresh_interval_seconds: 3600 },
})
).toEqual({ operation: 'created', id: 'scr-2' })
expect(await activateSourceSettingsRevision('conn/1', 'scr/2')).toEqual({ operation: 'replayed', id: null })
const [createCall, activateCall] = fetchMock.mock.calls
expect(createCall[0]).toBe('/api/v1/source-connections/conn%2F1/settings/revisions')
expect((createCall[1] as RequestInit).headers).toMatchObject({ 'X-Governance-Action': 'source-settings' })
expect(((createCall[1] as RequestInit).headers as Record<string, string>)['Idempotency-Key']).toMatch(/[0-9a-f-]{36}/)
expect(((createCall[1] as RequestInit).headers as Record<string, string>)['Idempotency-Key']).toMatch(
/[0-9a-f-]{36}/
)
expect(activateCall[0]).toBe('/api/v1/source-connections/conn%2F1/settings/revisions/scr%2F2/activate')
expect(createCall[1]).not.toBe(activateCall[1])
})
it('surfaces server error details on failure', async () => {
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(new Response(JSON.stringify({ error: 'manual sources cannot be scheduled' }), { status: 400 })))
await expect(createSourceSettingsRevision('c', { source_type: 'manual', name: 'A', endpoint: '', credential_ref: '', enabled: true, schedule: { refresh_interval_seconds: 3600 } })).rejects.toThrow('保存来源设置草稿失败:manual sources cannot be scheduled')
vi.stubGlobal(
'fetch',
vi
.fn()
.mockResolvedValue(
new Response(JSON.stringify({ error: 'manual sources cannot be scheduled' }), { status: 400 })
)
)
await expect(
createSourceSettingsRevision('c', {
source_type: 'manual',
name: 'A',
endpoint: '',
credential_ref: '',
enabled: true,
schedule: { refresh_interval_seconds: 3600 },
})
).rejects.toThrow('保存来源设置草稿失败:manual sources cannot be scheduled')
})
it('parses revision history strictly', async () => {
const revision = { id: 'scr-1', revision_number: 1, state: 'active', name: 'A', endpoint: 'http://x', credential_ref: 'ref', enabled: true, refresh_interval_seconds: '3600', created_by: 'z', created_at: '2026-09-12T01:00:00Z', validated_at: null, activated_at: '2026-09-12T01:00:00Z', retired_at: null }
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(new Response(JSON.stringify({ data: [revision] }), { status: 200 })))
const revision = {
id: 'scr-1',
revision_number: 1,
state: 'active',
name: 'A',
endpoint: 'http://x',
credential_ref: 'ref',
enabled: true,
refresh_interval_seconds: '3600',
created_by: 'z',
created_at: '2026-09-12T01:00:00Z',
validated_at: null,
activated_at: '2026-09-12T01:00:00Z',
retired_at: null,
}
vi.stubGlobal(
'fetch',
vi.fn().mockResolvedValue(new Response(JSON.stringify({ data: [revision] }), { status: 200 }))
)
const history = await fetchSourceSettingsRevisions('c')
expect(history[0].state).toBe('active')
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(new Response(JSON.stringify({ data: [{ ...revision, state: 'weird' }] }), { status: 200 })))
vi.stubGlobal(
'fetch',
vi
.fn()
.mockResolvedValue(new Response(JSON.stringify({ data: [{ ...revision, state: 'weird' }] }), { status: 200 }))
)
await expect(fetchSourceSettingsRevisions('c')).rejects.toThrow('invalid source settings revision')
})
})
describe('去掉来源(归档)客户端', () => {
it('归档/恢复都带写意图头与幂等键,并正确编码来源 ID', async () => {
const fetchMock = vi.fn().mockResolvedValue(new Response('{}', { status: 200 }))
vi.stubGlobal('fetch', fetchMock)
await archiveSourceConnection('conn/1')
const [archiveURL, archiveInit] = fetchMock.mock.calls[0]
expect(archiveURL).toBe('/api/v1/source-connections/conn%2F1/archive')
expect(archiveInit.method).toBe('POST')
expect(archiveInit.headers['X-Governance-Action']).toBe('source-archive')
expect(typeof archiveInit.headers['Idempotency-Key']).toBe('string')
expect(archiveInit.headers['Idempotency-Key'].length).toBeGreaterThan(0)
await restoreSourceConnection('conn/1')
const [restoreURL, restoreInit] = fetchMock.mock.calls[1]
expect(restoreURL).toBe('/api/v1/source-connections/conn%2F1/restore')
expect(restoreInit.headers['X-Governance-Action']).toBe('source-restore')
})
it('后端拒绝时把原因带出来(不吞错误)', async () => {
vi.stubGlobal(
'fetch',
vi.fn().mockResolvedValue(new Response(JSON.stringify({ error: 'source connection not found' }), { status: 404 }))
)
await expect(archiveSourceConnection('conn-x')).rejects.toThrow('去掉来源失败:source connection not found')
})
})
+218 -58
View File
@@ -79,7 +79,13 @@ const healthStatuses = new Set<SourceHealthStatus>(['unknown', 'healthy', 'degra
const scanStates = new Set<SourceScanState>(['pending', 'running', 'completed', 'failed'])
const scanChangeTypes = new Set<SourceScanChangeType>(['added', 'updated', 'unavailable'])
const summaryKeys: ReadonlyArray<keyof SourceConnectionSummary> = [
'total', 'enabled', 'disabled', 'healthy', 'degraded', 'unhealthy', 'unknown',
'total',
'enabled',
'disabled',
'healthy',
'degraded',
'unhealthy',
'unknown',
]
export function parseSourceConnectionsResponse(value: unknown): SourceConnectionPage {
@@ -87,11 +93,19 @@ export function parseSourceConnectionsResponse(value: unknown): SourceConnection
throw new Error('invalid source connections response')
}
const items = value.data.map((raw): SourceConnectionView => {
if (!isRecord(raw) || typeof raw.id !== 'string' || typeof raw.source_type !== 'string' ||
!sourceTypes.has(raw.source_type as SourceConnectionType) || typeof raw.name !== 'string' ||
typeof raw.enabled !== 'boolean' || typeof raw.schedule_configured !== 'boolean' ||
typeof raw.health_status !== 'string' || !healthStatuses.has(raw.health_status as SourceHealthStatus) ||
!isCount(raw.item_count) || typeof raw.updated_at !== 'string') {
if (
!isRecord(raw) ||
typeof raw.id !== 'string' ||
typeof raw.source_type !== 'string' ||
!sourceTypes.has(raw.source_type as SourceConnectionType) ||
typeof raw.name !== 'string' ||
typeof raw.enabled !== 'boolean' ||
typeof raw.schedule_configured !== 'boolean' ||
typeof raw.health_status !== 'string' ||
!healthStatuses.has(raw.health_status as SourceHealthStatus) ||
!isCount(raw.item_count) ||
typeof raw.updated_at !== 'string'
) {
throw new Error('invalid source connection item')
}
const lastScan = parseSourceScanSummary(raw.last_scan)
@@ -131,18 +145,28 @@ export interface SourceConnectionListItem {
function parseConnectionList(raw: unknown): SourceConnectionListItem[] {
if (!Array.isArray(raw)) throw new Error('invalid source connections list')
return raw.map((item): SourceConnectionListItem => {
if (!isRecord(item) || typeof item.id !== 'string' || typeof item.source_type !== 'string' ||
typeof item.name !== 'string' || typeof item.enabled !== 'boolean' ||
typeof item.schedule_configured !== 'boolean' || typeof item.item_count !== 'number' ||
typeof item.updated_at !== 'string' ||
(item.next_refresh_at !== null && typeof item.next_refresh_at !== 'string')) {
if (
!isRecord(item) ||
typeof item.id !== 'string' ||
typeof item.source_type !== 'string' ||
typeof item.name !== 'string' ||
typeof item.enabled !== 'boolean' ||
typeof item.schedule_configured !== 'boolean' ||
typeof item.item_count !== 'number' ||
typeof item.updated_at !== 'string' ||
(item.next_refresh_at !== null && typeof item.next_refresh_at !== 'string')
) {
throw new Error('invalid source connection item')
}
return {
id: item.id, source_type: item.source_type as SourceConnectionType, name: item.name,
enabled: item.enabled, schedule_configured: item.schedule_configured,
id: item.id,
source_type: item.source_type as SourceConnectionType,
name: item.name,
enabled: item.enabled,
schedule_configured: item.schedule_configured,
health_status: typeof item.health_status === 'string' ? item.health_status : '',
item_count: item.item_count, updated_at: item.updated_at,
item_count: item.item_count,
updated_at: item.updated_at,
next_refresh_at: (item.next_refresh_at as string | null) ?? null,
}
})
@@ -151,7 +175,7 @@ function parseConnectionList(raw: unknown): SourceConnectionListItem[] {
export async function fetchSourceConnectionList(signal?: AbortSignal): Promise<SourceConnectionListItem[]> {
const response = await fetch('/api/v1/source-connections', { headers: { Accept: 'application/json' }, signal })
if (!response.ok) throw new Error(`加载来源连接失败(HTTP ${response.status})`)
const value = await response.json() as unknown
const value = (await response.json()) as unknown
if (!isRecord(value) || !Array.isArray(value.data)) throw new Error('invalid source connections response')
return parseConnectionList(value.data)
}
@@ -175,10 +199,17 @@ export async function triggerSourceScan(connectionID: string): Promise<Triggered
if (!response.ok) {
throw new Error(response.status === 409 ? '该来源已有扫描正在运行' : `触发来源扫描失败(HTTP ${response.status})`)
}
const raw = await response.json() as unknown
if (!isRecord(raw) || typeof raw.id !== 'string' || typeof raw.source_connection_id !== 'string' ||
typeof raw.source_type !== 'string' || !sourceTypes.has(raw.source_type as SourceConnectionType) ||
typeof raw.state !== 'string' || !scanStates.has(raw.state as SourceScanState) || typeof raw.created_at !== 'string') {
const raw = (await response.json()) as unknown
if (
!isRecord(raw) ||
typeof raw.id !== 'string' ||
typeof raw.source_connection_id !== 'string' ||
typeof raw.source_type !== 'string' ||
!sourceTypes.has(raw.source_type as SourceConnectionType) ||
typeof raw.state !== 'string' ||
!scanStates.has(raw.state as SourceScanState) ||
typeof raw.created_at !== 'string'
) {
throw new Error('invalid triggered source scan response')
}
return {
@@ -191,8 +222,14 @@ export async function triggerSourceScan(connectionID: string): Promise<Triggered
}
export function parseSourceScansResponse(value: unknown): SourceScanPage {
if (!isRecord(value) || !Array.isArray(value.data) || !isCount(value.total) ||
!isCount(value.limit) || value.limit < 1 || !isCount(value.offset)) {
if (
!isRecord(value) ||
!Array.isArray(value.data) ||
!isCount(value.total) ||
!isCount(value.limit) ||
value.limit < 1 ||
!isCount(value.offset)
) {
throw new Error('invalid source scans response')
}
return {
@@ -214,10 +251,15 @@ export function parseSourceScanDetailResponse(value: unknown): SourceScanDetail
const summary = parseSourceScanSummary(value.data)
if (!summary) throw new Error('invalid source scan detail response')
const changes = value.data.changes.map((raw): SourceScanChangeView => {
if (!isRecord(raw) || typeof raw.id !== 'string' ||
(raw.source_item_id !== null && typeof raw.source_item_id !== 'string') ||
typeof raw.change_type !== 'string' || !scanChangeTypes.has(raw.change_type as SourceScanChangeType) ||
(raw.observed_size_bytes !== null && !isCount(raw.observed_size_bytes)) || typeof raw.created_at !== 'string') {
if (
!isRecord(raw) ||
typeof raw.id !== 'string' ||
(raw.source_item_id !== null && typeof raw.source_item_id !== 'string') ||
typeof raw.change_type !== 'string' ||
!scanChangeTypes.has(raw.change_type as SourceScanChangeType) ||
(raw.observed_size_bytes !== null && !isCount(raw.observed_size_bytes)) ||
typeof raw.created_at !== 'string'
) {
throw new Error('invalid source scan change')
}
return {
@@ -231,31 +273,59 @@ export function parseSourceScanDetailResponse(value: unknown): SourceScanDetail
return { ...summary, changes }
}
export async function fetchSourceScans(connectionID: string, limit = 20, offset = 0, signal?: AbortSignal): Promise<SourceScanPage> {
export async function fetchSourceScans(
connectionID: string,
limit = 20,
offset = 0,
signal?: AbortSignal
): Promise<SourceScanPage> {
const params = new URLSearchParams({ limit: String(limit), offset: String(offset) })
const response = await fetch(`/api/v1/source-connections/${encodeURIComponent(connectionID)}/scans?${params}`, {
headers: { Accept: 'application/json' }, signal,
headers: { Accept: 'application/json' },
signal,
})
if (!response.ok) throw new Error(`加载扫描历史失败(HTTP ${response.status})`)
return parseSourceScansResponse(await response.json())
}
export async function fetchSourceScanDetail(connectionID: string, scanID: string, signal?: AbortSignal): Promise<SourceScanDetail> {
const response = await fetch(`/api/v1/source-connections/${encodeURIComponent(connectionID)}/scans/${encodeURIComponent(scanID)}`, {
headers: { Accept: 'application/json' }, signal,
})
export async function fetchSourceScanDetail(
connectionID: string,
scanID: string,
signal?: AbortSignal
): Promise<SourceScanDetail> {
const response = await fetch(
`/api/v1/source-connections/${encodeURIComponent(connectionID)}/scans/${encodeURIComponent(scanID)}`,
{
headers: { Accept: 'application/json' },
signal,
}
)
if (!response.ok) throw new Error(`加载扫描详情失败(HTTP ${response.status})`)
return parseSourceScanDetailResponse(await response.json())
}
function parseSourceScanSummary(raw: unknown): SourceScanSummary | null {
if (raw === null || raw === undefined) return null
if (!isRecord(raw) || typeof raw.id !== 'string' || typeof raw.state !== 'string' ||
!scanStates.has(raw.state as SourceScanState) || typeof raw.error_code !== 'string' ||
typeof raw.created_at !== 'string' || (raw.completed_at !== null && typeof raw.completed_at !== 'string')) {
if (
!isRecord(raw) ||
typeof raw.id !== 'string' ||
typeof raw.state !== 'string' ||
!scanStates.has(raw.state as SourceScanState) ||
typeof raw.error_code !== 'string' ||
typeof raw.created_at !== 'string' ||
(raw.completed_at !== null && typeof raw.completed_at !== 'string')
) {
throw new Error('invalid source scan summary')
}
const countKeys = ['observed_items', 'unchanged_items', 'added_items', 'updated_items', 'over_limit_items', 'missing_items', 'planned_uploads'] as const
const countKeys = [
'observed_items',
'unchanged_items',
'added_items',
'updated_items',
'over_limit_items',
'missing_items',
'planned_uploads',
] as const
for (const key of countKeys) {
if (!isCount(raw[key])) throw new Error('invalid source scan summary')
}
@@ -315,63 +385,153 @@ export interface SourceSettingsDraftRequest {
const revisionStates = new Set<SourceSettingsRevisionState>(['validated', 'active', 'retired', 'failed'])
function parseSettingsRevision(raw: unknown): SourceSettingsRevision {
if (!isRecord(raw) || typeof raw.id !== 'string' || typeof raw.revision_number !== 'number' ||
typeof raw.state !== 'string' || !revisionStates.has(raw.state as SourceSettingsRevisionState) ||
typeof raw.name !== 'string' || typeof raw.endpoint !== 'string' || typeof raw.credential_ref !== 'string' ||
typeof raw.enabled !== 'boolean' || typeof raw.refresh_interval_seconds !== 'string' ||
typeof raw.created_by !== 'string' || typeof raw.created_at !== 'string' ||
(raw.validated_at !== null && typeof raw.validated_at !== 'string') ||
(raw.activated_at !== null && typeof raw.activated_at !== 'string') ||
(raw.retired_at !== null && typeof raw.retired_at !== 'string')) {
if (
!isRecord(raw) ||
typeof raw.id !== 'string' ||
typeof raw.revision_number !== 'number' ||
typeof raw.state !== 'string' ||
!revisionStates.has(raw.state as SourceSettingsRevisionState) ||
typeof raw.name !== 'string' ||
typeof raw.endpoint !== 'string' ||
typeof raw.credential_ref !== 'string' ||
typeof raw.enabled !== 'boolean' ||
typeof raw.refresh_interval_seconds !== 'string' ||
typeof raw.created_by !== 'string' ||
typeof raw.created_at !== 'string' ||
(raw.validated_at !== null && typeof raw.validated_at !== 'string') ||
(raw.activated_at !== null && typeof raw.activated_at !== 'string') ||
(raw.retired_at !== null && typeof raw.retired_at !== 'string')
) {
throw new Error('invalid source settings revision')
}
return {
id: raw.id, revision_number: raw.revision_number, state: raw.state as SourceSettingsRevisionState,
name: raw.name, endpoint: raw.endpoint, credential_ref: raw.credential_ref, enabled: raw.enabled,
refresh_interval_seconds: raw.refresh_interval_seconds, created_by: raw.created_by, created_at: raw.created_at,
id: raw.id,
revision_number: raw.revision_number,
state: raw.state as SourceSettingsRevisionState,
name: raw.name,
endpoint: raw.endpoint,
credential_ref: raw.credential_ref,
enabled: raw.enabled,
refresh_interval_seconds: raw.refresh_interval_seconds,
created_by: raw.created_by,
created_at: raw.created_at,
validated_at: (raw.validated_at as string | null) ?? null,
activated_at: (raw.activated_at as string | null) ?? null,
retired_at: (raw.retired_at as string | null) ?? null,
}
}
export async function fetchSourceSettingsRevisions(connectionID: string, signal?: AbortSignal): Promise<SourceSettingsRevision[]> {
export async function fetchSourceSettingsRevisions(
connectionID: string,
signal?: AbortSignal
): Promise<SourceSettingsRevision[]> {
const response = await fetch(`/api/v1/source-connections/${encodeURIComponent(connectionID)}/settings/revisions`, {
headers: { Accept: 'application/json' },
signal,
})
if (!response.ok) throw new Error(`加载来源设置历史失败(HTTP ${response.status})`)
const value = await response.json() as unknown
const value = (await response.json()) as unknown
if (!isRecord(value) || !Array.isArray(value.data)) throw new Error('invalid source settings revisions response')
return value.data.map(parseSettingsRevision)
}
async function postSourceSettings(path: string, body: string, errorMessage: string): Promise<{ operation: string; id: string | null }> {
async function postSourceSettings(
path: string,
body: string,
errorMessage: string
): Promise<{ operation: string; id: string | null }> {
const response = await fetch(path, {
method: 'POST',
headers: { Accept: 'application/json', 'Content-Type': 'application/json', 'X-Governance-Action': 'source-settings', 'Idempotency-Key': newOperationID() },
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
'X-Governance-Action': 'source-settings',
'Idempotency-Key': newOperationID(),
},
body,
})
if (!response.ok) {
let detail = ''
try {
const parsed = await response.json() as unknown
const parsed = (await response.json()) as unknown
if (isRecord(parsed) && typeof parsed.error === 'string') detail = parsed.error
} catch { /* keep status-only message */ }
} catch {
/* keep status-only message */
}
throw new Error(detail ? `${errorMessage}:${detail}` : `${errorMessage}(HTTP ${response.status})`)
}
const value = await response.json() as unknown
if (!isRecord(value) || (value.operation !== 'created' && value.operation !== 'replayed') || !isRecord(value.data)) throw new Error('invalid source settings response')
const value = (await response.json()) as unknown
if (!isRecord(value) || (value.operation !== 'created' && value.operation !== 'replayed') || !isRecord(value.data))
throw new Error('invalid source settings response')
const id = typeof value.data.id === 'string' ? value.data.id : null
return { operation: value.operation, id }
}
export async function createSourceSettingsRevision(connectionID: string, draft: SourceSettingsDraftRequest): Promise<{ operation: string; id: string }> {
const result = await postSourceSettings(`/api/v1/source-connections/${encodeURIComponent(connectionID)}/settings/revisions`, JSON.stringify(draft), '保存来源设置草稿失败')
export async function createSourceSettingsRevision(
connectionID: string,
draft: SourceSettingsDraftRequest
): Promise<{ operation: string; id: string }> {
const result = await postSourceSettings(
`/api/v1/source-connections/${encodeURIComponent(connectionID)}/settings/revisions`,
JSON.stringify(draft),
'保存来源设置草稿失败'
)
if (!result.id) throw new Error('创建来源设置草稿未返回修订号')
return result as { operation: string; id: string }
}
export async function activateSourceSettingsRevision(connectionID: string, revisionID: string): Promise<{ operation: string }> {
return postSourceSettings(`/api/v1/source-connections/${encodeURIComponent(connectionID)}/settings/revisions/${encodeURIComponent(revisionID)}/activate`, '{}', '启用来源设置修订失败')
export async function activateSourceSettingsRevision(
connectionID: string,
revisionID: string
): Promise<{ operation: string }> {
return postSourceSettings(
`/api/v1/source-connections/${encodeURIComponent(connectionID)}/settings/revisions/${encodeURIComponent(revisionID)}/activate`,
'{}',
'启用来源设置修订失败'
)
}
/**
* 「去掉来源」:归档下架(**不是物理删除**)。
* 来源会从数据源配置、来源与目录、计划刷新与手动扫描里消失;目录快照、发布记录与
* 审计数据原样保留 —— 再用 dws 重新登记同一个来源就会原地复活。幂等,仅管理员可用。
*/
export async function archiveSourceConnection(connectionID: string): Promise<void> {
await postSourceArchive(
`/api/v1/source-connections/${encodeURIComponent(connectionID)}/archive`,
'source-archive',
'去掉来源失败'
)
}
/** 撤销归档:来源重新出现,但保持停用(调度不会自动恢复)。 */
export async function restoreSourceConnection(connectionID: string): Promise<void> {
await postSourceArchive(
`/api/v1/source-connections/${encodeURIComponent(connectionID)}/restore`,
'source-restore',
'恢复来源失败'
)
}
async function postSourceArchive(path: string, intent: string, errorMessage: string): Promise<void> {
const response = await fetch(path, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
'X-Governance-Action': intent,
'Idempotency-Key': newOperationID(),
},
body: '{}',
})
if (!response.ok) {
let detail = ''
try {
const parsed = (await response.json()) as unknown
if (isRecord(parsed) && typeof parsed.error === 'string') detail = parsed.error
} catch {
/* keep status-only message */
}
throw new Error(detail ? `${errorMessage}:${detail}` : `${errorMessage}(HTTP ${response.status})`)
}
}