split some function in the store

This commit is contained in:
pa
2026-03-04 21:31:11 +09:00
parent 1f1b996239
commit 4a72d77a96
6 changed files with 300 additions and 279 deletions

View File

@@ -1,4 +1,8 @@
import { gameLogSearchFilter } from '../gameLog';
import {
compareGameLogRows,
gameLogSearchFilter,
getGameLogCreatedAtTs
} from '../gameLog';
describe('gameLogSearchFilter', () => {
test('returns true for empty search query', () => {
@@ -96,3 +100,87 @@ describe('gameLogSearchFilter', () => {
expect(gameLogSearchFilter(row, 'anything')).toBe(true);
});
});
describe('getGameLogCreatedAtTs', () => {
test('returns millisecond timestamp from millis number', () => {
expect(getGameLogCreatedAtTs({ created_at: 1700000000000 })).toBe(
1700000000000
);
});
test('converts seconds to millis for small numbers', () => {
expect(getGameLogCreatedAtTs({ created_at: 1700000000 })).toBe(
1700000000000
);
});
test('parses ISO string via Date.parse', () => {
const ts = getGameLogCreatedAtTs({
created_at: '2024-01-15T12:00:00Z'
});
expect(ts).toBe(Date.parse('2024-01-15T12:00:00Z'));
});
test('supports createdAt alias', () => {
expect(getGameLogCreatedAtTs({ createdAt: 1700000000000 })).toBe(
1700000000000
);
});
test('supports dt alias', () => {
expect(getGameLogCreatedAtTs({ dt: 1700000000000 })).toBe(
1700000000000
);
});
test('returns 0 for null/undefined row', () => {
expect(getGameLogCreatedAtTs(null)).toBe(0);
expect(getGameLogCreatedAtTs(undefined)).toBe(0);
});
test('returns 0 for missing timestamp fields', () => {
expect(getGameLogCreatedAtTs({})).toBe(0);
});
test('returns 0 for unparseable string', () => {
expect(getGameLogCreatedAtTs({ created_at: 'not-a-date' })).toBe(0);
});
test('returns 0 for NaN number', () => {
expect(getGameLogCreatedAtTs({ created_at: NaN })).toBe(0);
});
});
describe('compareGameLogRows', () => {
test('sorts by timestamp descending (newest first)', () => {
const a = { created_at: 2000 };
const b = { created_at: 1000 };
expect(compareGameLogRows(a, b)).toBeLessThan(0);
expect(compareGameLogRows(b, a)).toBeGreaterThan(0);
});
test('equal timestamps → sorts by rowId descending', () => {
const a = { created_at: 1000, rowId: 10 };
const b = { created_at: 1000, rowId: 5 };
expect(compareGameLogRows(a, b)).toBeLessThan(0);
expect(compareGameLogRows(b, a)).toBeGreaterThan(0);
});
test('equal timestamp and rowId → sorts by uid reverse-lex', () => {
const a = { created_at: 1000, rowId: 1, uid: 'bbb' };
const b = { created_at: 1000, rowId: 1, uid: 'aaa' };
expect(compareGameLogRows(a, b)).toBeLessThan(0);
expect(compareGameLogRows(b, a)).toBeGreaterThan(0);
});
test('returns 0 for identical rows', () => {
const row = { created_at: 1000, rowId: 1, uid: 'aaa' };
expect(compareGameLogRows(row, row)).toBe(0);
});
test('handles missing fields gracefully', () => {
const a = {};
const b = {};
expect(compareGameLogRows(a, b)).toBe(0);
});
});