amazon-bedrock.test.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import { test, expect } from "bun:test"
  2. import path from "path"
  3. import { tmpdir } from "../fixture/fixture"
  4. import { Instance } from "../../src/project/instance"
  5. import { Provider } from "../../src/provider/provider"
  6. import { Env } from "../../src/env"
  7. import { Auth } from "../../src/auth"
  8. import { Global } from "../../src/global"
  9. test("Bedrock: config region takes precedence over AWS_REGION env var", async () => {
  10. await using tmp = await tmpdir({
  11. init: async (dir) => {
  12. await Bun.write(
  13. path.join(dir, "opencode.json"),
  14. JSON.stringify({
  15. $schema: "https://opencode.ai/config.json",
  16. provider: {
  17. "amazon-bedrock": {
  18. options: {
  19. region: "eu-west-1",
  20. },
  21. },
  22. },
  23. }),
  24. )
  25. },
  26. })
  27. await Instance.provide({
  28. directory: tmp.path,
  29. init: async () => {
  30. Env.set("AWS_REGION", "us-east-1")
  31. Env.set("AWS_PROFILE", "default")
  32. },
  33. fn: async () => {
  34. const providers = await Provider.list()
  35. expect(providers["amazon-bedrock"]).toBeDefined()
  36. // Region from config should be used (not env var)
  37. expect(providers["amazon-bedrock"].options?.region).toBe("eu-west-1")
  38. },
  39. })
  40. })
  41. test("Bedrock: falls back to AWS_REGION env var when no config", async () => {
  42. await using tmp = await tmpdir({
  43. init: async (dir) => {
  44. await Bun.write(
  45. path.join(dir, "opencode.json"),
  46. JSON.stringify({
  47. $schema: "https://opencode.ai/config.json",
  48. }),
  49. )
  50. },
  51. })
  52. await Instance.provide({
  53. directory: tmp.path,
  54. init: async () => {
  55. Env.set("AWS_REGION", "eu-west-1")
  56. Env.set("AWS_PROFILE", "default")
  57. },
  58. fn: async () => {
  59. const providers = await Provider.list()
  60. expect(providers["amazon-bedrock"]).toBeDefined()
  61. expect(providers["amazon-bedrock"].options?.region).toBe("eu-west-1")
  62. },
  63. })
  64. })
  65. test("Bedrock: without explicit region config, uses AWS_REGION env or defaults", async () => {
  66. await using tmp = await tmpdir({
  67. init: async (dir) => {
  68. await Bun.write(
  69. path.join(dir, "opencode.json"),
  70. JSON.stringify({
  71. $schema: "https://opencode.ai/config.json",
  72. }),
  73. )
  74. },
  75. })
  76. await Instance.provide({
  77. directory: tmp.path,
  78. init: async () => {
  79. Env.set("AWS_PROFILE", "default")
  80. // AWS_REGION might be set in the environment, use that or default
  81. },
  82. fn: async () => {
  83. const providers = await Provider.list()
  84. expect(providers["amazon-bedrock"]).toBeDefined()
  85. // Should have some region set (either from env or default)
  86. expect(providers["amazon-bedrock"].options?.region).toBeDefined()
  87. expect(typeof providers["amazon-bedrock"].options?.region).toBe("string")
  88. },
  89. })
  90. })
  91. test("Bedrock: uses config region in provider options", async () => {
  92. await using tmp = await tmpdir({
  93. init: async (dir) => {
  94. await Bun.write(
  95. path.join(dir, "opencode.json"),
  96. JSON.stringify({
  97. $schema: "https://opencode.ai/config.json",
  98. provider: {
  99. "amazon-bedrock": {
  100. options: {
  101. region: "eu-north-1",
  102. },
  103. },
  104. },
  105. }),
  106. )
  107. },
  108. })
  109. await Instance.provide({
  110. directory: tmp.path,
  111. init: async () => {
  112. Env.set("AWS_PROFILE", "default")
  113. },
  114. fn: async () => {
  115. const providers = await Provider.list()
  116. const bedrockProvider = providers["amazon-bedrock"]
  117. expect(bedrockProvider).toBeDefined()
  118. expect(bedrockProvider.options?.region).toBe("eu-north-1")
  119. },
  120. })
  121. })
  122. test("Bedrock: respects config region for different instances", async () => {
  123. // First instance with EU config
  124. await using tmp1 = await tmpdir({
  125. init: async (dir) => {
  126. await Bun.write(
  127. path.join(dir, "opencode.json"),
  128. JSON.stringify({
  129. $schema: "https://opencode.ai/config.json",
  130. provider: {
  131. "amazon-bedrock": {
  132. options: {
  133. region: "eu-west-1",
  134. },
  135. },
  136. },
  137. }),
  138. )
  139. },
  140. })
  141. await Instance.provide({
  142. directory: tmp1.path,
  143. init: async () => {
  144. Env.set("AWS_PROFILE", "default")
  145. Env.set("AWS_REGION", "us-east-1")
  146. },
  147. fn: async () => {
  148. const providers1 = await Provider.list()
  149. expect(providers1["amazon-bedrock"].options?.region).toBe("eu-west-1")
  150. },
  151. })
  152. // Second instance with US config
  153. await using tmp2 = await tmpdir({
  154. init: async (dir) => {
  155. await Bun.write(
  156. path.join(dir, "opencode.json"),
  157. JSON.stringify({
  158. $schema: "https://opencode.ai/config.json",
  159. provider: {
  160. "amazon-bedrock": {
  161. options: {
  162. region: "us-west-2",
  163. },
  164. },
  165. },
  166. }),
  167. )
  168. },
  169. })
  170. await Instance.provide({
  171. directory: tmp2.path,
  172. init: async () => {
  173. Env.set("AWS_PROFILE", "default")
  174. Env.set("AWS_REGION", "eu-west-1")
  175. },
  176. fn: async () => {
  177. const providers2 = await Provider.list()
  178. expect(providers2["amazon-bedrock"].options?.region).toBe("us-west-2")
  179. },
  180. })
  181. })
  182. test("Bedrock: loads when bearer token from auth.json is present", async () => {
  183. await using tmp = await tmpdir({
  184. init: async (dir) => {
  185. await Bun.write(
  186. path.join(dir, "opencode.json"),
  187. JSON.stringify({
  188. $schema: "https://opencode.ai/config.json",
  189. provider: {
  190. "amazon-bedrock": {
  191. options: {
  192. region: "eu-west-1",
  193. },
  194. },
  195. },
  196. }),
  197. )
  198. },
  199. })
  200. // Setup auth.json with bearer token for amazon-bedrock
  201. const authPath = path.join(Global.Path.data, "auth.json")
  202. await Bun.write(
  203. authPath,
  204. JSON.stringify({
  205. "amazon-bedrock": {
  206. type: "api",
  207. key: "test-bearer-token",
  208. },
  209. }),
  210. )
  211. await Instance.provide({
  212. directory: tmp.path,
  213. init: async () => {
  214. // Clear env vars so only auth.json should trigger autoload
  215. Env.set("AWS_PROFILE", "")
  216. Env.set("AWS_ACCESS_KEY_ID", "")
  217. Env.set("AWS_BEARER_TOKEN_BEDROCK", "")
  218. },
  219. fn: async () => {
  220. const providers = await Provider.list()
  221. expect(providers["amazon-bedrock"]).toBeDefined()
  222. expect(providers["amazon-bedrock"].options?.region).toBe("eu-west-1")
  223. },
  224. })
  225. })