|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import { |
| 5 | + terminateOptions, |
| 6 | + isTerminateInstanceOptions, |
| 7 | + TERMINATE_OPTIONS_SYMBOL, |
| 8 | +} from "../src"; |
| 9 | + |
| 10 | +describe("TerminateInstanceOptions", () => { |
| 11 | + describe("terminateOptions factory function", () => { |
| 12 | + it("should create options with recursive flag", () => { |
| 13 | + const options = terminateOptions({ recursive: true }); |
| 14 | + |
| 15 | + expect(options.recursive).toBe(true); |
| 16 | + expect(isTerminateInstanceOptions(options)).toBe(true); |
| 17 | + }); |
| 18 | + |
| 19 | + it("should create options with output", () => { |
| 20 | + const options = terminateOptions({ output: { reason: "cancelled" } }); |
| 21 | + |
| 22 | + expect(options.output).toEqual({ reason: "cancelled" }); |
| 23 | + expect(isTerminateInstanceOptions(options)).toBe(true); |
| 24 | + }); |
| 25 | + |
| 26 | + it("should create options with both recursive and output", () => { |
| 27 | + const options = terminateOptions({ |
| 28 | + recursive: true, |
| 29 | + output: { reason: "cancelled" }, |
| 30 | + }); |
| 31 | + |
| 32 | + expect(options.recursive).toBe(true); |
| 33 | + expect(options.output).toEqual({ reason: "cancelled" }); |
| 34 | + expect(isTerminateInstanceOptions(options)).toBe(true); |
| 35 | + }); |
| 36 | + |
| 37 | + it("should create options with empty object", () => { |
| 38 | + const options = terminateOptions({}); |
| 39 | + |
| 40 | + expect(options.recursive).toBeUndefined(); |
| 41 | + expect(options.output).toBeUndefined(); |
| 42 | + expect(isTerminateInstanceOptions(options)).toBe(true); |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + describe("isTerminateInstanceOptions type guard", () => { |
| 47 | + it("should return true for options created with terminateOptions()", () => { |
| 48 | + const options = terminateOptions({ recursive: true }); |
| 49 | + expect(isTerminateInstanceOptions(options)).toBe(true); |
| 50 | + }); |
| 51 | + |
| 52 | + it("should return false for plain objects with recursive property", () => { |
| 53 | + // This is the key test - user output that happens to have 'recursive' property |
| 54 | + const userOutput = { recursive: true, data: "some value" }; |
| 55 | + expect(isTerminateInstanceOptions(userOutput)).toBe(false); |
| 56 | + }); |
| 57 | + |
| 58 | + it("should return false for plain objects with output property", () => { |
| 59 | + // User output that happens to have 'output' property |
| 60 | + const userOutput = { output: "some value", status: "done" }; |
| 61 | + expect(isTerminateInstanceOptions(userOutput)).toBe(false); |
| 62 | + }); |
| 63 | + |
| 64 | + it("should return false for plain objects with both recursive and output properties", () => { |
| 65 | + // User output that coincidentally has both properties |
| 66 | + const userOutput = { recursive: true, output: "some data", extra: "field" }; |
| 67 | + expect(isTerminateInstanceOptions(userOutput)).toBe(false); |
| 68 | + }); |
| 69 | + |
| 70 | + it("should return false for null", () => { |
| 71 | + expect(isTerminateInstanceOptions(null)).toBe(false); |
| 72 | + }); |
| 73 | + |
| 74 | + it("should return false for undefined", () => { |
| 75 | + expect(isTerminateInstanceOptions(undefined)).toBe(false); |
| 76 | + }); |
| 77 | + |
| 78 | + it("should return false for primitives", () => { |
| 79 | + expect(isTerminateInstanceOptions("string")).toBe(false); |
| 80 | + expect(isTerminateInstanceOptions(123)).toBe(false); |
| 81 | + expect(isTerminateInstanceOptions(true)).toBe(false); |
| 82 | + }); |
| 83 | + |
| 84 | + it("should return false for arrays", () => { |
| 85 | + expect(isTerminateInstanceOptions([1, 2, 3])).toBe(false); |
| 86 | + expect(isTerminateInstanceOptions([{ recursive: true }])).toBe(false); |
| 87 | + }); |
| 88 | + |
| 89 | + it("should return false for arbitrary objects", () => { |
| 90 | + expect(isTerminateInstanceOptions({ foo: "bar" })).toBe(false); |
| 91 | + expect(isTerminateInstanceOptions({ status: "done", data: {} })).toBe(false); |
| 92 | + }); |
| 93 | + |
| 94 | + it("should return false for objects that look like options but lack the symbol", () => { |
| 95 | + // This simulates the old detection bug - objects with matching property names |
| 96 | + const fakeOptions = { recursive: true, output: { reason: "test" } }; |
| 97 | + expect(isTerminateInstanceOptions(fakeOptions)).toBe(false); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + describe("TERMINATE_OPTIONS_SYMBOL", () => { |
| 102 | + it("should be a symbol", () => { |
| 103 | + expect(typeof TERMINATE_OPTIONS_SYMBOL).toBe("symbol"); |
| 104 | + }); |
| 105 | + |
| 106 | + it("should be consistent across imports (Symbol.for)", () => { |
| 107 | + const symbol1 = Symbol.for("durabletask.TerminateInstanceOptions"); |
| 108 | + const symbol2 = Symbol.for("durabletask.TerminateInstanceOptions"); |
| 109 | + expect(symbol1).toBe(symbol2); |
| 110 | + expect(symbol1).toBe(TERMINATE_OPTIONS_SYMBOL); |
| 111 | + }); |
| 112 | + |
| 113 | + it("should be present in options created with terminateOptions()", () => { |
| 114 | + const options = terminateOptions({ recursive: true }); |
| 115 | + expect(TERMINATE_OPTIONS_SYMBOL in options).toBe(true); |
| 116 | + expect(options[TERMINATE_OPTIONS_SYMBOL]).toBe(true); |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + describe("edge cases for user output disambiguation", () => { |
| 121 | + it("should treat plain object with recursive:true as output, not options", () => { |
| 122 | + // This was the original bug - user passing output like {recursive: true, data: {...}} |
| 123 | + const userOutput = { recursive: true, data: { value: 123 } }; |
| 124 | + |
| 125 | + // Should NOT be detected as TerminateInstanceOptions |
| 126 | + expect(isTerminateInstanceOptions(userOutput)).toBe(false); |
| 127 | + |
| 128 | + // When used with terminateOptions, it works correctly |
| 129 | + const options = terminateOptions({ output: userOutput, recursive: false }); |
| 130 | + expect(isTerminateInstanceOptions(options)).toBe(true); |
| 131 | + expect(options.output).toEqual(userOutput); |
| 132 | + }); |
| 133 | + |
| 134 | + it("should correctly handle nested objects with problematic property names", () => { |
| 135 | + const complexOutput = { |
| 136 | + recursive: true, |
| 137 | + output: "nested", |
| 138 | + metadata: { |
| 139 | + recursive: false, |
| 140 | + output: "deeply nested" |
| 141 | + } |
| 142 | + }; |
| 143 | + |
| 144 | + // Plain object should not be detected as options |
| 145 | + expect(isTerminateInstanceOptions(complexOutput)).toBe(false); |
| 146 | + |
| 147 | + // Wrapped in terminateOptions should work |
| 148 | + const options = terminateOptions({ output: complexOutput }); |
| 149 | + expect(isTerminateInstanceOptions(options)).toBe(true); |
| 150 | + }); |
| 151 | + }); |
| 152 | +}); |
0 commit comments