Files
markdown-converter/tests/goal-tracker.test.js
T
amitwh 7ab5a0ddb4 feat(ai): add Anthropic-compatible provider; fix async plugin backends
Anthropic-compatible provider:
- New 'anthropic-compatible' option for any base URL speaking the Anthropic
  messages schema (LiteLLM proxies, Bedrock gateways, local servers)
- Sends x-api-key AND Bearer auth when a key is set (gateway-friendly,
  harmless for the official API); keyless proxies supported
- Tolerates base URLs with or without a trailing /v1 segment
- Settings modal, manifest, and provider docs updated

Runtime bug fixes found by booting the app (run-to-verify pass):
- PDF editor: File > Open PDF sends operation=null which matched no switch
  case and crashed on getElementById(undefined); now defaults to the merge
  section
- backlinks-panel: wrong require depth (../../utils -> ../utils) threw at
  panel registration time
- writing-studio stack was written against a synchronous settings backend but
  the real one is IPC-backed: GoalTracker/SnapshotManager/ProjectManager and
  all four panels now await; JSON.parse(Promise) crashes eliminated
- manuscript panel used window.prompt (unavailable in Electron); replaced
  with an inline dialog
- collaboration comment-store/save-load made async to match its IPC IO

617/617 tests green; 4 consecutive clean app boots (no uncaught errors)
2026-09-05 21:53:34 +05:30

106 lines
3.5 KiB
JavaScript

const { GoalTracker } = require('../src/plugins/built-in/writing-studio/goal-tracker');
// All GoalTracker methods are async (the real settings backend is the
// IPC-backed SettingsStore; these tests inject a synchronous fake, which
// `await` handles transparently).
describe('GoalTracker', () => {
let tracker;
let store;
beforeEach(() => {
store = {};
tracker = new GoalTracker({
get: (key) => store[key],
set: (key, value) => {
store[key] = value;
},
});
});
test('addWords records words for today', async () => {
await tracker.addWords(500);
const today = new Date().toISOString().split('T')[0];
expect(store['plugins.writing-studio.history']).toBeDefined();
const history = JSON.parse(store['plugins.writing-studio.history']);
expect(history[today].words).toBe(500);
});
test('addWords accumulates across multiple calls', async () => {
await tracker.addWords(300);
await tracker.addWords(200);
const today = new Date().toISOString().split('T')[0];
const history = JSON.parse(store['plugins.writing-studio.history']);
expect(history[today].words).toBe(500);
});
test('works with a Promise-returning (IPC-style) backend', async () => {
const asyncTracker = new GoalTracker({
get: async (key) => store[key],
set: async (key, value) => {
store[key] = value;
},
});
await asyncTracker.addWords(250);
await expect(asyncTracker.getDailyProgress(1000)).resolves.toEqual({
written: 250,
goal: 1000,
pct: 25,
});
});
test('getDailyProgress returns 0 when no history', async () => {
await expect(tracker.getDailyProgress(1000)).resolves.toEqual({
written: 0,
goal: 1000,
pct: 0,
});
});
test('getDailyProgress returns percentage', async () => {
await tracker.addWords(500);
const progress = await tracker.getDailyProgress(1000);
expect(progress.written).toBe(500);
expect(progress.pct).toBe(50);
});
test('getStreak counts consecutive days meeting goal', async () => {
const today = new Date();
for (let i = 0; i < 3; i++) {
const d = new Date(today);
d.setDate(d.getDate() - i);
const key = d.toISOString().split('T')[0];
await tracker._setHistoryDay(key, { words: 1200 });
}
await expect(tracker.getStreak(1000)).resolves.toBe(3);
});
test('getStreak breaks on missed day', async () => {
const today = new Date();
await tracker._setHistoryDay(today.toISOString().split('T')[0], { words: 1200 });
const yesterday = new Date(today);
yesterday.setDate(yesterday.getDate() - 1);
await tracker._setHistoryDay(yesterday.toISOString().split('T')[0], { words: 500 });
const dayBefore = new Date(today);
dayBefore.setDate(dayBefore.getDate() - 2);
await tracker._setHistoryDay(dayBefore.toISOString().split('T')[0], { words: 1200 });
await expect(tracker.getStreak(1000)).resolves.toBe(1);
});
test('getLast30Days returns array of 30 entries', async () => {
await tracker.addWords(100);
const days = await tracker.getLast30Days();
expect(days.length).toBe(30);
expect(days[29].words).toBe(100);
});
test('getWeeklyTotal sums last 7 days', async () => {
const today = new Date();
for (let i = 0; i < 7; i++) {
const d = new Date(today);
d.setDate(d.getDate() - i);
await tracker._setHistoryDay(d.toISOString().split('T')[0], { words: 200 });
}
await expect(tracker.getWeeklyTotal()).resolves.toBe(1400);
});
});