Elffuss BraidHub

kikocis/utopia public

dev · 4.2 KiBRawPermalink
1//! 采纳时的主宾对调,打在真库上。
2//!
3//! `X produced_by Y` 与 `Y produces X` 是同一条边。采纳被动形时不对调,
4//! 图上就会多出一条反着的箭头,而且它跟正向那些永远合不到一起。
5//!
6//! 这条只能真跑:对调发生在 `adopt` 的 INSERT 里,`cargo check` 看不见 SQL。
7//! 实测中它确实漏过——`demo-b3` 那个库里 `produced_by` 与 `produces`
8//! 各成一个关系,因为采纳路径压根没走匹配器。
9
10use sqlx::PgPool;
11use uuid::Uuid;
12
13#[tokio::test]
14async fn adopting_a_passive_wording_flips_subject_and_object() -> anyhow::Result<()> {
15 let Ok(url) = std::env::var("UTOPIA_DATABASE_URL") else {
16 eprintln!("跳过:未设 UTOPIA_DATABASE_URL");
17 return Ok(());
18 };
19 let pool = PgPool::connect(&url).await?;
20 let (org, ws, kb) = (Uuid::now_v7(), Uuid::now_v7(), Uuid::now_v7());
21 let etype = Uuid::now_v7();
22 let produces = Uuid::now_v7();
23 let (openai, chatgpt) = (Uuid::now_v7(), Uuid::now_v7());
24 let (doc, chunk, fact) = (Uuid::now_v7(), Uuid::now_v7(), Uuid::now_v7());
25
26 sqlx::query("INSERT INTO organizations (id, name) VALUES ($1, 'swap-test')")
27 .bind(org)
28 .execute(&pool)
29 .await?;
30 sqlx::query("INSERT INTO workspaces (id, org_id, name) VALUES ($1, $2, 'swap-test')")
31 .bind(ws)
32 .bind(org)
33 .execute(&pool)
34 .await?;
35 sqlx::query(
36 "INSERT INTO knowledge_bases (id, workspace_id, name) VALUES ($1, $2, 'swap-test')",
37 )
38 .bind(kb)
39 .bind(ws)
40 .execute(&pool)
41 .await?;
42 sqlx::query(
43 "INSERT INTO entity_types (id, kb_id, key, label) VALUES ($1, $2, 'thing', 'Thing')",
44 )
45 .bind(etype)
46 .bind(kb)
47 .execute(&pool)
48 .await?;
49 sqlx::query("INSERT INTO relation_types (id, kb_id, key, label) VALUES ($1, $2, $3, $3)")
50 .bind(produces)
51 .bind(kb)
52 .bind("produces")
53 .execute(&pool)
54 .await?;
55 for (id, name) in [(openai, "OpenAI"), (chatgpt, "ChatGPT")] {
56 sqlx::query(
57 "INSERT INTO entities (id, kb_id, type_id, canonical_name) VALUES ($1,$2,$3,$4)",
58 )
59 .bind(id)
60 .bind(kb)
61 .bind(etype)
62 .bind(name)
63 .execute(&pool)
64 .await?;
65 }
66 sqlx::query("INSERT INTO documents (id, kb_id, filename, sha256) VALUES ($1,$2,'a.txt','a')")
67 .bind(doc)
68 .bind(kb)
69 .execute(&pool)
70 .await?;
71 sqlx::query("INSERT INTO chunks (id, kb_id, document_id, seq, text) VALUES ($1,$2,$3,0,'x')")
72 .bind(chunk)
73 .bind(kb)
74 .bind(doc)
75 .execute(&pool)
76 .await?;
77 // 原文说的是 "ChatGPT produced_by OpenAI",本体里没有这个关系,
78 // 于是这条事实**没有谓词**——兜底谓词已经不存在了
79 sqlx::query(
80 "INSERT INTO facts (id, kb_id, subject_id, predicate_id, object_id)
81 VALUES ($1, $2, $3, NULL, $4)",
82 )
83 .bind(fact)
84 .bind(kb)
85 .bind(chatgpt)
86 .bind(openai)
87 .execute(&pool)
88 .await?;
89 sqlx::query(
90 "INSERT INTO fact_evidence (fact_id, chunk_id, document_id, proposed_predicate)
91 VALUES ($1, $2, $3, 'produced_by')",
92 )
93 .bind(fact)
94 .bind(chunk)
95 .bind(doc)
96 .execute(&pool)
97 .await?;
98
99 let run = async {
100 let moved = utopia_store::graph::adopt_proposed_predicates(
101 &pool,
102 kb,
103 produces,
104 &["produced_by".to_string()],
105 true,
106 )
107 .await?
108 .moved;
109 assert_eq!(moved, 1);
110 // 改写后应该是 OpenAI -[produces]-> ChatGPT,**方向反过来**
111 let (s, o): (Uuid, Option<Uuid>) = sqlx::query_as(
112 "SELECT subject_id, object_id FROM facts
113 WHERE kb_id = $1 AND predicate_id = $2 AND invalidated_at IS NULL",
114 )
115 .bind(kb)
116 .bind(produces)
117 .fetch_one(&pool)
118 .await?;
119 assert_eq!(s, openai, "主语该是 OpenAI(原来是宾语)");
120 assert_eq!(o, Some(chatgpt), "宾语该是 ChatGPT(原来是主语)");
121 Ok::<_, anyhow::Error>(())
122 }
123 .await;
124
125 sqlx::query("DELETE FROM knowledge_bases WHERE id = $1")
126 .bind(kb)
127 .execute(&pool)
128 .await?;
129 run
130}