Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "accounts",
"version": "0.1.87",
"version": "0.1.88",
"private": true,
"description": "Own viewer of your accounts (Fidelity first): a Tampermonkey script reads your positions (read only) into your Google Sheet",
"source": "src/viewer/index.html",
Expand Down
26 changes: 25 additions & 1 deletion src/shared/adp/mykplan.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// <reference types="bun" />
import { expect, test } from 'bun:test';
import { AUTH_WAIT, NoPriceDay, SiteTrouble, adpFlows, errorText, noPriceDay, adpHoldings, adpIn, adpSeries, parseWindow, adpStoreFromRows, adpTotals, FIRST_MONTH, SessionEnded, earliestDay, hiddenFields, historyFields, isoDay, kindOf, lastPriceDay, num, parseAdpStore, parseHistory, readAdpHistory, replaceFrom, startOf, usDay, type AdpStore, type AdpTxn } from './mykplan';
import { AUTH_WAIT, NoPriceDay, SiteTrouble, adpFlows, errorText, noPriceDay, priced, adpHoldings, adpIn, adpSeries, parseWindow, adpStoreFromRows, adpTotals, FIRST_MONTH, SessionEnded, earliestDay, hiddenFields, historyFields, isoDay, kindOf, lastPriceDay, num, parseAdpStore, parseHistory, readAdpHistory, replaceFrom, startOf, usDay, type AdpStore, type AdpTxn } from './mykplan';

// made-up funds and amounts in the page's shape: 12 cells a row, the fund on the first row of its block
const row = (fund: string, date: string, pay: string, type: string, units: string, price: string, amount: string) =>
Expand Down Expand Up @@ -178,6 +178,30 @@ test("mykplan's own trouble page (GlobalError.aspx) is asked once more, then the
expect(n).toBe(2);
});

test('a day mykplan has not priced yet (units, price 0, $0) is not a balance, a fund sold out is', async () => {
expect(priced({ quantity: 10, price: 0 })).toBe(false);
expect(priced({ quantity: 10, price: 13.08 })).toBe(true);
expect(priced({ quantity: 0, price: 0 })).toBe(true);
// the page: the 12/2 window ends with the units and no price, the Market Value Change takes the whole balance
const DEC = page([
row('DEMO INDEX FUND', '12/1/2026', '', 'Beginning Balance', '100.000', '20.0000', '$2,000.00'),
row('', '12/1/2026', '11/27/2026', 'Contribution - Employee Roth 401k', '2.500', '20.0000', '$50.00'),
row('', '12/2/2026', '', 'Market Value Change', '0.0000', '0.000000', '($2,050.00)'),
row('', '12/2/2026', '', 'Ending Balance', '102.500', '0.000000', '$0.00'),
]);
expect(parseWindow(DEC)!.ends).toEqual([]);
expect(parseWindow(DEC)!.rows.map((r) => r.type)).toEqual(['Contribution - Employee Roth 401k']);
// the sheet and the cached store written before this fix: the line keeps its last priced day
const nov = { date: '2026-11-30', fund: 'F', quantity: 100, price: 20, value: 2000 };
const bad = { date: '2026-12-02', fund: 'F', quantity: 102.5, price: 0, value: 0 };
const rows = parseHistory(DEC)!;
const fromSheet = adpStoreFromRows(rows, 'at', [nov, bad])!;
expect(fromSheet.balances).toEqual([nov]);
expect(adpSeries(fromSheet).slice(-1)).toEqual([{ date: '2026-11-30', value: 2000 }]);
expect(adpHoldings(fromSheet)).toEqual([nov]);
expect(parseAdpStore({ version: 1, at: 'x', through: '2026-12-02', rows, balances: [nov, bad] })!.balances).toEqual([nov]);
});

test('the plan value starts at $0 the day before the first money came in', () => {
const r = (date: string, kind: AdpTxn['kind']): AdpTxn => ({ date, payDate: '', fund: 'F', type: '', kind, source: '', quantity: 1, price: 1, net: 100 });
const store: AdpStore = { version: 1, at: 'x', through: '2023-08-31', rows: [r('2023-07-18', 'contribution'), r('2023-07-05', 'contribution')], balances: [{ date: '2023-07-31', fund: 'F', quantity: 2, price: 100.5, value: 201 }] };
Expand Down
15 changes: 12 additions & 3 deletions src/shared/adp/mykplan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ export interface AdpTxn {
}
/** the balance of a fund at the end of a window read (a month, or the last price day for the open month) */
export interface AdpBalance { date: string; fund: string; quantity: number; price: number; value: number }
/**
* A balance with units but no price is a day mykplan has not priced yet, not a balance (owner 2026-12-03: its statement
* for 12/1..12/2/2026 gave the fund a price of 0.000000 with all the units still there, a Market Value Change of minus
* the whole balance and an Ending Balance of $0.00; the 401k chart fell to $0 and Fund allocation went empty). Such a
* balance is left out wherever balances come in: the page, the sheet, the cached store. A fund sold out (no units, $0)
* stays a real balance
*/
export const priced = (b: { quantity: number; price: number }): boolean => !(b.quantity > 0 && !(b.price > 0));
/** `through`: the last day read; `at`: when; `balances`: the end of every month read, per fund (since 0.1.51) */
export interface AdpStore { version: 1; at: string; through: string; rows: AdpTxn[]; balances?: AdpBalance[] }

Expand Down Expand Up @@ -109,7 +117,8 @@ export function parseWindow(html: string): { rows: AdpTxn[]; ends: Array<{ fund:
const v = num(c[10] ?? '');
const q = num(c[8] ?? '');
const pr = num(c[9] ?? '');
if (Number.isFinite(v)) ends.push({ fund, quantity: Number.isFinite(q) ? q : 0, price: Number.isFinite(pr) ? pr : 0, value: v });
const end = { fund, quantity: Number.isFinite(q) ? q : 0, price: Number.isFinite(pr) ? pr : 0, value: v };
if (Number.isFinite(v) && priced(end)) ends.push(end);
return;
}
const k = kindOf(c[7] ?? '');
Expand Down Expand Up @@ -255,7 +264,7 @@ export function adpStoreFromRows(read: Array<Partial<AdpTxn>>, at: string, balan
const rows: AdpTxn[] = read.filter((r) => !!r.date).map(cleanRow);
if (!rows.length) return null;
const sorted = rows.slice().sort((a, b) => (a.date < b.date ? -1 : a.date > b.date ? 1 : 0));
const bal = balances.filter((b) => !!b.date && typeof b.value === 'number').map((b) => ({ date: b.date!, fund: b.fund ?? '', quantity: b.quantity ?? 0, price: b.price ?? 0, value: b.value! })).sort((x, y) => (x.date < y.date ? -1 : x.date > y.date ? 1 : 0));
const bal = balances.filter((b) => !!b.date && typeof b.value === 'number').map((b) => ({ date: b.date!, fund: b.fund ?? '', quantity: b.quantity ?? 0, price: b.price ?? 0, value: b.value! })).filter(priced).sort((x, y) => (x.date < y.date ? -1 : x.date > y.date ? 1 : 0));
const last = [sorted[sorted.length - 1]!.date, bal[bal.length - 1]?.date ?? ''].sort().pop()!;
return { version: 1, at, through: last, rows: sorted, balances: bal };
}
Expand Down Expand Up @@ -321,7 +330,7 @@ export function parseAdpStore(json: unknown): AdpStore | null {
try {
const s = typeof json === 'string' ? JSON.parse(json) : json;
// a copy cached before the rows were cleaned (0.1.46 viewer) may hold missing fields: cleaned here too
return s && s.version === 1 && Array.isArray(s.rows) && typeof s.through === 'string' ? { ...(s as AdpStore), rows: (s.rows as Array<Partial<AdpTxn>>).filter((r) => !!r?.date).map(cleanRow) } : null;
return s && s.version === 1 && Array.isArray(s.rows) && typeof s.through === 'string' ? { ...(s as AdpStore), rows: (s.rows as Array<Partial<AdpTxn>>).filter((r) => !!r?.date).map(cleanRow), ...(Array.isArray(s.balances) ? { balances: (s.balances as AdpBalance[]).filter((b) => !!b && priced(b)) } : {}) } : null;
} catch {
return null;
}
Expand Down