@pagopa/dx-savemoney 0.3.2 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (163) hide show
  1. package/README.md +12 -0
  2. package/dist/__tests__/finding.test.js +22 -0
  3. package/dist/__tests__/finding.test.js.map +1 -1
  4. package/dist/azure/__tests__/analyzer-tags.test.js +28 -1
  5. package/dist/azure/__tests__/analyzer-tags.test.js.map +1 -1
  6. package/dist/azure/__tests__/config.test.js +10 -0
  7. package/dist/azure/__tests__/config.test.js.map +1 -1
  8. package/dist/azure/__tests__/report.test.js +71 -0
  9. package/dist/azure/__tests__/report.test.js.map +1 -1
  10. package/dist/azure/analyzer.d.ts +8 -1
  11. package/dist/azure/analyzer.d.ts.map +1 -1
  12. package/dist/azure/analyzer.js +55 -37
  13. package/dist/azure/analyzer.js.map +1 -1
  14. package/dist/azure/analyzers/registry.d.ts.map +1 -1
  15. package/dist/azure/analyzers/registry.js +4 -4
  16. package/dist/azure/analyzers/registry.js.map +1 -1
  17. package/dist/azure/analyzers/types.d.ts +6 -0
  18. package/dist/azure/analyzers/types.d.ts.map +1 -1
  19. package/dist/azure/pricing/__tests__/cache.test.d.ts +8 -0
  20. package/dist/azure/pricing/__tests__/cache.test.d.ts.map +1 -0
  21. package/dist/azure/pricing/__tests__/cache.test.js +56 -0
  22. package/dist/azure/pricing/__tests__/cache.test.js.map +1 -0
  23. package/dist/azure/pricing/__tests__/client.test.d.ts +9 -0
  24. package/dist/azure/pricing/__tests__/client.test.d.ts.map +1 -0
  25. package/dist/azure/pricing/__tests__/client.test.js +181 -0
  26. package/dist/azure/pricing/__tests__/client.test.js.map +1 -0
  27. package/dist/azure/pricing/__tests__/pricing-service.test.d.ts +6 -0
  28. package/dist/azure/pricing/__tests__/pricing-service.test.d.ts.map +1 -0
  29. package/dist/azure/pricing/__tests__/pricing-service.test.js +283 -0
  30. package/dist/azure/pricing/__tests__/pricing-service.test.js.map +1 -0
  31. package/dist/azure/pricing/cache.d.ts +45 -0
  32. package/dist/azure/pricing/cache.d.ts.map +1 -0
  33. package/dist/azure/pricing/cache.js +79 -0
  34. package/dist/azure/pricing/cache.js.map +1 -0
  35. package/dist/azure/pricing/client.d.ts +67 -0
  36. package/dist/azure/pricing/client.d.ts.map +1 -0
  37. package/dist/azure/pricing/client.js +96 -0
  38. package/dist/azure/pricing/client.js.map +1 -0
  39. package/dist/azure/pricing/index.d.ts +6 -0
  40. package/dist/azure/pricing/index.d.ts.map +1 -0
  41. package/dist/azure/pricing/index.js +6 -0
  42. package/dist/azure/pricing/index.js.map +1 -0
  43. package/dist/azure/pricing/pricing-service.d.ts +46 -0
  44. package/dist/azure/pricing/pricing-service.d.ts.map +1 -0
  45. package/dist/azure/pricing/pricing-service.js +84 -0
  46. package/dist/azure/pricing/pricing-service.js.map +1 -0
  47. package/dist/azure/pricing/resolvers/__tests__/app-service.test.d.ts +5 -0
  48. package/dist/azure/pricing/resolvers/__tests__/app-service.test.d.ts.map +1 -0
  49. package/dist/azure/pricing/resolvers/__tests__/app-service.test.js +137 -0
  50. package/dist/azure/pricing/resolvers/__tests__/app-service.test.js.map +1 -0
  51. package/dist/azure/pricing/resolvers/__tests__/disk.test.d.ts +5 -0
  52. package/dist/azure/pricing/resolvers/__tests__/disk.test.d.ts.map +1 -0
  53. package/dist/azure/pricing/resolvers/__tests__/disk.test.js +160 -0
  54. package/dist/azure/pricing/resolvers/__tests__/disk.test.js.map +1 -0
  55. package/dist/azure/pricing/resolvers/__tests__/public-ip.test.d.ts +5 -0
  56. package/dist/azure/pricing/resolvers/__tests__/public-ip.test.d.ts.map +1 -0
  57. package/dist/azure/pricing/resolvers/__tests__/public-ip.test.js +113 -0
  58. package/dist/azure/pricing/resolvers/__tests__/public-ip.test.js.map +1 -0
  59. package/dist/azure/pricing/resolvers/__tests__/vm.test.d.ts +9 -0
  60. package/dist/azure/pricing/resolvers/__tests__/vm.test.d.ts.map +1 -0
  61. package/dist/azure/pricing/resolvers/__tests__/vm.test.js +204 -0
  62. package/dist/azure/pricing/resolvers/__tests__/vm.test.js.map +1 -0
  63. package/dist/azure/pricing/resolvers/app-service.d.ts +27 -0
  64. package/dist/azure/pricing/resolvers/app-service.d.ts.map +1 -0
  65. package/dist/azure/pricing/resolvers/app-service.js +67 -0
  66. package/dist/azure/pricing/resolvers/app-service.js.map +1 -0
  67. package/dist/azure/pricing/resolvers/disk.d.ts +59 -0
  68. package/dist/azure/pricing/resolvers/disk.d.ts.map +1 -0
  69. package/dist/azure/pricing/resolvers/disk.js +140 -0
  70. package/dist/azure/pricing/resolvers/disk.js.map +1 -0
  71. package/dist/azure/pricing/resolvers/index.d.ts +12 -0
  72. package/dist/azure/pricing/resolvers/index.d.ts.map +1 -0
  73. package/dist/azure/pricing/resolvers/index.js +8 -0
  74. package/dist/azure/pricing/resolvers/index.js.map +1 -0
  75. package/dist/azure/pricing/resolvers/public-ip.d.ts +32 -0
  76. package/dist/azure/pricing/resolvers/public-ip.d.ts.map +1 -0
  77. package/dist/azure/pricing/resolvers/public-ip.js +56 -0
  78. package/dist/azure/pricing/resolvers/public-ip.js.map +1 -0
  79. package/dist/azure/pricing/resolvers/vm.d.ts +51 -0
  80. package/dist/azure/pricing/resolvers/vm.d.ts.map +1 -0
  81. package/dist/azure/pricing/resolvers/vm.js +89 -0
  82. package/dist/azure/pricing/resolvers/vm.js.map +1 -0
  83. package/dist/azure/pricing/schema.d.ts +88 -0
  84. package/dist/azure/pricing/schema.d.ts.map +1 -0
  85. package/dist/azure/pricing/schema.js +64 -0
  86. package/dist/azure/pricing/schema.js.map +1 -0
  87. package/dist/azure/pricing/test-cache-dir.d.ts +3 -0
  88. package/dist/azure/pricing/test-cache-dir.d.ts.map +1 -0
  89. package/dist/azure/pricing/test-cache-dir.js +17 -0
  90. package/dist/azure/pricing/test-cache-dir.js.map +1 -0
  91. package/dist/azure/report.d.ts.map +1 -1
  92. package/dist/azure/report.js +57 -18
  93. package/dist/azure/report.js.map +1 -1
  94. package/dist/azure/resources/__tests__/app-service.test.d.ts +5 -0
  95. package/dist/azure/resources/__tests__/app-service.test.d.ts.map +1 -0
  96. package/dist/azure/resources/__tests__/app-service.test.js +105 -0
  97. package/dist/azure/resources/__tests__/app-service.test.js.map +1 -0
  98. package/dist/azure/resources/app-service.d.ts +8 -3
  99. package/dist/azure/resources/app-service.d.ts.map +1 -1
  100. package/dist/azure/resources/app-service.js +35 -2
  101. package/dist/azure/resources/app-service.js.map +1 -1
  102. package/dist/azure/resources/disk.d.ts +3 -1
  103. package/dist/azure/resources/disk.d.ts.map +1 -1
  104. package/dist/azure/resources/disk.js +35 -3
  105. package/dist/azure/resources/disk.js.map +1 -1
  106. package/dist/azure/resources/public-ip.d.ts +3 -1
  107. package/dist/azure/resources/public-ip.d.ts.map +1 -1
  108. package/dist/azure/resources/public-ip.js +41 -3
  109. package/dist/azure/resources/public-ip.js.map +1 -1
  110. package/dist/azure/resources/vm.d.ts +4 -1
  111. package/dist/azure/resources/vm.d.ts.map +1 -1
  112. package/dist/azure/resources/vm.js +49 -6
  113. package/dist/azure/resources/vm.js.map +1 -1
  114. package/dist/azure/types.d.ts +13 -0
  115. package/dist/azure/types.d.ts.map +1 -1
  116. package/dist/finding.d.ts +3 -3
  117. package/dist/finding.d.ts.map +1 -1
  118. package/dist/finding.js +7 -0
  119. package/dist/finding.js.map +1 -1
  120. package/dist/index.d.ts +2 -2
  121. package/dist/index.d.ts.map +1 -1
  122. package/dist/index.js.map +1 -1
  123. package/dist/types.d.ts +8 -0
  124. package/dist/types.d.ts.map +1 -1
  125. package/dist/types.js +2 -0
  126. package/dist/types.js.map +1 -1
  127. package/package.json +9 -9
  128. package/src/__tests__/finding.test.ts +24 -0
  129. package/src/azure/__tests__/analyzer-tags.test.ts +36 -1
  130. package/src/azure/__tests__/config.test.ts +12 -0
  131. package/src/azure/__tests__/report.test.ts +78 -0
  132. package/src/azure/analyzer.ts +67 -43
  133. package/src/azure/analyzers/registry.ts +8 -2
  134. package/src/azure/analyzers/types.ts +6 -0
  135. package/src/azure/pricing/__tests__/cache.test.ts +67 -0
  136. package/src/azure/pricing/__tests__/client.test.ts +212 -0
  137. package/src/azure/pricing/__tests__/fixtures/vm-b2s-westeurope.json +51 -0
  138. package/src/azure/pricing/__tests__/pricing-service.test.ts +315 -0
  139. package/src/azure/pricing/cache.ts +92 -0
  140. package/src/azure/pricing/client.ts +137 -0
  141. package/src/azure/pricing/index.ts +6 -0
  142. package/src/azure/pricing/pricing-service.ts +125 -0
  143. package/src/azure/pricing/resolvers/__tests__/app-service.test.ts +154 -0
  144. package/src/azure/pricing/resolvers/__tests__/disk.test.ts +189 -0
  145. package/src/azure/pricing/resolvers/__tests__/public-ip.test.ts +130 -0
  146. package/src/azure/pricing/resolvers/__tests__/vm.test.ts +232 -0
  147. package/src/azure/pricing/resolvers/app-service.ts +102 -0
  148. package/src/azure/pricing/resolvers/disk.ts +170 -0
  149. package/src/azure/pricing/resolvers/index.ts +31 -0
  150. package/src/azure/pricing/resolvers/public-ip.ts +87 -0
  151. package/src/azure/pricing/resolvers/vm.ts +125 -0
  152. package/src/azure/pricing/schema.ts +69 -0
  153. package/src/azure/pricing/test-cache-dir.ts +26 -0
  154. package/src/azure/report.ts +86 -21
  155. package/src/azure/resources/__tests__/app-service.test.ts +165 -0
  156. package/src/azure/resources/app-service.ts +65 -4
  157. package/src/azure/resources/disk.ts +46 -4
  158. package/src/azure/resources/public-ip.ts +61 -2
  159. package/src/azure/resources/vm.ts +89 -11
  160. package/src/azure/types.ts +14 -0
  161. package/src/finding.ts +10 -3
  162. package/src/index.ts +3 -3
  163. package/src/types.ts +12 -0
@@ -0,0 +1,154 @@
1
+ /**
2
+ * Tests for the App Service Plan Retail Prices resolver.
3
+ */
4
+
5
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
6
+
7
+ import type { FetchLike } from "../../client.js";
8
+ import type { PricingResponse } from "../../schema.js";
9
+
10
+ import { DiskCache } from "../../cache.js";
11
+ import { PricingClient } from "../../client.js";
12
+ import { makeTestCacheDir, removeTestCacheDir } from "../../test-cache-dir.js";
13
+ import { resolveAppServicePlanMonthlyPrice } from "../app-service.js";
14
+ import { HOURS_PER_MONTH } from "../vm.js";
15
+
16
+ function makeFetch(body: PricingResponse): FetchLike {
17
+ return vi.fn<FetchLike>().mockResolvedValue({
18
+ json: async () => body,
19
+ ok: true,
20
+ status: 200,
21
+ statusText: "OK",
22
+ });
23
+ }
24
+
25
+ let dir: string;
26
+
27
+ beforeEach(async () => {
28
+ dir = await makeTestCacheDir("app-service-resolver");
29
+ });
30
+
31
+ afterEach(async () => {
32
+ await removeTestCacheDir(dir);
33
+ });
34
+
35
+ describe("resolveAppServicePlanMonthlyPrice", () => {
36
+ it("returns the monthly cost for the matching Linux plan SKU and worker count", async () => {
37
+ const body: PricingResponse = {
38
+ Items: [
39
+ {
40
+ armRegionName: "westeurope",
41
+ currencyCode: "EUR",
42
+ meterName: "S1 App",
43
+ productName: "Azure App Service Standard Plan",
44
+ retailPrice: 0.087758,
45
+ skuName: "S1",
46
+ type: "Consumption",
47
+ unitOfMeasure: "1 Hour",
48
+ unitPrice: 0.087758,
49
+ },
50
+ {
51
+ armRegionName: "westeurope",
52
+ currencyCode: "EUR",
53
+ meterName: "S1 App",
54
+ productName: "Azure App Service Standard Plan - Linux",
55
+ retailPrice: 0.08337,
56
+ skuName: "S1",
57
+ type: "Consumption",
58
+ unitOfMeasure: "1 Hour",
59
+ unitPrice: 0.08337,
60
+ },
61
+ ],
62
+ NextPageLink: null,
63
+ };
64
+ const client = new PricingClient({
65
+ cache: new DiskCache({ dir }),
66
+ fetch: makeFetch(body),
67
+ });
68
+
69
+ const money = await resolveAppServicePlanMonthlyPrice(client, {
70
+ armRegionName: "westeurope",
71
+ os: "linux",
72
+ skuName: "S1",
73
+ workerCount: 2,
74
+ });
75
+
76
+ expect(money).toEqual({
77
+ amount: Math.round(0.08337 * HOURS_PER_MONTH * 2 * 100) / 100,
78
+ currency: "EUR",
79
+ });
80
+ });
81
+
82
+ it("matches Retail Prices SKU names with inconsistent spacing", async () => {
83
+ const body: PricingResponse = {
84
+ Items: [
85
+ {
86
+ armRegionName: "westeurope",
87
+ currencyCode: "EUR",
88
+ meterName: "P2 v3 App",
89
+ productName: "Azure App Service Premium v3 Plan",
90
+ retailPrice: 0.593243,
91
+ skuName: "P2 v3",
92
+ type: "Consumption",
93
+ unitOfMeasure: "1 Hour",
94
+ unitPrice: 0.593243,
95
+ },
96
+ ],
97
+ NextPageLink: null,
98
+ };
99
+ const client = new PricingClient({
100
+ cache: new DiskCache({ dir }),
101
+ fetch: makeFetch(body),
102
+ });
103
+
104
+ const money = await resolveAppServicePlanMonthlyPrice(client, {
105
+ armRegionName: "West Europe",
106
+ os: "windows",
107
+ skuName: "P2v3",
108
+ });
109
+
110
+ expect(money?.amount).toBeCloseTo(0.593243 * HOURS_PER_MONTH, 2);
111
+ });
112
+
113
+ it("ignores non-plan and non-consumption meters", async () => {
114
+ const body: PricingResponse = {
115
+ Items: [
116
+ {
117
+ armRegionName: "westeurope",
118
+ currencyCode: "EUR",
119
+ meterName: "Standard Bandwidth Usage",
120
+ productName: "Static Web Apps",
121
+ retailPrice: 0.175516,
122
+ skuName: "Standard",
123
+ type: "Consumption",
124
+ unitOfMeasure: "1 GB",
125
+ unitPrice: 0.175516,
126
+ },
127
+ {
128
+ armRegionName: "westeurope",
129
+ currencyCode: "EUR",
130
+ meterName: "S1 App",
131
+ productName: "Azure App Service Standard Plan",
132
+ reservationTerm: "1 Year",
133
+ retailPrice: 640,
134
+ skuName: "S1",
135
+ type: "Reservation",
136
+ unitOfMeasure: "1 Hour",
137
+ unitPrice: 640,
138
+ },
139
+ ],
140
+ NextPageLink: null,
141
+ };
142
+ const client = new PricingClient({
143
+ cache: new DiskCache({ dir }),
144
+ fetch: makeFetch(body),
145
+ });
146
+
147
+ const money = await resolveAppServicePlanMonthlyPrice(client, {
148
+ armRegionName: "westeurope",
149
+ skuName: "S1",
150
+ });
151
+
152
+ expect(money).toBeUndefined();
153
+ });
154
+ });
@@ -0,0 +1,189 @@
1
+ /**
2
+ * Tests for the Managed Disk Retail Prices resolver.
3
+ */
4
+
5
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
6
+
7
+ import type { FetchLike } from "../../client.js";
8
+ import type { PricingResponse } from "../../schema.js";
9
+
10
+ import { DiskCache } from "../../cache.js";
11
+ import { PricingClient } from "../../client.js";
12
+ import { makeTestCacheDir, removeTestCacheDir } from "../../test-cache-dir.js";
13
+ import { pickTier, resolveDiskMonthlyPrice } from "../disk.js";
14
+
15
+ function makeFetch(body: PricingResponse): {
16
+ fetch: FetchLike;
17
+ spy: ReturnType<typeof vi.fn>;
18
+ } {
19
+ const spy = vi.fn<FetchLike>().mockResolvedValue({
20
+ json: async () => body,
21
+ ok: true,
22
+ status: 200,
23
+ statusText: "OK",
24
+ });
25
+ return { fetch: spy, spy };
26
+ }
27
+
28
+ let dir: string;
29
+
30
+ beforeEach(async () => {
31
+ dir = await makeTestCacheDir("disk-resolver");
32
+ });
33
+
34
+ afterEach(async () => {
35
+ await removeTestCacheDir(dir);
36
+ });
37
+
38
+ describe("pickTier", () => {
39
+ it.each([
40
+ [1, 1],
41
+ [4, 1],
42
+ [5, 2],
43
+ [128, 10],
44
+ [129, 15],
45
+ [1024, 30],
46
+ [32768, 80],
47
+ ])("maps %i GiB to tier %i", (sizeGiB, tier) => {
48
+ expect(pickTier(sizeGiB)).toBe(tier);
49
+ });
50
+
51
+ it("returns undefined for sizes above the largest tier", () => {
52
+ expect(pickTier(32769)).toBeUndefined();
53
+ });
54
+ });
55
+
56
+ describe("resolveDiskMonthlyPrice", () => {
57
+ it("resolves the monthly price for a Premium_LRS P10 disk", async () => {
58
+ const body: PricingResponse = {
59
+ Items: [
60
+ {
61
+ armRegionName: "westeurope",
62
+ currencyCode: "EUR",
63
+ meterName: "P10 LRS Disk",
64
+ productName: "Premium SSD Managed Disks",
65
+ retailPrice: 19.71,
66
+ skuName: "P10 LRS",
67
+ type: "Consumption",
68
+ unitOfMeasure: "1/Month",
69
+ unitPrice: 19.71,
70
+ },
71
+ ],
72
+ NextPageLink: null,
73
+ };
74
+ const { fetch, spy } = makeFetch(body);
75
+ const client = new PricingClient({
76
+ cache: new DiskCache({ dir }),
77
+ fetch,
78
+ });
79
+
80
+ const money = await resolveDiskMonthlyPrice(client, {
81
+ armRegionName: "westeurope",
82
+ diskSizeGiB: 128,
83
+ sku: "Premium_LRS",
84
+ });
85
+
86
+ expect(money).toEqual({ amount: 19.71, currency: "EUR" });
87
+ expect(spy).toHaveBeenCalledTimes(1);
88
+ // The OData filter must target the tier-derived skuName, not the raw
89
+ // managed-disk SKU. URLSearchParams encodes spaces as `+`.
90
+ const url = String(spy.mock.calls[0]?.[0]);
91
+ expect(url).toContain("skuName+eq+%27P10+LRS%27");
92
+ expect(url).toContain("productName+eq+%27Premium+SSD+Managed+Disks%27");
93
+ });
94
+
95
+ it("uses the ZRS product/replication for Premium_ZRS", async () => {
96
+ const body: PricingResponse = {
97
+ Items: [
98
+ {
99
+ armRegionName: "westeurope",
100
+ currencyCode: "EUR",
101
+ meterName: "P10 ZRS Disk",
102
+ productName: "Premium SSD Managed Disks",
103
+ retailPrice: 24.64,
104
+ skuName: "P10 ZRS",
105
+ type: "Consumption",
106
+ unitOfMeasure: "1/Month",
107
+ unitPrice: 24.64,
108
+ },
109
+ ],
110
+ NextPageLink: null,
111
+ };
112
+ const { fetch, spy } = makeFetch(body);
113
+ const client = new PricingClient({
114
+ cache: new DiskCache({ dir }),
115
+ fetch,
116
+ });
117
+
118
+ const money = await resolveDiskMonthlyPrice(client, {
119
+ armRegionName: "westeurope",
120
+ diskSizeGiB: 128,
121
+ sku: "Premium_ZRS",
122
+ });
123
+
124
+ expect(money).toEqual({ amount: 24.64, currency: "EUR" });
125
+ const url = String(spy.mock.calls[0]?.[0]);
126
+ expect(url).toContain("skuName+eq+%27P10+ZRS%27");
127
+ });
128
+
129
+ it("returns undefined when the response has no matching entry", async () => {
130
+ const body: PricingResponse = {
131
+ Items: [
132
+ {
133
+ armRegionName: "westeurope",
134
+ currencyCode: "EUR",
135
+ // Per-GB-month item; resolver must skip it because of unitOfMeasure.
136
+ productName: "Premium SSD Managed Disks",
137
+ retailPrice: 0.1,
138
+ skuName: "P10 LRS",
139
+ type: "Consumption",
140
+ unitOfMeasure: "1 GB/Month",
141
+ unitPrice: 0.1,
142
+ },
143
+ ],
144
+ NextPageLink: null,
145
+ };
146
+ const client = new PricingClient({
147
+ cache: new DiskCache({ dir }),
148
+ fetch: makeFetch(body).fetch,
149
+ });
150
+
151
+ const money = await resolveDiskMonthlyPrice(client, {
152
+ armRegionName: "westeurope",
153
+ diskSizeGiB: 128,
154
+ sku: "Premium_LRS",
155
+ });
156
+
157
+ expect(money).toBeUndefined();
158
+ });
159
+
160
+ it("returns undefined when the disk size exceeds the largest tier", async () => {
161
+ const client = new PricingClient({
162
+ cache: new DiskCache({ dir }),
163
+ fetch: vi.fn<FetchLike>(),
164
+ });
165
+
166
+ const money = await resolveDiskMonthlyPrice(client, {
167
+ armRegionName: "westeurope",
168
+ diskSizeGiB: 100_000,
169
+ sku: "Premium_LRS",
170
+ });
171
+
172
+ expect(money).toBeUndefined();
173
+ });
174
+
175
+ it("rejects invalid input via zod", async () => {
176
+ const client = new PricingClient({
177
+ cache: new DiskCache({ dir }),
178
+ fetch: vi.fn<FetchLike>(),
179
+ });
180
+
181
+ await expect(
182
+ resolveDiskMonthlyPrice(client, {
183
+ armRegionName: "westeurope",
184
+ diskSizeGiB: -1,
185
+ sku: "Premium_LRS",
186
+ }),
187
+ ).rejects.toThrow();
188
+ });
189
+ });
@@ -0,0 +1,130 @@
1
+ /**
2
+ * Tests for the Public IP Retail Prices resolver.
3
+ */
4
+
5
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
6
+
7
+ import type { FetchLike } from "../../client.js";
8
+ import type { PricingResponse } from "../../schema.js";
9
+
10
+ import { DiskCache } from "../../cache.js";
11
+ import { PricingClient } from "../../client.js";
12
+ import { makeTestCacheDir, removeTestCacheDir } from "../../test-cache-dir.js";
13
+ import { resolvePublicIpMonthlyPrice } from "../public-ip.js";
14
+ import { HOURS_PER_MONTH } from "../vm.js";
15
+
16
+ function makeFetch(body: PricingResponse): FetchLike {
17
+ return vi.fn<FetchLike>().mockResolvedValue({
18
+ json: async () => body,
19
+ ok: true,
20
+ status: 200,
21
+ statusText: "OK",
22
+ });
23
+ }
24
+
25
+ let dir: string;
26
+
27
+ beforeEach(async () => {
28
+ dir = await makeTestCacheDir("public-ip-resolver");
29
+ });
30
+
31
+ afterEach(async () => {
32
+ await removeTestCacheDir(dir);
33
+ });
34
+
35
+ describe("resolvePublicIpMonthlyPrice", () => {
36
+ it("returns the monthly cost of a Standard static Public IP", async () => {
37
+ const body: PricingResponse = {
38
+ Items: [
39
+ {
40
+ armRegionName: "westeurope",
41
+ currencyCode: "EUR",
42
+ meterName: "Standard Static IP Address",
43
+ productName: "Virtual Network",
44
+ retailPrice: 0.004,
45
+ skuName: "Standard",
46
+ type: "Consumption",
47
+ unitOfMeasure: "1 Hour",
48
+ unitPrice: 0.004,
49
+ },
50
+ ],
51
+ NextPageLink: null,
52
+ };
53
+ const client = new PricingClient({
54
+ cache: new DiskCache({ dir }),
55
+ fetch: makeFetch(body),
56
+ });
57
+
58
+ const money = await resolvePublicIpMonthlyPrice(client, {
59
+ allocation: "static",
60
+ armRegionName: "westeurope",
61
+ sku: "standard",
62
+ });
63
+
64
+ expect(money).toEqual({
65
+ amount: Math.round(0.004 * HOURS_PER_MONTH * 100) / 100,
66
+ currency: "EUR",
67
+ });
68
+ });
69
+
70
+ it("filters by allocation token in meterName", async () => {
71
+ const body: PricingResponse = {
72
+ Items: [
73
+ {
74
+ armRegionName: "westeurope",
75
+ currencyCode: "EUR",
76
+ meterName: "Basic Dynamic IP Address",
77
+ productName: "Virtual Network",
78
+ retailPrice: 0.0036,
79
+ skuName: "Basic",
80
+ type: "Consumption",
81
+ unitOfMeasure: "1 Hour",
82
+ unitPrice: 0.0036,
83
+ },
84
+ {
85
+ armRegionName: "westeurope",
86
+ currencyCode: "EUR",
87
+ meterName: "Basic Static IP Address",
88
+ productName: "Virtual Network",
89
+ retailPrice: 0.0036,
90
+ skuName: "Basic",
91
+ type: "Consumption",
92
+ unitOfMeasure: "1 Hour",
93
+ unitPrice: 0.0036,
94
+ },
95
+ ],
96
+ NextPageLink: null,
97
+ };
98
+ const client = new PricingClient({
99
+ cache: new DiskCache({ dir }),
100
+ fetch: makeFetch(body),
101
+ });
102
+
103
+ const money = await resolvePublicIpMonthlyPrice(client, {
104
+ allocation: "dynamic",
105
+ armRegionName: "westeurope",
106
+ sku: "basic",
107
+ });
108
+
109
+ expect(money?.amount).toBeCloseTo(0.0036 * HOURS_PER_MONTH, 2);
110
+ });
111
+
112
+ it("returns undefined when no meter matches", async () => {
113
+ const body: PricingResponse = {
114
+ Items: [],
115
+ NextPageLink: null,
116
+ };
117
+ const client = new PricingClient({
118
+ cache: new DiskCache({ dir }),
119
+ fetch: makeFetch(body),
120
+ });
121
+
122
+ const money = await resolvePublicIpMonthlyPrice(client, {
123
+ allocation: "static",
124
+ armRegionName: "westeurope",
125
+ sku: "standard",
126
+ });
127
+
128
+ expect(money).toBeUndefined();
129
+ });
130
+ });
@@ -0,0 +1,232 @@
1
+ /**
2
+ * Tests for the VM Retail Prices resolver.
3
+ *
4
+ * The Retail Prices API is mocked via the `FetchLike` indirection so the
5
+ * tests run hermetically. Fixtures live next to the tests to keep the
6
+ * surface explicit.
7
+ */
8
+
9
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
10
+
11
+ import type { FetchLike } from "../../client.js";
12
+ import type { PricingResponse } from "../../schema.js";
13
+
14
+ import { DiskCache } from "../../cache.js";
15
+ import { PricingClient } from "../../client.js";
16
+ import { makeTestCacheDir, removeTestCacheDir } from "../../test-cache-dir.js";
17
+ import {
18
+ HOURS_PER_MONTH,
19
+ normalizeArmRegion,
20
+ resolveVmMonthlyPrice,
21
+ } from "../vm.js";
22
+
23
+ function makeFetch(body: PricingResponse): FetchLike {
24
+ return vi.fn<FetchLike>().mockResolvedValue({
25
+ json: async () => body,
26
+ ok: true,
27
+ status: 200,
28
+ statusText: "OK",
29
+ });
30
+ }
31
+
32
+ let dir: string;
33
+
34
+ beforeEach(async () => {
35
+ dir = await makeTestCacheDir("vm-resolver");
36
+ });
37
+
38
+ afterEach(async () => {
39
+ await removeTestCacheDir(dir);
40
+ });
41
+
42
+ describe("normalizeArmRegion", () => {
43
+ it.each([
44
+ ["westeurope", "westeurope"],
45
+ ["West Europe", "westeurope"],
46
+ ["WEST EUROPE", "westeurope"],
47
+ [" West Europe ", "westeurope"],
48
+ ["italynorth", "italynorth"],
49
+ ])("normalises %j → %j", (input, expected) => {
50
+ expect(normalizeArmRegion(input)).toBe(expected);
51
+ });
52
+ });
53
+
54
+ describe("resolveVmMonthlyPrice", () => {
55
+ it("picks the cheapest Linux consumption hourly item and returns 730×price", async () => {
56
+ const body: PricingResponse = {
57
+ Items: [
58
+ {
59
+ armRegionName: "westeurope",
60
+ armSkuName: "Standard_B2s",
61
+ currencyCode: "EUR",
62
+ meterName: "B2s",
63
+ productName: "Virtual Machines BS Series",
64
+ retailPrice: 0.05,
65
+ skuName: "B2s",
66
+ type: "Consumption",
67
+ unitOfMeasure: "1 Hour",
68
+ unitPrice: 0.05,
69
+ },
70
+ {
71
+ armRegionName: "westeurope",
72
+ armSkuName: "Standard_B2s",
73
+ currencyCode: "EUR",
74
+ meterName: "B2s",
75
+ productName: "Virtual Machines BS Series Windows",
76
+ retailPrice: 0.09,
77
+ skuName: "B2s",
78
+ type: "Consumption",
79
+ unitOfMeasure: "1 Hour",
80
+ unitPrice: 0.09,
81
+ },
82
+ ],
83
+ NextPageLink: null,
84
+ };
85
+ const client = new PricingClient({
86
+ cache: new DiskCache({ dir }),
87
+ fetch: makeFetch(body),
88
+ });
89
+
90
+ const money = await resolveVmMonthlyPrice(client, {
91
+ armRegionName: "westeurope",
92
+ armSkuName: "Standard_B2s",
93
+ });
94
+
95
+ expect(money).toEqual({
96
+ amount: Math.round(0.05 * HOURS_PER_MONTH * 100) / 100,
97
+ currency: "EUR",
98
+ });
99
+ });
100
+
101
+ it("picks the Windows item when os hint is 'windows'", async () => {
102
+ const body: PricingResponse = {
103
+ Items: [
104
+ {
105
+ armRegionName: "westeurope",
106
+ armSkuName: "Standard_B2s",
107
+ currencyCode: "EUR",
108
+ productName: "Virtual Machines BS Series",
109
+ retailPrice: 0.05,
110
+ skuName: "B2s",
111
+ type: "Consumption",
112
+ unitOfMeasure: "1 Hour",
113
+ unitPrice: 0.05,
114
+ },
115
+ {
116
+ armRegionName: "westeurope",
117
+ armSkuName: "Standard_B2s",
118
+ currencyCode: "EUR",
119
+ productName: "Virtual Machines BS Series Windows",
120
+ retailPrice: 0.09,
121
+ skuName: "B2s",
122
+ type: "Consumption",
123
+ unitOfMeasure: "1 Hour",
124
+ unitPrice: 0.09,
125
+ },
126
+ ],
127
+ NextPageLink: null,
128
+ };
129
+ const client = new PricingClient({
130
+ cache: new DiskCache({ dir }),
131
+ fetch: makeFetch(body),
132
+ });
133
+
134
+ const money = await resolveVmMonthlyPrice(client, {
135
+ armRegionName: "westeurope",
136
+ armSkuName: "Standard_B2s",
137
+ os: "windows",
138
+ });
139
+
140
+ expect(money?.amount).toBeCloseTo(0.09 * HOURS_PER_MONTH, 2);
141
+ });
142
+
143
+ it("skips Spot / Low Priority items", async () => {
144
+ const body: PricingResponse = {
145
+ Items: [
146
+ {
147
+ armRegionName: "westeurope",
148
+ armSkuName: "Standard_B2s",
149
+ currencyCode: "EUR",
150
+ productName: "Virtual Machines BS Series",
151
+ retailPrice: 0.01,
152
+ skuName: "B2s Spot",
153
+ type: "Consumption",
154
+ unitOfMeasure: "1 Hour",
155
+ unitPrice: 0.01,
156
+ },
157
+ {
158
+ armRegionName: "westeurope",
159
+ armSkuName: "Standard_B2s",
160
+ currencyCode: "EUR",
161
+ productName: "Virtual Machines BS Series",
162
+ retailPrice: 0.05,
163
+ skuName: "B2s",
164
+ type: "Consumption",
165
+ unitOfMeasure: "1 Hour",
166
+ unitPrice: 0.05,
167
+ },
168
+ ],
169
+ NextPageLink: null,
170
+ };
171
+ const client = new PricingClient({
172
+ cache: new DiskCache({ dir }),
173
+ fetch: makeFetch(body),
174
+ });
175
+
176
+ const money = await resolveVmMonthlyPrice(client, {
177
+ armRegionName: "westeurope",
178
+ armSkuName: "Standard_B2s",
179
+ });
180
+
181
+ expect(money?.amount).toBeCloseTo(0.05 * HOURS_PER_MONTH, 2);
182
+ });
183
+
184
+ it("ignores Reservation entries", async () => {
185
+ const body: PricingResponse = {
186
+ Items: [
187
+ {
188
+ armRegionName: "westeurope",
189
+ armSkuName: "Standard_B2s",
190
+ currencyCode: "EUR",
191
+ productName: "Virtual Machines BS Series",
192
+ reservationTerm: "1 Year",
193
+ retailPrice: 0.03,
194
+ skuName: "B2s",
195
+ type: "Reservation",
196
+ unitOfMeasure: "1 Hour",
197
+ unitPrice: 0.03,
198
+ },
199
+ ],
200
+ NextPageLink: null,
201
+ };
202
+ const client = new PricingClient({
203
+ cache: new DiskCache({ dir }),
204
+ fetch: makeFetch(body),
205
+ });
206
+
207
+ const money = await resolveVmMonthlyPrice(client, {
208
+ armRegionName: "westeurope",
209
+ armSkuName: "Standard_B2s",
210
+ });
211
+
212
+ expect(money).toBeUndefined();
213
+ });
214
+
215
+ it("returns undefined when no items match", async () => {
216
+ const body: PricingResponse = {
217
+ Items: [],
218
+ NextPageLink: null,
219
+ };
220
+ const client = new PricingClient({
221
+ cache: new DiskCache({ dir }),
222
+ fetch: makeFetch(body),
223
+ });
224
+
225
+ const money = await resolveVmMonthlyPrice(client, {
226
+ armRegionName: "westeurope",
227
+ armSkuName: "Standard_B2s",
228
+ });
229
+
230
+ expect(money).toBeUndefined();
231
+ });
232
+ });