Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/durabletask-js/src/entities/task-entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export abstract class TaskEntity<TState> implements ITaskEntity {
if (name.toLowerCase() === operationName) {
const prop = (this as unknown as Record<string, unknown>)[name];
// Skip non-functions and built-in methods
if (typeof prop === "function" && name !== "constructor" && name !== "run") {
if (typeof prop === "function" && name !== "constructor" && name !== "run" && name !== "initializeState") {
return name;
}
}
Expand Down
53 changes: 53 additions & 0 deletions packages/durabletask-js/test/task-entity.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,4 +422,57 @@ describe("TaskEntity", () => {
);
});
});

describe("lifecycle method exclusion", () => {
it("should not dispatch to overridden initializeState as an operation", async () => {
const entity = new CounterEntity();
const { operation } = createMockOperation("initializeState", undefined, { count: 42 });

// initializeState is a lifecycle method, not a user-facing operation.
// Even though CounterEntity overrides it, it should not be callable.
await expect(entity.run(operation)).rejects.toThrow(
"No suitable method found for entity operation 'initializeState'",
);
});

it("should not dispatch to initializeState case-insensitively", async () => {
const entity = new CounterEntity();
const { operation } = createMockOperation("INITIALIZESTATE", undefined, { count: 42 });

await expect(entity.run(operation)).rejects.toThrow(
"No suitable method found for entity operation 'INITIALIZESTATE'",
);
});

it("should still call initializeState internally when state is null", async () => {
const entity = new CounterEntity();
// No initial state — initializeState should be called internally
const { operation } = createMockOperation("get", undefined, undefined);

const result = await entity.run(operation);

// initializeState returns { count: 0 }, so get() returns 0
expect(result).toBe(0);
});

it("should not dispatch to 'run' as an operation", async () => {
// Verify that 'run' remains excluded from dispatch
class SimpleEntity extends TaskEntity<{ value: number }> {
getValue(): number {
return this.state.value;
}

protected initializeState(): { value: number } {
return { value: 0 };
}
}

const entity = new SimpleEntity();
const { operation } = createMockOperation("run", undefined, { value: 10 });

await expect(entity.run(operation)).rejects.toThrow(
"No suitable method found for entity operation 'run'",
);
});
});
});
Loading