Skip to content

Commit f2400a0

Browse files
committed
test(opencode): refine date filter coverage
1 parent 95329b9 commit f2400a0

3 files changed

Lines changed: 63 additions & 17 deletions

File tree

‎apps/opencode/src/_shared-args.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ function parseDateArg(value: string): string {
1111
return v.parse(filterDateSchema, value);
1212
}
1313

14+
/**
15+
* Shared date-range CLI flags for OpenCode reports.
16+
*
17+
* Both `since` and `until` accept `YYYYMMDD` strings and use `parseDateArg`
18+
* for the same format validation before satisfying Gunshi's `Args` shape.
19+
*/
1420
export const dateFilterArgs = {
1521
since: {
1622
type: 'custom',
@@ -25,3 +31,25 @@ export const dateFilterArgs = {
2531
parse: parseDateArg,
2632
},
2733
} as const satisfies Args;
34+
35+
if (import.meta.vitest != null) {
36+
const { describe, expect, it } = import.meta.vitest;
37+
38+
describe('dateFilterArgs', () => {
39+
it('should parse main-mode compatible since and until args', () => {
40+
expect(dateFilterArgs.since.short).toBe('s');
41+
expect(dateFilterArgs.since.description).toBe('Filter from date (YYYYMMDD format)');
42+
expect(dateFilterArgs.since.parse('20240102')).toBe('20240102');
43+
expect(() => dateFilterArgs.since.parse('2024-01-02')).toThrow(
44+
'Date must be in YYYYMMDD format',
45+
);
46+
47+
expect(dateFilterArgs.until.short).toBe('u');
48+
expect(dateFilterArgs.until.description).toBe('Filter until date (YYYYMMDD format)');
49+
expect(dateFilterArgs.until.parse('20240103')).toBe('20240103');
50+
expect(() => dateFilterArgs.until.parse('2024-01-03')).toThrow(
51+
'Date must be in YYYYMMDD format',
52+
);
53+
});
54+
});
55+
}
Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1+
import { dateFilterArgs } from '../_shared-args.ts';
12
import { dailyCommand } from './daily.ts';
23
import { monthlyCommand } from './monthly.ts';
34
import { sessionCommand } from './session.ts';
45
import { weeklyCommand } from './weekly.ts';
56

6-
export { dailyCommand } from './daily.ts';
7-
export { monthlyCommand } from './monthly.ts';
8-
export { sessionCommand } from './session.ts';
9-
export { weeklyCommand } from './weekly.ts';
7+
export { dailyCommand, monthlyCommand, sessionCommand, weeklyCommand };
108

119
if (import.meta.vitest != null) {
1210
const { describe, expect, it } = import.meta.vitest;
1311

1412
type DateArg = {
15-
short: string;
16-
description: string;
17-
parse: (value: string) => string;
13+
readonly short: string;
14+
readonly description: string;
15+
readonly parse: (value: string) => string;
1816
};
1917
type CommandWithDateArgs = {
2018
args: {
@@ -29,18 +27,11 @@ if (import.meta.vitest != null) {
2927
['weekly', weeklyCommand],
3028
['monthly', monthlyCommand],
3129
['session', sessionCommand],
32-
])('should expose main-mode compatible since and until args for %s', (_name, command) => {
30+
])('should expose shared since and until args for %s', (_name, command) => {
3331
const args = (command as CommandWithDateArgs).args;
3432

35-
expect(args.since?.short).toBe('s');
36-
expect(args.since?.description).toBe('Filter from date (YYYYMMDD format)');
37-
expect(args.since?.parse('20240102')).toBe('20240102');
38-
expect(() => args.since?.parse('2024-01-02')).toThrow('Date must be in YYYYMMDD format');
39-
40-
expect(args.until?.short).toBe('u');
41-
expect(args.until?.description).toBe('Filter until date (YYYYMMDD format)');
42-
expect(args.until?.parse('20240103')).toBe('20240103');
43-
expect(() => args.until?.parse('2024-01-03')).toThrow('Date must be in YYYYMMDD format');
33+
expect(args.since).toBe(dateFilterArgs.since);
34+
expect(args.until).toBe(dateFilterArgs.until);
4435
});
4536
});
4637
}

‎apps/opencode/src/data-loader.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ export type LoadedSessionMetadata = {
123123
directory: string;
124124
};
125125

126+
/**
127+
* Date range options for loading OpenCode messages.
128+
*
129+
* `since` and `until` use `YYYYMMDD` strings so the loader can apply the same
130+
* lexicographic date comparison semantics as the main CLI.
131+
*/
126132
export type LoadOpenCodeMessagesOptions = {
127133
since?: string;
128134
until?: string;
@@ -499,5 +505,26 @@ if (import.meta.vitest != null) {
499505
await rm(root, { recursive: true, force: true });
500506
}
501507
});
508+
509+
it('should return no OpenCode messages for non-overlapping date ranges', async () => {
510+
const root = await createOpenCodeMessageFixture([
511+
createTestMessage('msg_20240101', '2024-01-01T12:00:00.000Z'),
512+
createTestMessage('msg_20240102', '2024-01-02T12:00:00.000Z'),
513+
createTestMessage('msg_20240103', '2024-01-03T12:00:00.000Z'),
514+
]);
515+
516+
try {
517+
vi.stubEnv(OPENCODE_CONFIG_DIR_ENV, root);
518+
519+
const entries = await loadOpenCodeMessages({
520+
since: '20240104',
521+
until: '20240105',
522+
});
523+
524+
expect(getSortedIsoTimestamps(entries)).toEqual([]);
525+
} finally {
526+
await rm(root, { recursive: true, force: true });
527+
}
528+
});
502529
});
503530
}

0 commit comments

Comments
 (0)