From fedda70a028fe1ba7e66f0a4c9eeb11c9a2d14d1 Mon Sep 17 00:00:00 2001 From: Nicolas DUBIEN Date: Sat, 7 Feb 2026 18:11:10 +0000 Subject: [PATCH 1/3] test: cover frontmatter with pbt --- .../utils/parse-basic-frontmatter.spec.ts | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) diff --git a/test/unit/shared/utils/parse-basic-frontmatter.spec.ts b/test/unit/shared/utils/parse-basic-frontmatter.spec.ts index 0b2d177d70..d30113e2bd 100644 --- a/test/unit/shared/utils/parse-basic-frontmatter.spec.ts +++ b/test/unit/shared/utils/parse-basic-frontmatter.spec.ts @@ -1,4 +1,5 @@ import { describe, expect, it } from 'vitest' +import fc from 'fast-check' import { parseBasicFrontmatter } from '../../../../shared/utils/parse-basic-frontmatter' describe('parseBasicFrontmatter', () => { @@ -105,4 +106,109 @@ describe('parseBasicFrontmatter', () => { tags: ['a', 'b'], }) }) + + it.fails('handles string numerics as strings', () => { + const input = "---\nid: '42'\n---" + expect(parseBasicFrontmatter(input)).toEqual({ id: '42' }) + }) + + it.fails('handles numbers using scientific notation', () => { + const input = '---\nprice: 1e+50\n---' + expect(parseBasicFrontmatter(input)).toEqual({ price: 1e50 }) + }) + + it.fails('handles escaped double quote', () => { + const input = '---\nquote: "He said, \\"Hello\\""\n---' + expect(parseBasicFrontmatter(input)).toEqual({ quote: 'He said, "Hello"' }) + }) + + it('strips leading and trailing whitespace', () => { + const input = '---\ntext: Something to say \n---' + expect(parseBasicFrontmatter(input)).toEqual({ text: 'Something to say' }) + }) + + it.fails('handles numeric in arrays', () => { + const input = '---\ntags: [1, 2]\n---' + expect(parseBasicFrontmatter(input)).toEqual({ tags: [1, 2] }) + }) + + it('handles strings with array like content', () => { + const input = '---\ntext: Read the content of this array [1, 2, 3]\n---' + expect(parseBasicFrontmatter(input)).toEqual({ + text: 'Read the content of this array [1, 2, 3]', + }) + }) + + it.fails('handles quoted strings with array as content', () => { + const input = '---\nvalue: "[123]"\n---' + expect(parseBasicFrontmatter(input)).toEqual({ value: '[123]' }) + }) + + it.fails('handles string with commas when quoted in arrays', () => { + const input = '---\ntags: ["foo, bar", "baz"]\n---' + expect(parseBasicFrontmatter(input)).toEqual({ tags: ['foo, bar', 'baz'] }) + }) + + it('should parse back every key-value pair from generated frontmatter', () => { + const keyArb = fc.stringMatching(/^[a-zA-Z][a-zA-Z0-9_]*$/) + const singleValueArbs = (inArray: boolean) => + // for arrays: all values get parsed as strings and there should not be any comma in the value even for quoted strings + [ + // For boolean values + fc.boolean().map(b => ({ raw: `${b}`, expected: inArray ? `${b}` : b })), + // For number values + fc + .oneof( + // no support for -0 + fc.constant(0), + // no support for scientific notation + // anything <0.000001 will be displayed in scientific notation, and anything >999999999999999900000 too + fc.double({ min: 0.000001, max: 999999999999999900000, noNaN: true }), + fc.double({ min: -999999999999999900000, max: -0.000001, noNaN: true }), + ) + .map(n => ({ raw: `${n}`, expected: inArray ? `${n}` : n })), + // For string values + fc.oneof( + fc + .stringMatching(inArray ? /^[^',]*$/ : /^[^']*$/) // single-quoted string + .filter(v => Number.isNaN(Number(v))) // numbers, even quoted ones, get parsed as numbers + .map(v => ({ raw: `'${v}'`, expected: v })), + fc + .stringMatching(inArray ? /^[^",]*$/ : /^[^"]*$/) // double-quoted string + .filter(v => Number.isNaN(Number(v))) + .map(v => ({ raw: `"${v}"`, expected: v })), + fc // need to forbid [, ', " or space as first character + .stringMatching(inArray ? /^[^,]+$/ : /^.+$/) // leading and trailing whitespace are ignored for unquoted strings + .filter(v => Number.isNaN(Number(v))) + .map(v => v.trim()) + .map(v => ({ raw: `'${v}'`, expected: v })), + ), + ] + const valueArb = fc.oneof( + ...singleValueArbs(false), + fc + .array(fc.oneof(...singleValueArbs(true)), { minLength: 1 }) // all values get read as strings, no support to empty arrays + .map(arr => ({ + raw: `[${arr.map(v => v.raw).join(', ')}]`, + expected: arr.map(v => v.expected), + })), + ) + const frontmatterContentArb = fc.dictionary(keyArb, valueArb).map(dict => { + const entries = Object.entries(dict).map(([key, { raw, expected }]) => ({ + key, + raw: `${key}: ${raw}`, + expected, + })) + return { + raw: `---\n${entries.map(e => e.raw).join('\n')}\n---\n`, + expected: Object.fromEntries(entries.map(e => [e.key, e.expected])), + } + }) + fc.assert( + fc.property(frontmatterContentArb, ({ raw, expected }) => { + expect(parseBasicFrontmatter(raw)).toEqual(expected) + }), + { numRuns: 10000, endOnFailure: true }, + ) + }) }) From 151004bf53d4fc77e69e28c935a3806e7f66236d Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Sat, 7 Feb 2026 18:19:04 +0000 Subject: [PATCH 2/3] [autofix.ci] apply automated fixes --- test/unit/shared/utils/parse-basic-frontmatter.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/shared/utils/parse-basic-frontmatter.spec.ts b/test/unit/shared/utils/parse-basic-frontmatter.spec.ts index d30113e2bd..e125400bd1 100644 --- a/test/unit/shared/utils/parse-basic-frontmatter.spec.ts +++ b/test/unit/shared/utils/parse-basic-frontmatter.spec.ts @@ -150,7 +150,7 @@ describe('parseBasicFrontmatter', () => { }) it('should parse back every key-value pair from generated frontmatter', () => { - const keyArb = fc.stringMatching(/^[a-zA-Z][a-zA-Z0-9_]*$/) + const keyArb = fc.stringMatching(/^[a-z][a-z0-9_]*$/i) const singleValueArbs = (inArray: boolean) => // for arrays: all values get parsed as strings and there should not be any comma in the value even for quoted strings [ From 7274106f10d019a0426e6ad219276fe96f45836f Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Sat, 7 Feb 2026 18:20:15 +0000 Subject: [PATCH 3/3] [autofix.ci] apply automated fixes (attempt 2/3) --- test/unit/shared/utils/parse-basic-frontmatter.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/shared/utils/parse-basic-frontmatter.spec.ts b/test/unit/shared/utils/parse-basic-frontmatter.spec.ts index e125400bd1..80914ba73f 100644 --- a/test/unit/shared/utils/parse-basic-frontmatter.spec.ts +++ b/test/unit/shared/utils/parse-basic-frontmatter.spec.ts @@ -150,7 +150,7 @@ describe('parseBasicFrontmatter', () => { }) it('should parse back every key-value pair from generated frontmatter', () => { - const keyArb = fc.stringMatching(/^[a-z][a-z0-9_]*$/i) + const keyArb = fc.stringMatching(/^[a-z]\w*$/i) const singleValueArbs = (inArray: boolean) => // for arrays: all values get parsed as strings and there should not be any comma in the value even for quoted strings [