feat(knowledge): enhance activity logging for FAQ imports

- Added new actions for FAQ import lifecycle: `faq.import_started`, `faq.import_completed`, and `faq.import_failed`.
- Updated activity details to include success, failure, and skipped counts for FAQ imports.
- Enhanced UI to display additional activity information, including filtering options and clearer summaries.
- Implemented tests to validate the new FAQ import outcomes and activity details.

This commit improves the tracking and visibility of FAQ import activities within the knowledge base, ensuring better audit trails and user feedback.
This commit is contained in:
wizardchen
2026-07-22 20:08:31 +08:00
committed by lyingbug
parent a3a0308b62
commit 5af8eae81d
14 changed files with 650 additions and 100 deletions
@@ -52,6 +52,60 @@ func TestRecordKBActivityCarriesInitiatorAndTaskMetadata(t *testing.T) {
}
}
func TestFAQImportCompletedOutcome(t *testing.T) {
cases := []struct {
success, failed, skipped int
outcome types.AuditOutcome
}{
{10, 0, 0, types.AuditOutcomeSuccess},
{8, 2, 0, types.AuditOutcomePartial},
{5, 1, 2, types.AuditOutcomePartial},
{0, 1, 1, types.AuditOutcomePartial},
{0, 2, 0, types.AuditOutcomeFailed},
{0, 0, 2, types.AuditOutcomeFailed},
}
for _, tc := range cases {
outcome := faqImportCompletedOutcome(tc.success, tc.failed, tc.skipped)
if outcome != tc.outcome {
t.Fatalf("faqImportCompletedOutcome(%d,%d,%d) = %s, want %s",
tc.success, tc.failed, tc.skipped, outcome, tc.outcome)
}
}
}
func TestFAQImportActivityDetails(t *testing.T) {
payload := &types.FAQImportPayload{Mode: types.FAQBatchModeAppend}
progress := &types.FAQImportProgress{SuccessCount: 0, FailedCount: 1, Total: 2}
details := faqImportActivityDetails(payload, progress, 2)
if details["count"] != 0 {
t.Fatalf("count = %#v, want 0", details["count"])
}
if details["total"] != 2 || details["failed"] != 1 || details["skipped"] != 1 {
t.Fatalf("details = %#v", details)
}
}
func TestKBActivityAppendSampleTitles(t *testing.T) {
details := map[string]any{"count": 3}
kbActivityAppendSampleTitles(details, " Alpha ", "Beta", "Alpha", "Gamma")
if details["title"] != "Alpha" {
t.Fatalf("title = %#v", details["title"])
}
titles, ok := details["titles"].([]string)
if !ok || len(titles) != 3 {
t.Fatalf("titles = %#v", details["titles"])
}
single := map[string]any{"count": 1}
kbActivityAppendSampleTitles(single, "Only one")
if single["title"] != "Only one" {
t.Fatalf("single title = %#v", single["title"])
}
if _, exists := single["titles"]; exists {
t.Fatalf("single titles should be omitted: %#v", single)
}
}
func TestRecordKBActivityCanSuppressCompositeTaskChildren(t *testing.T) {
audit := &captureKBActivityAudit{}
ctx := withKBActivitySuppressed(context.Background())
@@ -61,3 +115,29 @@ func TestRecordKBActivityCanSuppressCompositeTaskChildren(t *testing.T) {
t.Fatalf("suppressed child activity was recorded: %#v", audit.entry)
}
}
// TestKBActivityTriggerReflectsInitiatorIdentity locks in the worker
// attribution used by ProcessKBClone / ProcessKnowledgeMove /
// ProcessKnowledgeListDelete / ProcessKnowledgeListReparse: the task
// trigger must be derived from the restored initiator, not hard-coded to
// "user". A synthetic (API-key) or empty initiator is service-owned work
// and must therefore report "system", matching the empty actor attribution.
func TestKBActivityTriggerReflectsInitiatorIdentity(t *testing.T) {
// Real human initiator → "user".
userCtx := types.TaskInitiator{UserID: "user-1", Role: types.TenantRoleAdmin}.Apply(context.Background())
if got := kbActivityTrigger(userCtx); got != "user" {
t.Fatalf("real initiator trigger = %q, want user", got)
}
// Empty / legacy initiator (e.g. scheduler or API-key origin) → "system".
systemCtx := types.TaskInitiator{}.Apply(context.Background())
if got := kbActivityTrigger(systemCtx); got != "system" {
t.Fatalf("empty initiator trigger = %q, want system", got)
}
// Synthetic API-key user is service identity → "system".
synthCtx := context.WithValue(context.Background(), types.UserIDContextKey, "system-7")
if got := kbActivityTrigger(synthCtx); got != "system" {
t.Fatalf("synthetic user trigger = %q, want system", got)
}
}