LLM Wiki is a cross-platform desktop application that turns your documents into an organized, interlinked knowledge base — automatically. Instead of traditional RAG (retrieve-and-answer from scratch every time), the LLM incrementally builds and maintains a persistent wiki from your sources。
TypeScript
test: Tier 6 property tests (fast-check)
nash_su5dc545c7bc1d
diff --git a/package-lock.json b/package-lock.jsonindex da3a079e..97ce8832 100644--- a/package-lock.json+++ b/package-lock.json@@ -48,6 +48,7 @@"@types/react": "^19.0.0","@types/react-dom": "^19.0.0","@vitejs/plugin-react": "^6.0.1",+ "fast-check": "^4.7.0","typescript": "^5.7.3","vite": "^8.0.0","vitest": "^4.1.4"@@ -4530,6 +4531,29 @@"integrity": "sha512-fjquC59cD7CyW6urNXK0FBufkZcoiGG80wTuPujX590cB5Ttln20E2UB4S/WARVqhXffZl2LNgS+gQdPIIim/g==","license": "MIT"},+ "node_modules/fast-check": {+ "version": "4.7.0",+ "resolved": "https://registry.npmjs.org/fast-check/-/fast-check-4.7.0.tgz",+ "integrity": "sha512-NsZRtqvSSoCP0HbNjUD+r1JH8zqZalyp6gLY9e7OYs7NK9b6AHOs2baBFeBG7bVNsuoukh89x2Yg3rPsul8ziQ==",+ "dev": true,+ "funding": [+ {+ "type": "individual",+ "url": "https://github.com/sponsors/dubzzz"+ },+ {+ "type": "opencollective",+ "url": "https://opencollective.com/fast-check"+ }+ ],+ "license": "MIT",+ "dependencies": {+ "pure-rand": "^8.0.0"+ },+ "engines": {+ "node": ">=12.17.0"+ }+ },"node_modules/fast-deep-equal": {"version": "3.1.3","resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz",@@ -7825,6 +7849,23 @@"node": ">= 0.10"}},+ "node_modules/pure-rand": {+ "version": "8.4.0",+ "resolved": "https://registry.npmjs.org/pure-rand/-/pure-rand-8.4.0.tgz",+ "integrity": "sha512-IoM8YF/jY0hiugFo/wOWqfmarlE6J0wc6fDK1PhftMk7MGhVZl88sZimmqBBFomLOCSmcCCpsfj7wXASCpvK9A==",+ "dev": true,+ "funding": [+ {+ "type": "individual",+ "url": "https://github.com/sponsors/dubzzz"+ },+ {+ "type": "opencollective",+ "url": "https://opencollective.com/fast-check"+ }+ ],+ "license": "MIT"+ },"node_modules/qs": {"version": "6.15.0","resolved": "https://registry.npmjs.org/qs/-/qs-6.15.0.tgz",diff --git a/package.json b/package.jsonindex 6c115f96..9c19a49d 100644--- a/package.json+++ b/package.json@@ -51,6 +51,7 @@"@types/react": "^19.0.0","@types/react-dom": "^19.0.0","@vitejs/plugin-react": "^6.0.1",+ "fast-check": "^4.7.0","typescript": "^5.7.3","vite": "^8.0.0","vitest": "^4.1.4"diff --git a/src/lib/detect-language.property.test.ts b/src/lib/detect-language.property.test.tsnew file mode 100644index 00000000..f2f1f141--- /dev/null+++ b/src/lib/detect-language.property.test.ts@@ -0,0 +1,82 @@+/**+ * Tier 6 — property tests for detectLanguage.+ */+import { describe, it, expect } from "vitest"+import fc from "fast-check"+import { detectLanguage } from "./detect-language"++describe("detectLanguage — properties", () => {+ it("pure ASCII random text always returns English", () => {+ // Exclude characters that would trigger Latin-language word heuristics+ // by restricting to printable ASCII without common short words.+ fc.assert(+ fc.property(+ fc.string({ minLength: 0, maxLength: 200 }).filter((s) => /^[\x20-\x7E]*$/.test(s)),+ (input) => {+ const lang = detectLanguage(input)+ // ASCII-only input may hit French/German/Spanish/Dutch/etc. heuristics+ // if it contains those trigger words. But for truly random ASCII+ // strings fast-check produces, hits are rare. We assert the weaker+ // property: the result is SOME known English-like language name.+ const knownLatin = new Set([+ "English", "French", "German", "Spanish", "Portuguese",+ "Italian", "Dutch", "Swedish", "Norwegian", "Danish",+ "Finnish", "Indonesian", "Swahili", "Polish", "Czech",+ "Romanian", "Hungarian", "Vietnamese", "Turkish",+ ])+ expect(knownLatin.has(lang)).toBe(true)+ },+ ),+ )+ })++ it("2+ CJK characters yield Chinese", () => {+ fc.assert(+ fc.property(+ fc.array(+ fc.integer({ min: 0x4E00, max: 0x9FFF }).map((cp) => String.fromCodePoint(cp)),+ { minLength: 2, maxLength: 50 },+ ),+ (cjkChars) => {+ expect(detectLanguage(cjkChars.join(""))).toBe("Chinese")+ },+ ),+ )+ })++ it("2+ Hiragana characters yield Japanese", () => {+ fc.assert(+ fc.property(+ fc.array(+ fc.integer({ min: 0x3041, max: 0x3096 }).map((cp) => String.fromCodePoint(cp)),+ { minLength: 2, maxLength: 50 },+ ),+ (chars) => {+ expect(detectLanguage(chars.join(""))).toBe("Japanese")+ },+ ),+ )+ })++ it("2+ Hangul characters yield Korean", () => {+ fc.assert(+ fc.property(+ fc.array(+ fc.integer({ min: 0xAC00, max: 0xD7AF }).map((cp) => String.fromCodePoint(cp)),+ { minLength: 2, maxLength: 50 },+ ),+ (chars) => {+ expect(detectLanguage(chars.join(""))).toBe("Korean")+ },+ ),+ )+ })++ it("never throws on arbitrary strings", () => {+ fc.assert(+ fc.property(fc.string(), (s) => {+ expect(() => detectLanguage(s)).not.toThrow()+ }),+ )+ })+})diff --git a/src/lib/path-utils.property.test.ts b/src/lib/path-utils.property.test.tsnew file mode 100644index 00000000..4be553a8--- /dev/null+++ b/src/lib/path-utils.property.test.ts@@ -0,0 +1,77 @@+/**+ * Tier 6 — property tests for path-utils.+ */+import { describe, it, expect } from "vitest"+import fc from "fast-check"+import { normalizePath, joinPath, getFileName, getFileStem } from "./path-utils"++describe("normalizePath — properties", () => {+ it("is idempotent", () => {+ fc.assert(+ fc.property(fc.string(), (p) => {+ expect(normalizePath(normalizePath(p))).toBe(normalizePath(p))+ }),+ )+ })++ it("output contains no backslashes", () => {+ fc.assert(+ fc.property(fc.string(), (p) => {+ expect(normalizePath(p)).not.toMatch(/\\/)+ }),+ )+ })++ it("only changes backslashes — length is preserved", () => {+ fc.assert(+ fc.property(fc.string(), (p) => {+ expect(normalizePath(p).length).toBe(p.length)+ }),+ )+ })+})++describe("getFileName / getFileStem — properties", () => {+ it("getFileName of a path ending in /foo is 'foo'", () => {+ fc.assert(+ fc.property(+ fc.string({ minLength: 1 }).filter((s) => !s.includes("/") && !s.includes("\\") && s.length > 0),+ (name) => {+ expect(getFileName(`/some/path/${name}`)).toBe(name)+ expect(getFileName(`C:\\a\\b\\${name}`)).toBe(name)+ },+ ),+ )+ })++ it("getFileStem strips at the LAST dot (if one exists past position 0)", () => {+ fc.assert(+ fc.property(+ fc.string({ minLength: 1 }).filter((s) => !s.includes("/") && !s.includes("\\") && !s.includes(".")),+ fc.string({ minLength: 1 }).filter((s) => !s.includes("/") && !s.includes("\\") && !s.includes(".")),+ (stem, ext) => {+ expect(getFileStem(`${stem}.${ext}`)).toBe(stem)+ },+ ),+ )+ })+})++describe("joinPath — properties", () => {+ it("contains no double-slashes in output", () => {+ fc.assert(+ fc.property(fc.array(fc.string({ maxLength: 10 }), { minLength: 1, maxLength: 5 }), (segs) => {+ const joined = joinPath(...segs)+ expect(joined).not.toMatch(/\/\//)+ }),+ )+ })++ it("contains no backslashes in output", () => {+ fc.assert(+ fc.property(fc.array(fc.string({ maxLength: 10 }), { minLength: 1, maxLength: 5 }), (segs) => {+ expect(joinPath(...segs)).not.toMatch(/\\/)+ }),+ )+ })+})diff --git a/src/lib/review-utils.property.test.ts b/src/lib/review-utils.property.test.tsnew file mode 100644index 00000000..55722dce--- /dev/null+++ b/src/lib/review-utils.property.test.ts@@ -0,0 +1,66 @@+/**+ * Tier 6 — property tests for review-utils.+ */+import { describe, it, expect } from "vitest"+import fc from "fast-check"+import { normalizeReviewTitle } from "./review-utils"++describe("normalizeReviewTitle — properties", () => {+ it("is idempotent: normalize(normalize(x)) === normalize(x)", () => {+ fc.assert(+ fc.property(fc.string(), (input) => {+ const once = normalizeReviewTitle(input)+ const twice = normalizeReviewTitle(once)+ expect(twice).toBe(once)+ }),+ )+ })++ it("is lowercase: result never contains uppercase ASCII letters", () => {+ fc.assert(+ fc.property(fc.string(), (input) => {+ const out = normalizeReviewTitle(input)+ expect(out).toBe(out.toLowerCase())+ }),+ )+ })++ it("collapses any run of whitespace to a single space", () => {+ fc.assert(+ fc.property(fc.string({ minLength: 1 }), (input) => {+ const out = normalizeReviewTitle(input)+ // Property: no double-space anywhere in output+ expect(out).not.toMatch(/ {2,}/)+ }),+ )+ })++ it("is monotonic in prefix-stripping: stripping a recognized prefix yields same result as no prefix", () => {+ fc.assert(+ fc.property(+ fc.constantFrom("Missing page: ", "缺失页面:", "重复页面:", "Duplicate page: "),+ fc.string({ minLength: 1 }).filter((s) => !/[::]/.test(s)),+ (prefix, body) => {+ const withPrefix = normalizeReviewTitle(prefix + body)+ const bare = normalizeReviewTitle(body)+ expect(withPrefix).toBe(bare)+ },+ ),+ )+ })++ it("merges variant-prefix titles to the same key", () => {+ fc.assert(+ fc.property(+ fc.string({ minLength: 1 }).filter((s) => !/[::]/.test(s) && s.trim().length > 0),+ (body) => {+ const a = normalizeReviewTitle(`Missing page: ${body}`)+ const b = normalizeReviewTitle(`缺失页面: ${body}`)+ const c = normalizeReviewTitle(`Missing-Page: ${body}`)+ expect(a).toBe(b)+ expect(a).toBe(c)+ },+ ),+ )+ })+})diff --git a/src/lib/sweep-reviews.property.test.ts b/src/lib/sweep-reviews.property.test.tsnew file mode 100644index 00000000..a653ecce--- /dev/null+++ b/src/lib/sweep-reviews.property.test.ts@@ -0,0 +1,95 @@+/**+ * Tier 6 — property tests for extractJsonObject.+ */+import { describe, it, expect } from "vitest"+import fc from "fast-check"+import { extractJsonObject } from "./sweep-reviews"++/** A valid JSON value. */+const jsonValueArb: fc.Arbitrary<unknown> = fc.jsonValue()++/** A JSON object (top-level {}). */+const jsonObjectArb = fc.dictionary(fc.string(), jsonValueArb)++describe("extractJsonObject — properties", () => {+ // Compare through JSON round-trip so edge cases like -0 vs +0 (which JSON+ // doesn't preserve) don't trip up the property. We care that the object+ // survives a round-trip through our extractor, not that -0 stays negative.+ function jsonRoundTrip(x: unknown): unknown {+ return JSON.parse(JSON.stringify(x))+ }++ it("extracts a bare JSON object unchanged (round-trip parses to same value)", () => {+ fc.assert(+ fc.property(jsonObjectArb, (obj) => {+ const serialized = JSON.stringify(obj)+ const extracted = extractJsonObject(serialized)+ expect(extracted).toBeTruthy()+ expect(JSON.parse(extracted)).toEqual(jsonRoundTrip(obj))+ }),+ )+ })++ it("extracts a fenced JSON object", () => {+ fc.assert(+ fc.property(jsonObjectArb, (obj) => {+ const serialized = JSON.stringify(obj)+ const wrapped = "```json\n" + serialized + "\n```"+ const extracted = extractJsonObject(wrapped)+ expect(JSON.parse(extracted)).toEqual(jsonRoundTrip(obj))+ }),+ )+ })++ it("finds a JSON object trailing after prose (no-brace prose)", () => {+ fc.assert(+ fc.property(+ fc.string().filter((s) => !s.includes("{") && !s.includes("}")),+ jsonObjectArb,+ (prose, obj) => {+ const input = prose + " " + JSON.stringify(obj)+ const extracted = extractJsonObject(input)+ expect(JSON.parse(extracted)).toEqual(jsonRoundTrip(obj))+ },+ ),+ )+ })++ it("returns a balanced {...} substring — depth returns to 0", () => {+ fc.assert(+ fc.property(jsonObjectArb, (obj) => {+ const input = "prose " + JSON.stringify(obj) + " trailing"+ const extracted = extractJsonObject(input)+ if (!extracted) return+ // Count braces outside strings — must balance+ let depth = 0+ let inString = false+ let escape = false+ for (const ch of extracted) {+ if (escape) {+ escape = false+ continue+ }+ if (ch === "\\" && inString) {+ escape = true+ continue+ }+ if (ch === '"') inString = !inString+ else if (!inString) {+ if (ch === "{") depth+++ else if (ch === "}") depth--+ }+ }+ expect(depth).toBe(0)+ }),+ )+ })++ it("never throws, even on random garbage", () => {+ fc.assert(+ fc.property(fc.string(), (input) => {+ expect(() => extractJsonObject(input)).not.toThrow()+ }),+ )+ })+})diff --git a/src/stores/review-store.property.test.ts b/src/stores/review-store.property.test.tsnew file mode 100644index 00000000..d53d8bfe--- /dev/null+++ b/src/stores/review-store.property.test.ts@@ -0,0 +1,119 @@+/**+ * Tier 6 — property tests for review-store dedupe invariants.+ */+import { describe, it, expect, beforeEach } from "vitest"+import fc from "fast-check"+import { useReviewStore, type ReviewItem } from "./review-store"+import { normalizeReviewTitle } from "@/lib/review-utils"++beforeEach(() => {+ useReviewStore.setState({ items: [] })+})++const typeArb = fc.constantFrom<ReviewItem["type"]>(+ "contradiction",+ "duplicate",+ "missing-page",+ "confirm",+ "suggestion",+)++const reviewInputArb = fc.record({+ type: typeArb,+ title: fc.string({ minLength: 1, maxLength: 60 }),+ description: fc.string({ maxLength: 100 }),+ options: fc.constant([]),+ affectedPages: fc.option(fc.array(fc.string(), { maxLength: 4 })),+ searchQueries: fc.option(fc.array(fc.string(), { maxLength: 4 })),+})++function key(type: string, title: string): string {+ return `${type}::${normalizeReviewTitle(title)}`+}++describe("review-store addItems — dedupe invariants", () => {+ it("after ANY sequence of addItems, pending items have unique (type, normalized title)", () => {+ fc.assert(+ fc.property(fc.array(fc.array(reviewInputArb, { maxLength: 8 }), { maxLength: 6 }), (batches) => {+ useReviewStore.setState({ items: [] })++ for (const batch of batches) {+ const input = batch.map((b) => ({+ type: b.type,+ title: b.title,+ description: b.description,+ options: b.options,+ affectedPages: b.affectedPages ?? undefined,+ searchQueries: b.searchQueries ?? undefined,+ }))+ useReviewStore.getState().addItems(input)+ }++ const pending = useReviewStore.getState().items.filter((i) => !i.resolved)+ const keys = pending.map((i) => key(i.type, i.title))+ const unique = new Set(keys)+ expect(unique.size).toBe(keys.length)+ }),+ )+ })++ it("merge preserves the union of affectedPages across duplicates", () => {+ fc.assert(+ fc.property(+ fc.string({ minLength: 1, maxLength: 20 }).filter((s) => s.trim().length > 0),+ typeArb,+ fc.array(fc.array(fc.string({ maxLength: 20 }), { maxLength: 5 }), { minLength: 2, maxLength: 5 }),+ (title, type, affectedBatches) => {+ useReviewStore.setState({ items: [] })++ for (const pages of affectedBatches) {+ useReviewStore.getState().addItems([+ {+ type,+ title,+ description: "",+ options: [],+ affectedPages: pages.length > 0 ? pages : undefined,+ },+ ])+ }++ const pending = useReviewStore.getState().items.filter((i) => !i.resolved)+ // Only one item for this (type, title)+ expect(pending.length).toBe(1)++ const allExpectedPages = new Set(affectedBatches.flat())+ const actualPages = new Set(pending[0].affectedPages ?? [])+ expect(actualPages).toEqual(allExpectedPages)+ },+ ),+ )+ })++ it("resolveItem always makes resolved item eligible for a NEW pending item of same key", () => {+ fc.assert(+ fc.property(+ fc.string({ minLength: 1, maxLength: 20 }).filter((s) => s.trim().length > 0),+ typeArb,+ (title, type) => {+ useReviewStore.setState({ items: [] })++ useReviewStore.getState().addItems([+ { type, title, description: "", options: [], affectedPages: ["first.md"] },+ ])+ const firstId = useReviewStore.getState().items[0].id+ useReviewStore.getState().resolveItem(firstId, "auto-resolved")++ useReviewStore.getState().addItems([+ { type, title, description: "", options: [], affectedPages: ["second.md"] },+ ])++ const all = useReviewStore.getState().items+ expect(all.length).toBe(2)+ expect(all.filter((i) => i.resolved)).toHaveLength(1)+ expect(all.filter((i) => !i.resolved)).toHaveLength(1)+ },+ ),+ )+ })+})