Debian Codegraph @

frontend/test/entries.test.ts

raw
1 import { test } from "uvu";
2 import assert from "uvu/assert";
3
4 import {
5 Amount,
6 Balance,
7 Cost,
8 Document,
9 EntryMetadata,
10 entryValidator,
11 Note,
12 Posting,
13 Transaction,
14 } from "../src/entries";
15 import { Position } from "../src/entries/position";
16 import { formatter_context } from "../src/format";
17
18 test("metadata: basic operations", () => {
19 const meta = new EntryMetadata({ key: "value" });
20 assert.not(meta.is_empty());
21 assert.equal(meta.toJSON(), { key: "value" });
22 assert.equal(meta.get("key"), "value");
23 assert.equal(meta.get("nokey"), undefined);
24 assert.equal(meta.entries(), [["key", "value"]]);
25
26 const meta_empty = meta.delete("key");
27 assert.ok(meta_empty.is_empty());
28
29 const meta_added = meta.add();
30 assert.equal(meta_added.entries(), [
31 ["key", "value"],
32 ["", ""],
33 ]);
34
35 const meta_updated = meta.update_key("key", "newkey");
36 assert.equal(meta_updated.entries(), [["newkey", "value"]]);
37
38 const meta_added_updated = meta_added.update_key("key", "newkey");
39 assert.equal(meta_added_updated.entries(), [
40 ["newkey", "value"],
41 ["", ""],
42 ]);
43 });
44
45 test("metadata: validate JSON with fallback", () => {
46 const meta = EntryMetadata.validator({
47 boolean: true,
48 false: false,
49 key: "value",
50 number: 10,
51 unsupported: [],
52 }).unwrap();
53 assert.equal(meta.entries(), [
54 ["boolean", "TRUE"],
55 ["false", "FALSE"],
56 ["key", "value"],
57 ["number", "10"],
58 ["unsupported", "Unsupported metadata value"],
59 ]);
60 });
61
62 test("metadata: entries as strings", () => {
63 const meta = new EntryMetadata({
64 boolean: true,
65 false: false,
66 key: "value",
67 number: 10,
68 });
69 assert.equal(meta.entries(), [
70 ["boolean", "TRUE"],
71 ["false", "FALSE"],
72 ["key", "value"],
73 ["number", "10"],
74 ]);
75 });
76
77 test("metadata: set from string", () => {
78 const meta = new EntryMetadata();
79 assert.equal(meta.set_string("key", "TRUE").get("key"), true);
80 assert.equal(meta.set_string("key", "true").get("key"), "true");
81 assert.equal(meta.set_string("key", "string").get("key"), "string");
82 assert.equal(meta.set_string("key", "FALSE").get("key"), false);
83 });
84
85 test("metadata: get filename and lineno", () => {
86 const meta = new EntryMetadata();
87 const meta_with_position = new EntryMetadata({
88 filename: "/path",
89 lineno: 100,
90 });
91 assert.equal(meta_with_position.filename, "/path");
92 assert.equal(meta_with_position.lineno, "100");
93 assert.equal(meta.filename, "");
94 assert.equal(meta.lineno, "");
95 });
96
97 test("postings", () => {
98 const posting = Posting.empty();
99 assert.instance(posting, Posting);
100 assert.ok(posting.is_empty());
101
102 assert.equal(posting.account, "");
103 const modified = posting.set("account", "Assets");
104 assert.equal(posting.account, "");
105 assert.equal(modified.account, "Assets");
106 });
107
108 test("create empty entries on a date", () => {
109 assert.instance(Balance.empty("2012-12-12"), Balance);
110 assert.instance(Note.empty("2012-12-12"), Note);
111 assert.instance(Transaction.empty("2012-12-12"), Transaction);
112 });
113
114 test("create amounts, cost, and positions from JSON data", () => {
115 const amt = Amount.validator({ number: 10, currency: "EUR" }).unwrap();
116 assert.is(amt.number, 10);
117 assert.instance(amt, Amount);
118
119 const cost = Cost.validator({
120 number: 10,
121 currency: "EUR",
122 date: "2012-12-12",
123 label: "a label",
124 }).unwrap();
125 assert.is(cost.number, 10);
126 assert.instance(cost, Cost);
127
128 const position = Position.validator({
129 units: { number: 10, currency: "EUR" },
130 }).unwrap();
131 assert.is(position.units.number, 10);
132 assert.is(position.cost, null);
133 assert.instance(position, Position);
134
135 const ctx = formatter_context(false, null, {});
136 assert.equal(amt.str(ctx), "10.00 EUR");
137 assert.equal(cost.str(ctx), '10.00 EUR, 2012-12-12, "a label"');
138 });
139
140 test("entries: create changed copies", () => {
141 const balance = Balance.empty("2012-12-12");
142 assert.ok(balance instanceof Balance);
143 const balance_changed = balance.set("account", "Assets:Cash");
144 assert.equal(balance_changed.account, "Assets:Cash");
145 assert.equal(balance.account, "");
146
147 const balance_with_meta = balance.set_meta("key", "value");
148 assert.equal(balance.meta.get("key"), undefined);
149 assert.equal(balance_with_meta.meta.get("key"), "value");
150
151 assert.not(balance.is_duplicate());
152 assert.ok(balance.set_meta("__duplicate__", true).is_duplicate());
153 assert.ok(balance.set_meta("__duplicate__", "asdfasdf").is_duplicate());
154 });
155
156 test("create entries from JSON data", () => {
157 const balance = entryValidator({
158 t: "Balance",
159 meta: { filename: "/home/test/test.beancount", lineno: 1 },
160 date: "2022-12-12",
161 account: "Expenses:Food",
162 amount: { number: "10", currency: "USD" },
163 }).unwrap();
164 assert.instance(balance, Balance);
165
166 const doc = entryValidator({
167 t: "Document",
168 meta: { filename: "/home/test/test.beancount", lineno: 1 },
169 date: "2022-12-12",
170 account: "Expenses:Food",
171 filename: "/tmp/some/path",
172 }).unwrap();
173 assert.instance(doc, Document);
174
175 const note = entryValidator({
176 t: "Note",
177 meta: { filename: "/home/test/test.beancount", lineno: 1 },
178 date: "2022-12-12",
179 account: "Expenses:Food",
180 comment: "Some note for the expenses account",
181 }).unwrap();
182 assert.instance(note, Note);
183
184 const raw = {
185 t: "Transaction",
186 meta: { filename: "/home/test/test.beancount", lineno: 1 },
187 date: "2022-12-12",
188 flag: "*",
189 payee: "Some Store",
190 narration: "Bought food",
191 tags: [],
192 links: [],
193 postings: [
194 { account: "Expenses:Food", amount: "", meta: {} },
195 { account: "Assets:Cash", amount: "-5.15 EUR", meta: {} },
196 { account: "Assets:Cash", amount: "-5.15 EUR", meta: {} },
197 ],
198 };
199 const transaction = entryValidator(raw).unwrap();
200 assert.ok(transaction instanceof Transaction);
201 const json: unknown = JSON.parse(JSON.stringify(transaction));
202 assert.equal(Object.keys(raw), Object.keys(json as Record<string, unknown>));
203 assert.equal(raw, json);
204
205 const raw_with_null_meta_postings = {
206 t: "Transaction",
207 meta: { filename: "/home/test/test.beancount", lineno: 1 },
208 date: "2022-12-12",
209 flag: "*",
210 payee: "Some Store",
211 narration: "Bought food",
212 tags: [],
213 links: [],
214 postings: [
215 { account: "Expenses:Food", amount: "", meta: {} },
216 { account: "Assets:Cash", amount: "-5.15 EUR", meta: null },
217 { account: "Assets:Cash", amount: "-5.15 EUR" },
218 ],
219 };
220 const transaction_with_null_meta = entryValidator(
221 raw_with_null_meta_postings,
222 ).unwrap();
223 assert.ok(transaction_with_null_meta instanceof Transaction);
224 });
225
226 test("transaction: combine narration, tags, and links", () => {
227 const txn = Transaction.empty("2022-12-12");
228 assert.equal(txn.get_narration_tags_links(), "");
229
230 const basic_narration = txn.set("narration", "a narration");
231 assert.equal(basic_narration.get_narration_tags_links(), "a narration");
232 const narration_and_tag = basic_narration.set("tags", ["tag"]);
233 assert.equal(
234 narration_and_tag.get_narration_tags_links(),
235 "a narration #tag",
236 );
237 const narration_tag_and_link = narration_and_tag.set("links", ["link"]);
238 assert.equal(
239 narration_tag_and_link.get_narration_tags_links(),
240 "a narration #tag ^link",
241 );
242
243 const parsed = txn.set_narration_tags_links("a narration #tag ^link");
244 assert.equal(parsed.narration, "a narration");
245 assert.equal(parsed.tags, ["tag"]);
246 assert.equal(parsed.links, ["link"]);
247 });
248
249 test.run();