diff --git a/src/builder-code.test.ts b/src/builder-code.test.ts index 0797306f..f565bfe9 100644 --- a/src/builder-code.test.ts +++ b/src/builder-code.test.ts @@ -19,6 +19,41 @@ describe("withBuilderCodeServiceCode", () => { expect(info.s).toEqual([BLOCKRUN_SERVICE_CODE]); }); + it("appends the BlockRun code after existing service attribution", () => { + const serviceCodes = ["bc_existing"]; + const ext = withBuilderCodeServiceCode({ + "builder-code": { info: { a: "app", s: serviceCodes } }, + }); + const info = (ext["builder-code"] as BuilderCodeExtension).info; + + expect(info.a).toBe("app"); + expect(info.s).toEqual(["bc_existing", BLOCKRUN_SERVICE_CODE]); + expect(info.s).not.toBe(serviceCodes); + expect(serviceCodes).toEqual(["bc_existing"]); + }); + + it("preserves valid service codes from a partially malformed array", () => { + const ext = withBuilderCodeServiceCode({ + "builder-code": { info: { s: ["bc_existing", 42, null] } }, + }); + + expect((ext["builder-code"] as BuilderCodeExtension).info.s).toEqual([ + "bc_existing", + BLOCKRUN_SERVICE_CODE, + ]); + }); + + it("does not duplicate an existing BlockRun service code", () => { + const ext = withBuilderCodeServiceCode({ + "builder-code": { info: { s: ["bc_existing", BLOCKRUN_SERVICE_CODE] } }, + }); + + expect((ext["builder-code"] as BuilderCodeExtension).info.s).toEqual([ + "bc_existing", + BLOCKRUN_SERVICE_CODE, + ]); + }); + it("preserves unrelated extensions", () => { const ext = withBuilderCodeServiceCode({ "some-ext": { foo: 1 } }); expect(ext["some-ext"]).toEqual({ foo: 1 }); diff --git a/src/builder-code.ts b/src/builder-code.ts index fd5c8ee1..a2153632 100644 --- a/src/builder-code.ts +++ b/src/builder-code.ts @@ -27,9 +27,17 @@ export function withBuilderCodeServiceCode( ): Record { const merged: Record = { ...(extensions ?? {}) }; const existing = (merged["builder-code"] as { info?: Record } | undefined) ?? {}; + const existingServiceCodes = Array.isArray(existing.info?.s) + ? existing.info.s.filter((code): code is string => typeof code === "string") + : []; merged["builder-code"] = { ...existing, - info: { ...(existing.info ?? {}), s: [BLOCKRUN_SERVICE_CODE] }, + info: { + ...(existing.info ?? {}), + s: existingServiceCodes.includes(BLOCKRUN_SERVICE_CODE) + ? [...existingServiceCodes] + : [...existingServiceCodes, BLOCKRUN_SERVICE_CODE], + }, }; return merged; }