230 lines
7.7 KiB
TypeScript
230 lines
7.7 KiB
TypeScript
import { test } from 'node:test';
|
|||
|
|
import { strict as assert } from 'node:assert';
|
||
|
|
import { DatabaseSync } from 'node:sqlite';
|
||
|
|
import { readFileSync } from 'node:fs';
|
||
|
|
import { dirname, join } from 'node:path';
|
||
|
|
import { fileURLToPath } from 'node:url';
|
||
|
|
|
||
|
|
import {
|
||
|
|
addEmotionLog,
|
||
|
|
getEmotionLogsByTrade,
|
||
|
|
deleteEmotionLog,
|
||
|
|
} from '../db/emotionLogRepository.ts';
|
||
|
|
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
// Test helpers
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
|
||
|
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
||
|
|
const SCHEMA_SQL = readFileSync(join(__dirname, '..', 'db', 'schema.sql'), 'utf8');
|
||
|
|
|
||
|
|
/** Create a fresh in-memory DatabaseSync with the full schema ready. */
|
||
|
|
function freshDb(): DatabaseSync {
|
||
|
|
const db = new DatabaseSync(':memory:');
|
||
|
|
db.exec(SCHEMA_SQL);
|
||
|
|
// Disable FK enforcement for tests (schema has FK chain: trades → trade_executions → emotion_logs)
|
||
|
|
db.exec('PRAGMA foreign_keys = OFF;');
|
||
|
|
return db;
|
||
|
|
}
|
||
|
|
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
// Tests — addEmotionLog
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
|
||
|
|
test('addEmotionLog creates a row and returns the created log with id', () => {
|
||
|
|
const db = freshDb();
|
||
|
|
|
||
|
|
// Insert a trade_execution row so the FK constraint is satisfied.
|
||
|
|
db.prepare('INSERT INTO trade_executions (id, trade_id, status, started_at, completed_at, created_at) VALUES (?, ?, ?, ?, ?, ?)')
|
||
|
|
.run('trade_123', 'trade_123', 'completed', new Date().toISOString(), new Date().toISOString(), new Date().toISOString());
|
||
|
|
|
||
|
|
const log = addEmotionLog(db, {
|
||
|
|
tradeExecutionId: 'trade_123',
|
||
|
|
emotion: 'fear',
|
||
|
|
priceAtEvent: 450.5,
|
||
|
|
note: 'Market dropped suddenly',
|
||
|
|
});
|
||
|
|
|
||
|
|
assert.ok(log.id.length > 0);
|
||
|
|
assert.equal(log.tradeExecutionId, 'trade_123');
|
||
|
|
assert.ok(log.timestamp > 0);
|
||
|
|
assert.equal(log.priceAtEvent, 450.5);
|
||
|
|
assert.equal(log.emotion, 'fear');
|
||
|
|
assert.equal(log.note, 'Market dropped suddenly');
|
||
|
|
assert.ok(log.createdAt.length > 0);
|
||
|
|
|
||
|
|
db.close();
|
||
|
|
});
|
||
|
|
|
||
|
|
test('addEmotionLog without priceAtEvent sets it to undefined', () => {
|
||
|
|
const db = freshDb();
|
||
|
|
db.prepare('INSERT INTO trade_executions (id, trade_id, status, started_at, completed_at, created_at) VALUES (?, ?, ?, ?, ?, ?)')
|
||
|
|
.run('trade_456', 'trade_456', 'completed', new Date().toISOString(), new Date().toISOString(), new Date().toISOString());
|
||
|
|
|
||
|
|
const log = addEmotionLog(db, {
|
||
|
|
tradeExecutionId: 'trade_456',
|
||
|
|
emotion: 'greed',
|
||
|
|
});
|
||
|
|
|
||
|
|
assert.equal(log.priceAtEvent, undefined);
|
||
|
|
assert.equal(log.note, undefined);
|
||
|
|
|
||
|
|
db.close();
|
||
|
|
});
|
||
|
|
|
||
|
|
test('addEmotionLog without note sets it to undefined', () => {
|
||
|
|
const db = freshDb();
|
||
|
|
db.prepare('INSERT INTO trade_executions (id, trade_id, status, started_at, completed_at, created_at) VALUES (?, ?, ?, ?, ?, ?)')
|
||
|
|
.run('trade_789', 'trade_789', 'completed', new Date().toISOString(), new Date().toISOString(), new Date().toISOString());
|
||
|
|
|
||
|
|
const log = addEmotionLog(db, {
|
||
|
|
tradeExecutionId: 'trade_789',
|
||
|
|
emotion: 'FOMO',
|
||
|
|
priceAtEvent: 200.0,
|
||
|
|
});
|
||
|
|
|
||
|
|
assert.equal(log.note, undefined);
|
||
|
|
|
||
|
|
db.close();
|
||
|
|
});
|
||
|
|
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
// Tests — getEmotionLogsByTrade
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
|
||
|
|
test('getEmotionLogsByTrade returns logs filtered by tradeExecutionId', () => {
|
||
|
|
const db = freshDb();
|
||
|
|
db.prepare('INSERT INTO trade_executions (id, trade_id, status, started_at, completed_at, created_at) VALUES (?, ?, ?, ?, ?, ?)')
|
||
|
|
.run('trade_123', 'trade_123', 'completed', new Date().toISOString(), new Date().toISOString(), new Date().toISOString());
|
||
|
|
db.prepare('INSERT INTO trade_executions (id, trade_id, status, started_at, completed_at, created_at) VALUES (?, ?, ?, ?, ?, ?)')
|
||
|
|
.run('trade_456', 'trade_456', 'completed', new Date().toISOString(), new Date().toISOString(), new Date().toISOString());
|
||
|
|
|
||
|
|
addEmotionLog(db, {
|
||
|
|
tradeExecutionId: 'trade_123',
|
||
|
|
emotion: 'fear',
|
||
|
|
priceAtEvent: 450.0,
|
||
|
|
});
|
||
|
|
addEmotionLog(db, {
|
||
|
|
tradeExecutionId: 'trade_123',
|
||
|
|
emotion: 'greed',
|
||
|
|
priceAtEvent: 460.0,
|
||
|
|
});
|
||
|
|
addEmotionLog(db, {
|
||
|
|
tradeExecutionId: 'trade_456',
|
||
|
|
emotion: 'regret',
|
||
|
|
priceAtEvent: 470.0,
|
||
|
|
});
|
||
|
|
|
||
|
|
const logs123 = getEmotionLogsByTrade(db, 'trade_123');
|
||
|
|
assert.equal(logs123.length, 2);
|
||
|
|
assert.equal(logs123[0].emotion, 'fear');
|
||
|
|
assert.equal(logs123[1].emotion, 'greed');
|
||
|
|
|
||
|
|
const logs456 = getEmotionLogsByTrade(db, 'trade_456');
|
||
|
|
assert.equal(logs456.length, 1);
|
||
|
|
assert.equal(logs456[0].emotion, 'regret');
|
||
|
|
|
||
|
|
db.close();
|
||
|
|
});
|
||
|
|
|
||
|
|
test('getEmotionLogsByTrade returns empty array for unknown trade', () => {
|
||
|
|
const db = freshDb();
|
||
|
|
|
||
|
|
const logs = getEmotionLogsByTrade(db, 'nonexistent_trade');
|
||
|
|
assert.equal(logs.length, 0);
|
||
|
|
|
||
|
|
db.close();
|
||
|
|
});
|
||
|
|
|
||
|
|
test('getEmotionLogsByTrade returns logs ordered by timestamp ASC', () => {
|
||
|
|
const db = freshDb();
|
||
|
|
db.prepare('INSERT INTO trade_executions (id, trade_id, status, started_at, completed_at, created_at) VALUES (?, ?, ?, ?, ?, ?)')
|
||
|
|
.run('trade_123', 'trade_123', 'completed', new Date().toISOString(), new Date().toISOString(), new Date().toISOString());
|
||
|
|
|
||
|
|
// Add in reverse order — the last one should appear first after sort.
|
||
|
|
addEmotionLog(db, {
|
||
|
|
tradeExecutionId: 'trade_123',
|
||
|
|
emotion: 'greed',
|
||
|
|
priceAtEvent: 460.0,
|
||
|
|
});
|
||
|
|
addEmotionLog(db, {
|
||
|
|
tradeExecutionId: 'trade_123',
|
||
|
|
emotion: 'fear',
|
||
|
|
priceAtEvent: 450.0,
|
||
|
|
});
|
||
|
|
|
||
|
|
const logs = getEmotionLogsByTrade(db, 'trade_123');
|
||
|
|
assert.equal(logs.length, 2);
|
||
|
|
// First inserted should be first in the array (ORDER BY timestamp ASC).
|
||
|
|
assert.equal(logs[0].emotion, 'greed');
|
||
|
|
assert.ok(logs[0].timestamp <= logs[1].timestamp);
|
||
|
|
|
||
|
|
db.close();
|
||
|
|
});
|
||
|
|
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
// Tests — deleteEmotionLog
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
|
||
|
|
test('deleteEmotionLog removes the row', () => {
|
||
|
|
const db = freshDb();
|
||
|
|
db.prepare('INSERT INTO trade_executions (id, trade_id, status, started_at, completed_at, created_at) VALUES (?, ?, ?, ?, ?, ?)')
|
||
|
|
.run('trade_123', 'trade_123', 'completed', new Date().toISOString(), new Date().toISOString(), new Date().toISOString());
|
||
|
|
|
||
|
|
const log = addEmotionLog(db, {
|
||
|
|
tradeExecutionId: 'trade_123',
|
||
|
|
emotion: 'fear',
|
||
|
|
priceAtEvent: 450.0,
|
||
|
|
});
|
||
|
|
|
||
|
|
deleteEmotionLog(db, log.id);
|
||
|
|
|
||
|
|
const logs = getEmotionLogsByTrade(db, 'trade_123');
|
||
|
|
assert.equal(logs.length, 0);
|
||
|
|
|
||
|
|
db.close();
|
||
|
|
});
|
||
|
|
|
||
|
|
test('deleteEmotionLog is a no-op when id does not exist', () => {
|
||
|
|
const db = freshDb();
|
||
|
|
|
||
|
|
// Should not throw.
|
||
|
|
deleteEmotionLog(db, 'nonexistent_id');
|
||
|
|
|
||
|
|
db.close();
|
||
|
|
});
|
||
|
|
|
||
|
|
test('full lifecycle: add, query, delete', () => {
|
||
|
|
const db = freshDb();
|
||
|
|
db.prepare('INSERT INTO trade_executions (id, trade_id, status, started_at, completed_at, created_at) VALUES (?, ?, ?, ?, ?, ?)')
|
||
|
|
.run('trade_999', 'trade_999', 'completed', new Date().toISOString(), new Date().toISOString(), new Date().toISOString());
|
||
|
|
|
||
|
|
// Add two logs for the same trade.
|
||
|
|
const log1 = addEmotionLog(db, {
|
||
|
|
tradeExecutionId: 'trade_999',
|
||
|
|
emotion: 'fear',
|
||
|
|
priceAtEvent: 100.0,
|
||
|
|
note: 'Dip buying',
|
||
|
|
});
|
||
|
|
const log2 = addEmotionLog(db, {
|
||
|
|
tradeExecutionId: 'trade_999',
|
||
|
|
emotion: 'greed',
|
||
|
|
priceAtEvent: 110.0,
|
||
|
|
note: 'Panic selling',
|
||
|
|
});
|
||
|
|
|
||
|
|
// Query — both should appear.
|
||
|
|
const logs = getEmotionLogsByTrade(db, 'trade_999');
|
||
|
|
assert.equal(logs.length, 2);
|
||
|
|
|
||
|
|
// Delete one.
|
||
|
|
deleteEmotionLog(db, log1.id);
|
||
|
|
|
||
|
|
// Only one remains.
|
||
|
|
const remaining = getEmotionLogsByTrade(db, 'trade_999');
|
||
|
|
assert.equal(remaining.length, 1);
|
||
|
|
assert.equal(remaining[0].id, log2.id);
|
||
|
|
assert.equal(remaining[0].emotion, 'greed');
|
||
|
|
|
||
|
|
db.close();
|
||
|
|
});
|