SPB Git

Repository Public

import-bibtex-roundtrip.test.ts 6.5 KB 107 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : Note SPBoucher AI8 * File             : tests/unit/import-bibtex-roundtrip.test.ts9 * Module           : tests10 * Repository       : https://git.spboucher.ai/note-spboucher-ai11 * Project Website  : https://note.spboucher.ai12 * Created          : 2026-11-1713 * Last Modified    : 2026-11-1714 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: contact@spboucher.ai23 * Development      : dev@spboucher.ai24 * Support          : support@spboucher.ai25 * Security/Admin   : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License          : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243/** Pure-logic tests (no database): BibTeX parse → format → parse keeps every field. Run: npx tsx --test tests/unit/import-bibtex-roundtrip.test.ts */44import { test } from "node:test";45import assert from "node:assert/strict";46import { bibFile, fieldsOf, formatBibtex, parseBibtex, parseBibtexDetailed, referenceBibtex } from "../../src/lib/research/bibtex";47import { extractMarkdown, formatCitation, shortLabel } from "../../src/lib/research/cite";4849const SRC = `% a comment line50@string{jfe = "Journal of Financial Economics"}51@article{gorton2019,52  author = {Gorton, Gary and {Bank of Canada} and de la Fontaine, Jean},53  title = {The {B}ayesian Flight to Safety: $\\alpha$ \\& more},54  journal = jfe,55  year = 2019, volume = {12}, number = {3}, pages = {45--67},56  month = jan,57  x-custom = "kept {as} is",58  keywords = {safe assets, crises},59  doi = {https://doi.org/10.1016/J.JFINECO.2019.09.005}60}61@book(knuth84, title = {The TeXbook}, author = "Donald E. Knuth", year = {1984}, publisher = {Addison-Wesley})62`;63const sorted = (o: Record<string, unknown>) => Object.fromEntries(Object.entries(o).sort(([a], [b]) => a.localeCompare(b)));6465test("every field of every entry survives parse → format → parse", () => {66  const first = parseBibtex(SRC);67  assert.equal(first.length, 2);68  const g = first[0];69  assert.equal(g.fields["x-custom"], "kept {as} is"); assert.equal(g.fields.journal, "Journal of Financial Economics"); assert.equal(g.fields.month, "jan");70  assert.equal(g.fields.author, "Gorton, Gary and {Bank of Canada} and de la Fontaine, Jean"); assert.equal(g.fields.title, "The {B}ayesian Flight to Safety: $\\alpha$ \\& more");71  assert.equal(g.doi, "10.1016/j.jfineco.2019.09.005"); assert.deepEqual(g.authors, ["Gary Gorton", "Bank of Canada", "Jean de la Fontaine"]);72  const again = parseBibtex(first.map((e) => formatBibtex({ citeKey: e.citeKey, entryType: e.entryType, fields: e.fields })).join("\n\n"));73  assert.deepEqual(again.map((e) => sorted(e.fields)), first.map((e) => sorted(e.fields)));74  assert.deepEqual(again.map((e) => [e.citeKey, e.entryType]), first.map((e) => [e.citeKey, e.entryType]));75});7677test("the export of stored rows round-trips, with or without a stored entry, and never leaks application data", () => {78  const rows = parseBibtex(SRC).map((p, i) => ({ citeKey: p.citeKey, entryType: p.entryType, title: p.title, authors: p.authors, year: p.year, journal: p.journal, doi: p.doi, url: p.url, bibtex: i === 0 ? null : p.bibtex, fields: { ...p.fields, extracts: [{ id: "1", quote: "q {unbalanced", page: "3", note: null, createdAt: "2026-01-01" }] } }));79  const file = bibFile(rows);80  assert.ok(!file.includes("extracts") && !file.includes("unbalanced"));81  const back = parseBibtexDetailed(file);82  assert.deepEqual(back.errors, []);83  for (const r of rows) { const b = back.entries.find((e) => e.citeKey === r.citeKey)!; const { extracts: _x, ...tags } = r.fields as Record<string, unknown>; assert.deepEqual(sorted(b.fields), sorted(tags)); }84});8586test("stored fields win over the structured columns (no silent rewrite of names or protected capitals)", () => {87  const f = fieldsOf({ title: "The Bayesian Flight", authors: ["Gary Gorton", "Bank of Canada"], year: 2019, journal: "JFE", doi: "10.1/x", url: null, entryType: "article", fields: { author: "Gorton, Gary and {Bank of Canada}", title: "The {B}ayesian Flight", note: "keep me" } });88  assert.equal(f.author, "Gorton, Gary and {Bank of Canada}"); assert.equal(f.title, "The {B}ayesian Flight"); assert.equal(f.note, "keep me");89  assert.equal(f.year, "2019"); assert.equal(f.journal, "JFE"); assert.equal(f.doi, "10.1/x");90  const manual = referenceBibtex({ citeKey: "k", entryType: "book", title: "T", authors: ["Ada Lovelace"], year: 1843, journal: "Taylor", fields: {} });91  assert.match(manual, /author\s+= \{Lovelace, Ada\}/); assert.match(manual, /publisher\s+= \{Taylor\}/);92});9394test("a malformed entry is reported and does not reject the others", () => {95  const r = parseBibtexDetailed(`@article{ok, title = {Fine}, year = {2020}}\n@article{bad, title = {Unbalanced\n`);96  assert.deepEqual(r.entries.map((e) => e.citeKey), ["ok"]); assert.equal(r.errors.length, 1); assert.equal(r.errors[0].citeKey, "bad");97  assert.throws(() => parseBibtex("@article{bad, title = {Unbalanced"));98});99100test("citations", () => {101  const [g] = parseBibtex(SRC);102  assert.equal(shortLabel(g), "Gorton et al. (2019)");103  assert.equal(formatCitation(g), "Gorton, G., Bank of Canada, & de la Fontaine, J. (2019). The Bayesian Flight to Safety: $\\alpha$ \\& more. Journal of Financial Economics, 12(3), 45–67. https://doi.org/10.1016/j.jfineco.2019.09.005");104  assert.equal(extractMarkdown({ citeKey: "gorton2019" }, { quote: "Line one\nline two", page: "12" }), "> Line one\n> line two\n> — (gorton2019, p. 12)");105  assert.equal(extractMarkdown({ citeKey: "k" }, { quote: "Q", page: null }), "> Q\n> — (k)");106});107
Files main
Menu

Theme