課題2を解決するには、時刻と ID を、テストで固定できる形で受け取ります。port は、ユースケースが外部に要求する処理の型です。修正前の deps.stateStore.save と deps.eventLog.append の2つを、deps.examinationStartedStore.store の1つに置き換えます。
-
deps.resolver(S04)とdeps.examinationStartedStore(S06)は、ユースケースがdepsを通して外部に要求する処理です。ユースケースが外部に要求する処理の型を、port と呼びます。 -
時刻と ID の取得も外部の処理です。
Clockは「現在の時刻を返す」処理の型、EventIdGeneratorは「新しい ID を返す」処理の型で、どちらも port です。ユースケースは処理の型だけを参照します。 -
本番では、port に本番の実装を渡します。
app.tsのcreateExaminationStartedStoreは SQLite の resolver と store を作り、defaultEffectsはnew Date()を呼ぶclockとrandomUUID()を呼ぶeventIdGeneratorを作ります。 -
テストでは、同じユースケースの port にテスト用の実装(fake)を渡します。メモリ上の store と、固定の時刻と ID を返す
clockとeventIdGeneratorを渡すため、ユースケースのコードを変更せずに結果を固定できます。
startExamination.tsS04deps.resolver. resolveById(input.appointmentId), startExamination.tsS06.andThrough(deps.examinationStartedStore.store) dependencies.tsS06export type ExaminationStartedStore = Readonly<{
store: (event: ExaminationStarted) => ResultAsync<void, AppointmentConflict>;
}>; dependencies.tsexport type EventContextDependencies = Readonly<{
clock: Clock;
eventIdGenerator: EventIdGenerator;
}>; clock.tsexport type Clock = Readonly<{
now: () => string;
}>; eventIdGenerator.tsexport type EventIdGenerator = Readonly<{
generate: () => EventId;
}>; app.tsconst store = createExaminationStartedStore(
database,
session06SolutionInitialAppointment,
); app.tsconst defaultEffects: EventContextDependencies = {
clock: { now: () => new Date(). toISOString() },
eventIdGenerator: { generate: () => EventId. parse(randomUUID()) },
}; in-memory-store.test.tsconst adapter = createInMemoryExaminationStartedStore([checkedIn]);
const result = await startExaminationWithEffects({
resolver: adapter.resolver,
examinationStartedStore: adapter.store,
clock: { now: () => "2026-08-30T06:30:00.000Z" },
eventIdGenerator: { generate: () => eventId },
})(input); ユースケースの
- port
deps何も.resolver つながって SQLiteデータベースからいません 予約を fake の resolverメモリ上の取得する 予約を返す - port
deps何も.examinationStartedStore つながって SQLiteデータベースへいません 保存する fake の storeメモリ上に保存し、呼ばれた回数を 数える - port
deps何も.clock つながって new Date()現在の時刻をいません 返す 固定の時刻2026-08-30T06:30:00.000Z を返す - port
deps何も.eventIdGenerator つながって randomUUID()実行のたびにいません 新しい ID を 固定の ID常に同じ ID を返す 返す
本番では本番の実装を、テストでは fake(テストのために本番の実装の代わりに渡す実装)を渡します。