From d875e08d482469bb96b2ad27199e3bd52578c144 Mon Sep 17 00:00:00 2001 From: Mauricio Siu Date: Sat, 13 Dec 2025 15:05:57 -0600 Subject: [PATCH] test(helpers): add tests for handling empty and undefined string variables in templates - Introduced new test cases to verify the behavior of the `processValue` function when dealing with empty string variables and undefined variables. - Ensured that empty strings are correctly replaced and undefined variables remain unchanged in the output. --- .../templates/helpers.template.test.ts | 44 +++++++++++++++++++ packages/server/src/templates/processors.ts | 4 +- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/apps/dokploy/__test__/templates/helpers.template.test.ts b/apps/dokploy/__test__/templates/helpers.template.test.ts index 3ae92ae20..f2af2717b 100644 --- a/apps/dokploy/__test__/templates/helpers.template.test.ts +++ b/apps/dokploy/__test__/templates/helpers.template.test.ts @@ -161,6 +161,50 @@ describe("helpers functions", () => { }); }); + describe("Empty string variables", () => { + it("should replace variables with empty string values correctly", () => { + const variables = { + smtp_username: "", + smtp_password: "", + non_empty: "value", + }; + + const result1 = processValue("${smtp_username}", variables, mockSchema); + expect(result1).toBe(""); + + const result2 = processValue("${smtp_password}", variables, mockSchema); + expect(result2).toBe(""); + + const result3 = processValue("${non_empty}", variables, mockSchema); + expect(result3).toBe("value"); + }); + + it("should not replace undefined variables", () => { + const variables = { + defined_var: "", + }; + + const result = processValue("${undefined_var}", variables, mockSchema); + expect(result).toBe("${undefined_var}"); + }); + + it("should handle mixed empty and non-empty variables in template", () => { + const variables = { + smtp_address: "smtp.example.com", + smtp_port: "2525", + smtp_username: "", + smtp_password: "", + }; + + const template = + "SMTP_ADDRESS=${smtp_address} SMTP_PORT=${smtp_port} SMTP_USERNAME=${smtp_username} SMTP_PASSWORD=${smtp_password}"; + const result = processValue(template, variables, mockSchema); + expect(result).toBe( + "SMTP_ADDRESS=smtp.example.com SMTP_PORT=2525 SMTP_USERNAME= SMTP_PASSWORD=", + ); + }); + }); + describe("${jwt}", () => { it("should generate a JWT string", () => { const jwt = processValue("${jwt}", {}, mockSchema); diff --git a/packages/server/src/templates/processors.ts b/packages/server/src/templates/processors.ts index 9e73fb1f4..ce1553095 100644 --- a/packages/server/src/templates/processors.ts +++ b/packages/server/src/templates/processors.ts @@ -170,12 +170,12 @@ export function processValue( } // If not a utility function, try to get from variables - return variables[varName] || match; + return varName in variables ? (variables[varName] ?? match) : match; }); // Then replace any remaining ${var} with their values from variables processedValue = processedValue.replace(/\${([^}]+)}/g, (match, varName) => { - return variables[varName] || match; + return varName in variables ? (variables[varName] ?? match) : match; }); return processedValue;