fix hitung karung page cycle selection showing start date - date after start date

This commit is contained in:
Alberto-Audrix committed 2026-10-13 11:51:05 +07:00
1 parent fbf1b6261f
commit 3e1b38697d
2 files changed
+223 -176

No files matched your search

+7 -8
View File
@@ -3,13 +3,7 @@ import { api, errorMessage } from '../services/apiClient.ts';
import type { Cycle, Karung, ManualInput } from '../types/api.ts';
import { useFarm } from '../context/FarmContext.tsx';
import { Banner, EmptyFarm, Spinner } from './ui/Feedback.tsx';
import {
addDaysIso,
cycleDayForDate,
formatDateId,
resolveCycleStatus,
todayIso,
} from '../utils/format.ts';
import { cycleDayForDate, formatDateId, resolveCycleStatus, todayIso } from '../utils/format.ts';
import { throughVisibleDate } from '../utils/visibility.ts';
import { buildFeedSackInsightContext } from '../utils/insightContextBuilders.ts';
import FeedStats from './feedSack/FeedStats.tsx';
@@ -142,7 +136,12 @@ const FeedSackCountingPage: React.FC<{ onSettings: () => void }> = ({ onSettings
const formatCycleDateRange = (cycle: Cycle) => {
const start = formatDateId(cycle.start_date);
if (cycle.end_date) return `${start} - ${formatDateId(cycle.end_date)}`;
return `${start} - ${formatDateId(addDaysIso(cycle.start_date, cycle.total_days))}`;
if (cycle.proposed_end_date && resolveCycleStatus(cycle) === 'pending_close') {
return `${start} - ${formatDateId(cycle.proposed_end_date)}`;
}
const bound = cycle.visible_through_date || cycle.soft_end_date || todayIso();
const effectiveEnd = bound < cycle.start_date ? cycle.start_date : bound;
return `${start} - ${formatDateId(effectiveEnd)}`;
};
const lastUpdated = useMemo(() => {
+216 -168
View File
@@ -3,176 +3,207 @@ import { beforeEach, describe, expect, it, vi } from 'vitest';
import { api } from '../../services/apiClient.ts';
import FeedSackCountingPage from '../FeedSackCountingPage.tsx';
const { karungRows, manualRows, cycle, mismatchedKarungRows, mismatchedManualRows } = vi.hoisted(
() => {
const cycle = {
const {
karungRows,
manualRows,
cycle,
activeCycle,
mismatchedKarungRows,
mismatchedManualRows,
farmState,
} = vi.hoisted(() => {
const cycle = {
id: 1,
kandang: 1,
flock: 1,
total_days: 35,
current_day: 2,
start_date: '2026-08-10',
end_date: '2026-09-13' as string | null,
visible_through_date: '2026-09-13',
chick_in_weight: 45,
doc_in_count: 20000,
status: 'closed' as string,
feed_initial_balance: 0,
created_at: '',
updated_at: '',
};
const activeCycle = {
...cycle,
id: 2,
start_date: '2026-10-06',
visible_through_date: '2026-10-13',
end_date: null as string | null,
total_days: 1,
status: 'active' as string,
};
const farmState: {
selectedCycle: typeof cycle;
cycles: (typeof cycle)[];
} = {
selectedCycle: cycle,
cycles: [cycle],
};
const karungRows = [
{
id: 1,
cycle: 1,
kandang: 1,
flock: 1,
total_days: 35,
current_day: 2,
start_date: '2026-08-10',
end_date: '2026-09-13',
chick_in_weight: 45,
doc_in_count: 20000,
status: 'active',
feed_initial_balance: 0,
flock_id: 1,
date: '2026-08-10',
in_today: 10,
out_today: 4,
in_total: 10,
out_total: 4,
feed_use_today: 4,
feed_use_total: 4,
created_at: '',
updated_at: '',
};
const karungRows = [
{
id: 1,
cycle: 1,
kandang: 1,
flock_id: 1,
date: '2026-08-10',
in_today: 10,
out_today: 4,
in_total: 10,
out_total: 4,
feed_use_today: 4,
feed_use_total: 4,
created_at: '',
updated_at: '',
},
{
id: 2,
cycle: 1,
kandang: 1,
flock_id: 1,
date: '2026-08-11',
in_today: 12,
out_today: 6,
in_total: 22,
out_total: 10,
feed_use_today: 6,
feed_use_total: 10,
created_at: '',
updated_at: '',
},
];
const manualRows = [
{
id: 1,
cycle: 1,
kandang: 1,
flock_id: 1,
date: '2026-08-10',
age_manual: 0,
mortality_manual: 0,
mortality_manual_total: 0,
feed_in_manual: 10,
feed_use_manual: 4,
feed_in_manual_total: 10,
feed_use_manual_total: 4,
feed_out_manual: 0,
feed_out_manual_total: 0,
harvest_manual: 0,
harvest_manual_total: 0,
harvest_weight_manual: 0,
harvest_weight_manual_total: 0,
manual_weight: 0,
average_harvest_day_manual: 0,
average_harvest_weight_manual: 0,
fcr_manual: 0,
eef_manual: 0,
created_at: '',
updated_at: '',
},
{
id: 2,
cycle: 1,
kandang: 1,
flock_id: 1,
date: '2026-08-11',
age_manual: 1,
mortality_manual: 0,
mortality_manual_total: 0,
feed_in_manual: 10,
feed_use_manual: 7,
feed_in_manual_total: 20,
feed_use_manual_total: 11,
feed_out_manual: 0,
feed_out_manual_total: 0,
harvest_manual: 0,
harvest_manual_total: 0,
harvest_weight_manual: 0,
harvest_weight_manual_total: 0,
manual_weight: 0,
average_harvest_day_manual: 0,
average_harvest_weight_manual: 0,
fcr_manual: 0,
eef_manual: 0,
created_at: '',
updated_at: '',
},
];
// IoT already on the newer day; manual only exists for the previous day.
const mismatchedKarungRows = [
{
id: 10,
cycle: 1,
kandang: 1,
flock_id: 1,
date: '2026-09-02',
in_today: 192,
out_today: 1,
in_total: 192,
out_total: 1,
feed_use_today: 7,
feed_use_total: 7,
created_at: '',
updated_at: '',
},
{
id: 11,
cycle: 1,
kandang: 1,
flock_id: 1,
date: '2026-09-03',
in_today: 50,
out_today: 0,
in_total: 242,
out_total: 1,
feed_use_today: 4,
feed_use_total: 11,
created_at: '',
updated_at: '',
},
];
const mismatchedManualRows = [
{
id: 10,
cycle: 1,
kandang: 1,
flock_id: 1,
date: '2026-09-02',
age_manual: 0,
mortality_manual: 0,
mortality_manual_total: 0,
feed_in_manual: 192,
feed_use_manual: 7,
feed_in_manual_total: 192,
feed_use_manual_total: 7,
feed_out_manual: 1,
feed_out_manual_total: 1,
harvest_manual: 0,
harvest_manual_total: 0,
harvest_weight_manual: 0,
harvest_weight_manual_total: 0,
manual_weight: 0,
average_harvest_day_manual: 0,
average_harvest_weight_manual: 0,
fcr_manual: 0,
eef_manual: 0,
created_at: '',
updated_at: '',
},
];
return { karungRows, manualRows, cycle, mismatchedKarungRows, mismatchedManualRows };
}
);
},
{
id: 2,
cycle: 1,
kandang: 1,
flock_id: 1,
date: '2026-08-11',
in_today: 12,
out_today: 6,
in_total: 22,
out_total: 10,
feed_use_today: 6,
feed_use_total: 10,
created_at: '',
updated_at: '',
},
];
const manualRows = [
{
id: 1,
cycle: 1,
kandang: 1,
flock_id: 1,
date: '2026-08-10',
age_manual: 0,
mortality_manual: 0,
mortality_manual_total: 0,
feed_in_manual: 10,
feed_use_manual: 4,
feed_in_manual_total: 10,
feed_use_manual_total: 4,
feed_out_manual: 0,
feed_out_manual_total: 0,
harvest_manual: 0,
harvest_manual_total: 0,
harvest_weight_manual: 0,
harvest_weight_manual_total: 0,
manual_weight: 0,
average_harvest_day_manual: 0,
average_harvest_weight_manual: 0,
fcr_manual: 0,
eef_manual: 0,
created_at: '',
updated_at: '',
},
{
id: 2,
cycle: 1,
kandang: 1,
flock_id: 1,
date: '2026-08-11',
age_manual: 1,
mortality_manual: 0,
mortality_manual_total: 0,
feed_in_manual: 10,
feed_use_manual: 7,
feed_in_manual_total: 20,
feed_use_manual_total: 11,
feed_out_manual: 0,
feed_out_manual_total: 0,
harvest_manual: 0,
harvest_manual_total: 0,
harvest_weight_manual: 0,
harvest_weight_manual_total: 0,
manual_weight: 0,
average_harvest_day_manual: 0,
average_harvest_weight_manual: 0,
fcr_manual: 0,
eef_manual: 0,
created_at: '',
updated_at: '',
},
];
// IoT already on the newer day; manual only exists for the previous day.
const mismatchedKarungRows = [
{
id: 10,
cycle: 1,
kandang: 1,
flock_id: 1,
date: '2026-09-02',
in_today: 192,
out_today: 1,
in_total: 192,
out_total: 1,
feed_use_today: 7,
feed_use_total: 7,
created_at: '',
updated_at: '',
},
{
id: 11,
cycle: 1,
kandang: 1,
flock_id: 1,
date: '2026-09-03',
in_today: 50,
out_today: 0,
in_total: 242,
out_total: 1,
feed_use_today: 4,
feed_use_total: 11,
created_at: '',
updated_at: '',
},
];
const mismatchedManualRows = [
{
id: 10,
cycle: 1,
kandang: 1,
flock_id: 1,
date: '2026-09-02',
age_manual: 0,
mortality_manual: 0,
mortality_manual_total: 0,
feed_in_manual: 192,
feed_use_manual: 7,
feed_in_manual_total: 192,
feed_use_manual_total: 7,
feed_out_manual: 1,
feed_out_manual_total: 1,
harvest_manual: 0,
harvest_manual_total: 0,
harvest_weight_manual: 0,
harvest_weight_manual_total: 0,
manual_weight: 0,
average_harvest_day_manual: 0,
average_harvest_weight_manual: 0,
fcr_manual: 0,
eef_manual: 0,
created_at: '',
updated_at: '',
},
];
return {
karungRows,
manualRows,
cycle,
activeCycle,
mismatchedKarungRows,
mismatchedManualRows,
farmState,
};
});
vi.mock('../../services/apiClient.ts', () => ({
api: {
@@ -202,8 +233,8 @@ vi.mock('../../context/FarmContext.tsx', () => ({
updated_at: '',
},
selectedKandangId: 1,
selectedCycle: cycle,
cycles: [cycle],
selectedCycle: farmState.selectedCycle,
cycles: farmState.cycles,
setSelectedCycleId: vi.fn(),
updateCycle: vi.fn(),
}),
@@ -287,4 +318,21 @@ describe('FeedSackCountingPage', () => {
vi.unstubAllGlobals();
}
});
it('displays the cutoff date (visible_through_date) for active cycles instead of sticking to start_date + 1 day', async () => {
vi.stubGlobal('ResizeObserver', ResizeObserverStub);
farmState.selectedCycle = activeCycle;
farmState.cycles = [activeCycle];
try {
render(<FeedSackCountingPage onSettings={() => undefined} />);
expect(await screen.findByRole('heading', { name: 'Hitung Karung' })).toBeInTheDocument();
// Should display "6 Okt 2026 - 13 Okt 2026"
expect(screen.getByText(/6 Okt 2026 - 13 Okt 2026/i)).toBeInTheDocument();
} finally {
farmState.selectedCycle = cycle;
farmState.cycles = [cycle];
vi.unstubAllGlobals();
}
});
});