@speedkit/cli 4.29.0 → 4.31.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 (62) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/README.md +1 -1
  3. package/dist/services/customer-config/customer-config-service-model.d.ts +6 -0
  4. package/dist/services/customer-config/customer-config-service-model.js +1 -0
  5. package/dist/services/customer-config/customer-config-service.d.ts +5 -0
  6. package/dist/services/customer-config/customer-config-service.js +15 -0
  7. package/dist/services/deploy/checks/activation/customer-approval-check.d.ts +21 -0
  8. package/dist/services/deploy/checks/activation/customer-approval-check.js +62 -0
  9. package/dist/services/deploy/checks/activation/customer-approval-check.spec.js +64 -0
  10. package/dist/services/deploy/checks/pre-deploy/device-detection-changed-check.js +9 -7
  11. package/dist/services/deploy/checks/pre-deploy/split-change-check.js +7 -6
  12. package/dist/services/deploy/deploy-service-factory.js +2 -1
  13. package/dist/services/deploy/handler/install-resource-handler.d.ts +7 -1
  14. package/dist/services/deploy/handler/install-resource-handler.js +40 -19
  15. package/dist/services/deploy/handler/testing-installation-links.d.ts +10 -0
  16. package/dist/services/deploy/handler/testing-installation-links.js +27 -0
  17. package/dist/services/deploy/handler/testing-installation-links.spec.d.ts +1 -0
  18. package/dist/services/deploy/handler/testing-installation-links.spec.js +43 -0
  19. package/dist/services/integration-api/file-list.d.ts +2 -1
  20. package/dist/services/integration-api/file-list.js +7 -1
  21. package/dist/services/integration-api/handler/install-resource-handler.js +10 -2
  22. package/dist/services/integration-api/integration-api-model.d.ts +8 -0
  23. package/dist/services/integration-api/integration-api-model.js +3 -0
  24. package/dist/services/integration-api/spec/config-plugin.spec.d.ts +1 -0
  25. package/dist/services/integration-api/spec/config-plugin.spec.js +188 -0
  26. package/dist/services/integration-api/spec/speed-kit-config-file.spec.d.ts +1 -0
  27. package/dist/{helpers/evaluate-speed-kit-config.spec.js → services/integration-api/spec/speed-kit-config-file.spec.js} +43 -12
  28. package/dist/services/integration-api/spec/speed-kit-config.spec.d.ts +1 -0
  29. package/dist/services/integration-api/spec/speed-kit-config.spec.js +108 -0
  30. package/dist/services/integration-api/struct/config-plugin.d.ts +41 -0
  31. package/dist/services/integration-api/struct/config-plugin.js +241 -0
  32. package/dist/services/integration-api/struct/customer-config.d.ts +7 -1
  33. package/dist/services/integration-api/struct/customer-config.js +10 -3
  34. package/dist/services/integration-api/struct/speed-kit-config-file.d.ts +46 -0
  35. package/dist/services/integration-api/struct/speed-kit-config-file.js +223 -0
  36. package/dist/services/integration-api/struct/speed-kit-config.d.ts +84 -0
  37. package/dist/services/integration-api/struct/speed-kit-config.js +114 -0
  38. package/dist/services/integration-api/virtual/static-recipe.d.ts +1 -1
  39. package/dist/services/query-builder/queries/spec/golden-configs.d.ts +2 -0
  40. package/dist/services/query-builder/queries/spec/golden-configs.js +37 -0
  41. package/dist/services/query-builder/queries/spec/golden.spec.js +5 -4
  42. package/dist/services/query-builder/queries/spec/queries.spec.js +2 -1
  43. package/dist/services/query-builder/queries/spec/reported-bugs.spec.js +2 -1
  44. package/dist/services/query-builder/query-builder-factory.js +4 -8
  45. package/dist/services/query-builder/query-builder-service.js +4 -0
  46. package/dist/services/query-builder/query-builder-service.spec.js +2 -1
  47. package/dist/services/query-builder/rules/rule-model.d.ts +6 -2
  48. package/dist/services/query-builder/rules/rule-model.js +0 -2
  49. package/dist/services/query-builder/rules/rule-parser.d.ts +7 -8
  50. package/dist/services/query-builder/rules/rule-parser.js +41 -47
  51. package/dist/services/query-builder/rules/spec/rule-parser.spec.js +30 -32
  52. package/dist/services/query-builder/rules/spec/rule-reducer.spec.js +2 -1
  53. package/dist/services/query-builder/rules/spec/rule-relevance.spec.js +7 -3
  54. package/dist/services/query-builder/rules/spec/rule-to-sql.spec.js +3 -2
  55. package/dist/services/query-builder/rules/spec/strip-parameters.spec.js +5 -2
  56. package/oclif.manifest.json +1 -1
  57. package/package.json +1 -1
  58. package/dist/helpers/evaluate-speed-kit-config.d.ts +0 -11
  59. package/dist/helpers/evaluate-speed-kit-config.js +0 -150
  60. /package/dist/{helpers/evaluate-speed-kit-config.spec.d.ts → services/deploy/checks/activation/customer-approval-check.spec.d.ts} +0 -0
  61. /package/dist/services/{deploy → integration-api}/error/evaluate-sk-config-error.d.ts +0 -0
  62. /package/dist/services/{deploy → integration-api}/error/evaluate-sk-config-error.js +0 -0
@@ -3,12 +3,16 @@ export * from "./struct/file.js";
3
3
  export * from "./struct/customer-config-file.js";
4
4
  export * from "./struct/module-file.js";
5
5
  export * from "./struct/install-resource-file.js";
6
+ export * from "./struct/speed-kit-config.js";
7
+ export * from "./struct/speed-kit-config-file.js";
6
8
  export * from "./struct/server-config-file.js";
7
9
  export * from "./struct/ro-file.js";
8
10
  export * from "./struct/virtual-file.js";
9
11
  export * from "./virtual/virtual-file-handler.js";
10
12
  export * from "./virtual/external-recipe.js";
13
+ export * from "./virtual/static-recipe.js";
11
14
  export * from "./external/external-file-reader.js";
15
+ import type { SpeedKitConfig } from "./struct/speed-kit-config.js";
12
16
  import { CustomerConfig } from "./struct/customer-config.js";
13
17
  import { RoFile } from "./struct/ro-file.js";
14
18
  import { VirtualFile } from "./struct/virtual-file.js";
@@ -29,6 +33,7 @@ export interface FileListInterface {
29
33
  getModules(): ModuleInterface[];
30
34
  getServerConfig(): ServerConfigInterface;
31
35
  getCustomerConfig(): CustomerConfigInterface;
36
+ getSpeedKitConfig(): SpeedKitConfigInterface;
32
37
  getByType<T>(type: FILE_TYPE): FileInterface[] | T[];
33
38
  getTypes(): FILE_TYPE[];
34
39
  filter(callback: (file: FileInterface) => boolean): void;
@@ -83,6 +88,9 @@ export interface ServerConfigInterface extends ConfigFileInterface, FileInterfac
83
88
  export interface InstallResourceInterface extends ConfigFileInterface, FileInterface {
84
89
  readonly remoteKey: string;
85
90
  }
91
+ export interface SpeedKitConfigInterface extends InstallResourceInterface {
92
+ evaluate(app: string, origin: string): Promise<SpeedKitConfig>;
93
+ }
86
94
  export interface CustomerConfigInterface extends ConfigFileInterface {
87
95
  config: CustomerConfig;
88
96
  updateOrigins(origins: string[]): void;
@@ -3,11 +3,14 @@ export * from "./struct/file.js";
3
3
  export * from "./struct/customer-config-file.js";
4
4
  export * from "./struct/module-file.js";
5
5
  export * from "./struct/install-resource-file.js";
6
+ export * from "./struct/speed-kit-config.js";
7
+ export * from "./struct/speed-kit-config-file.js";
6
8
  export * from "./struct/server-config-file.js";
7
9
  export * from "./struct/ro-file.js";
8
10
  export * from "./struct/virtual-file.js";
9
11
  export * from "./virtual/virtual-file-handler.js";
10
12
  export * from "./virtual/external-recipe.js";
13
+ export * from "./virtual/static-recipe.js";
11
14
  export * from "./external/external-file-reader.js";
12
15
  export var FILE_TYPE;
13
16
  (function (FILE_TYPE) {
@@ -0,0 +1,188 @@
1
+ import * as vm from "node:vm";
2
+ import { expect } from "chai";
3
+ import { describe, it } from "mocha";
4
+ import { SpeedKitConfig } from "../struct/speed-kit-config.js";
5
+ /**
6
+ * The plugin shapes here mirror the published bundles at
7
+ * `https://www.baqend.com/speed-kit-config/latest/`: a `name`, an `options`,
8
+ * and every hook as an own instance field — which is what
9
+ * `ConfigController` reads with `Object.keys`.
10
+ */
11
+ function blacklistPlugin(options = {}) {
12
+ return {
13
+ name: "BlacklistPlugin",
14
+ options,
15
+ addBlacklist: () => [
16
+ { pathname: [/cart/i], tags: ["cart"] },
17
+ { pathname: [/checkout/i], tags: ["checkout"] },
18
+ ],
19
+ };
20
+ }
21
+ function config(evaluated, ...plugins) {
22
+ return SpeedKitConfig.createFrom(evaluated, plugins);
23
+ }
24
+ describe("config plugins", () => {
25
+ it("should add the rules a plugin contributes to the rule set", () => {
26
+ const parsed = config({ blacklist: [{ pathname: [/\/wishlist/i] }] }, blacklistPlugin());
27
+ expect(parsed.blacklist).to.have.lengthOf(3);
28
+ expect(parsed.plugins).to.deep.equal(["BlacklistPlugin"]);
29
+ });
30
+ it("should start a rule set a plugin is the only contributor to", () => {
31
+ const parsed = config({}, blacklistPlugin());
32
+ expect(parsed.blacklist).to.have.lengthOf(2);
33
+ });
34
+ it("should leave the config the file wrote untouched", () => {
35
+ const evaluated = { blacklist: [{ pathname: [/\/wishlist/i] }] };
36
+ const parsed = config(evaluated, blacklistPlugin());
37
+ expect(parsed.raw).to.equal(evaluated);
38
+ expect(evaluated.blacklist).to.have.lengthOf(1);
39
+ });
40
+ it("should prefix a plugin's tags with its name", () => {
41
+ const parsed = config({}, blacklistPlugin());
42
+ expect(parsed.blacklist[0].tags).to.deep.equal(["BlacklistPlugin-cart"]);
43
+ });
44
+ it("should drop the rules an exclude option names", () => {
45
+ const parsed = config({}, blacklistPlugin({ exclude: ["cart"] }));
46
+ expect(parsed.blacklist).to.have.lengthOf(1);
47
+ expect(parsed.blacklist[0].tags).to.deep.equal([
48
+ "BlacklistPlugin-checkout",
49
+ ]);
50
+ });
51
+ it("should accept an exclude option that carries the plugin name", () => {
52
+ const parsed = config({}, blacklistPlugin({ exclude: ["BlacklistPlugin-cart"] }));
53
+ expect(parsed.blacklist).to.have.lengthOf(1);
54
+ });
55
+ it("should refuse a blacklist rule with a key that is no condition", () => {
56
+ // The snippet drops the whole contribution rather than feed the service
57
+ // worker a rule it cannot read.
58
+ const parsed = config({ blacklist: [{ host: "a.de" }] }, {
59
+ name: "BrokenPlugin",
60
+ addBlacklist: () => [{ pathname: [/cart/i], sometimes: true }],
61
+ });
62
+ expect(parsed.blacklist).to.have.lengthOf(1);
63
+ });
64
+ it("should add the scopes a plugin disables", () => {
65
+ const parsed = config({ disabledScopes: ["/admin"] }, {
66
+ name: "ShopifyPlugin",
67
+ addDisabledScopes: () => ["/cart", "/checkout"],
68
+ });
69
+ expect(parsed.disabledScopes).to.deep.equal([
70
+ "/admin",
71
+ "/cart",
72
+ "/checkout",
73
+ ]);
74
+ });
75
+ it("should take a single disabled scope as a list of one", () => {
76
+ const parsed = config({}, {
77
+ name: "P",
78
+ addDisabledScopes: () => "/checkout",
79
+ });
80
+ expect(parsed.disabledScopes).to.deep.equal(["/checkout"]);
81
+ });
82
+ describe("stripQueryParams", () => {
83
+ const paramPlugin = {
84
+ name: "StripQueryParamPlugin",
85
+ addStripQueryParams: () => [{ params: ["gclid"], tags: ["ads"] }],
86
+ };
87
+ it("should merge two bare conditions into one", () => {
88
+ const parsed = config({ stripQueryParams: ["size"] }, {
89
+ name: "P",
90
+ addStripQueryParams: () => ["gclid", "fbclid"],
91
+ });
92
+ expect(parsed.stripQueryParams).to.deep.equal([
93
+ "size",
94
+ "gclid",
95
+ "fbclid",
96
+ ]);
97
+ });
98
+ it("should turn a bare condition into rules when the plugin brings rules", () => {
99
+ // Conditions and rules must not be mixed — the service worker converts
100
+ // the property as a whole.
101
+ const parsed = config({ stripQueryParams: ["size"] }, paramPlugin);
102
+ expect(parsed.stripQueryParams).to.deep.equal([
103
+ { params: ["size"] },
104
+ { params: ["gclid"], tags: ["StripQueryParamPlugin-ads"] },
105
+ ]);
106
+ });
107
+ it("should append to the rules a config already has", () => {
108
+ const parsed = config({ stripQueryParams: [{ params: ["size"] }] }, paramPlugin);
109
+ expect(parsed.stripQueryParams).to.have.lengthOf(2);
110
+ });
111
+ it("should recognise a pattern from the config's own realm", () => {
112
+ // The config is evaluated in a vm, so its regular expressions are not
113
+ // `instanceof RegExp` here. Read as rules, a bare list of them carries
114
+ // no `params` and every parameter would silently stop being stripped.
115
+ const foreign = vm.runInNewContext("[/^utm[_-]/]");
116
+ const parsed = config({ stripQueryParams: foreign }, {
117
+ name: "P",
118
+ addStripQueryParams: () => ["gclid"],
119
+ });
120
+ expect(parsed.stripQueryParams).to.have.lengthOf(2);
121
+ expect(parsed.stripQueryParams[1]).to.equal("gclid");
122
+ });
123
+ });
124
+ it("should let applyToConfig change the config directly", () => {
125
+ const parsed = config({}, {
126
+ name: "SSRPlugin",
127
+ applyToConfig: (speedKit) => {
128
+ speedKit.disabledSites = [{ pathname: [/\/print/i] }];
129
+ },
130
+ });
131
+ expect(parsed.disabledSites).to.have.lengthOf(1);
132
+ expect(parsed.plugins).to.deep.equal(["SSRPlugin"]);
133
+ });
134
+ it("should run applyToConfig after every other hook", () => {
135
+ // `BlacklistPlugin.applyToConfig` reads back what its own `addBlacklist`
136
+ // contributed, so the order is not an implementation detail.
137
+ const seen = [];
138
+ config({}, {
139
+ name: "P",
140
+ applyToConfig: (speedKit) => seen.push(speedKit.blacklist.length),
141
+ addBlacklist: () => [{ pathname: [/cart/i] }],
142
+ });
143
+ expect(seen).to.deep.equal([1]);
144
+ });
145
+ it("should show a hook the config as it was before any plugin ran", () => {
146
+ const seen = [];
147
+ const record = (speedKit) => {
148
+ seen.push(speedKit.blacklist?.length ?? 0);
149
+ return [{ pathname: [/cart/i] }];
150
+ };
151
+ config({}, { name: "First", addBlacklist: record }, { name: "Second", addBlacklist: record });
152
+ // The second plugin must not see the first one's rule.
153
+ expect(seen).to.deep.equal([0, 0]);
154
+ });
155
+ it("should keep the other plugins when one of them throws", () => {
156
+ const parsed = config({}, {
157
+ name: "BrokenPlugin",
158
+ addBlacklist: () => {
159
+ throw new Error("no");
160
+ },
161
+ }, blacklistPlugin());
162
+ expect(parsed.blacklist).to.have.lengthOf(2);
163
+ expect(parsed.plugins).to.deep.equal(["BlacklistPlugin"]);
164
+ });
165
+ it("should ignore a hook it knows nothing about", () => {
166
+ const parsed = config({}, {
167
+ name: "ShopifyPlugin",
168
+ onShopifyAnalyticsReady: () => "never called here",
169
+ });
170
+ expect(parsed.plugins).to.deep.equal([]);
171
+ });
172
+ it("should read the fields a pre-deploy check compares from the file", () => {
173
+ // `SSRPlugin` replaces `detectDevice` with a wrapper of its own. A check
174
+ // asking whether a human changed the device detection has to see the
175
+ // function in the file, not the wrapper around it.
176
+ const detectDevice = () => "mobile";
177
+ const parsed = config({ detectDevice, split: 0.5 }, {
178
+ name: "SSRPlugin",
179
+ applyToConfig: (speedKit) => {
180
+ speedKit.detectDevice = () => "wrapped";
181
+ speedKit.split = 1;
182
+ },
183
+ });
184
+ expect(parsed.detectDevice).to.equal(detectDevice);
185
+ expect(parsed.split).to.equal(0.5);
186
+ expect(parsed.effective.split).to.equal(1);
187
+ });
188
+ });
@@ -1,18 +1,20 @@
1
1
  import { expect } from "chai";
2
2
  import { describe, it } from "mocha";
3
- import { evaluateLocalSpeedKitConfig } from "./evaluate-speed-kit-config.js";
3
+ import { SpeedKitConfigFile, VirtualSpeedKitConfigFile, } from "../index.js";
4
+ import { INTEGRATION_FILES } from "../../../models/files.js";
4
5
  function configFile(source) {
5
- return {
6
- getContent: () => source,
7
- path: "config_SpeedKit.js",
8
- };
6
+ return new SpeedKitConfigFile(INTEGRATION_FILES.CONFIG.SPEED_KIT, "config_SpeedKit.js", source, "config");
9
7
  }
10
8
  function evaluate(source) {
11
- return evaluateLocalSpeedKitConfig("shop-de", "https://www.shop.de", configFile(source));
9
+ return configFile(source).evaluate("shop-de", "https://www.shop.de");
12
10
  }
13
- describe("evaluateLocalSpeedKitConfig", () => {
11
+ /** What the config assigned, for the tests about the browser mock itself. */
12
+ async function evaluateRaw(source) {
13
+ return (await evaluate(source)).raw;
14
+ }
15
+ describe("SpeedKitConfigFile", () => {
14
16
  it("should read the app and the origin the config is evaluated for", async () => {
15
- const speedKit = await evaluate(`
17
+ const speedKit = await evaluateRaw(`
16
18
  window.speedKit = { appName: APP, host: window.location.host };
17
19
  `);
18
20
  expect(speedKit.appName).to.equal("shop-de");
@@ -22,7 +24,7 @@ describe("evaluateLocalSpeedKitConfig", () => {
22
24
  // A real shape: `precache` is collected from the document's own script and
23
25
  // link tags. A `querySelectorAll` returning undefined would throw here,
24
26
  // and the whole config — every rule in it — would be unreadable.
25
- const speedKit = await evaluate(`
27
+ const speedKit = await evaluateRaw(`
26
28
  window.speedKit = {
27
29
  appName: APP,
28
30
  precache: Array.from(
@@ -41,7 +43,7 @@ describe("evaluateLocalSpeedKitConfig", () => {
41
43
  // `document.referrer` to tell a new visitor from a returning one. When
42
44
  // `window.location` was undefined this failed with
43
45
  // "Cannot read properties of undefined (reading 'hostname')".
44
- const speedKit = await evaluate(`
46
+ const speedKit = await evaluateRaw(`
45
47
  var hostname = window.location.hostname;
46
48
  var isNewUser = !document.referrer;
47
49
  window.speedKit = {
@@ -56,7 +58,7 @@ describe("evaluateLocalSpeedKitConfig", () => {
56
58
  expect(speedKit.isNewUser).to.equal(true);
57
59
  });
58
60
  it("should answer a missing element with null, the way the dom does", async () => {
59
- const speedKit = await evaluate(`
61
+ const speedKit = await evaluateRaw(`
60
62
  window.speedKit = {
61
63
  appName: APP,
62
64
  hasMarker: document.querySelector('.marker') !== null,
@@ -72,7 +74,36 @@ describe("evaluateLocalSpeedKitConfig", () => {
72
74
  expect.fail("should have thrown");
73
75
  }
74
76
  catch (error) {
75
- expect(String(error)).to.contain("config_Speedkit.js");
77
+ expect(String(error)).to.contain("config_SpeedKit.js");
76
78
  }
77
79
  });
80
+ it("should read a config once per app and origin", async () => {
81
+ // Two callers asking for the same reading get the same one — the query
82
+ // builder and the pre-deploy checks both go through here.
83
+ const file = configFile(`window.speedKit = { host: window.location.host };`);
84
+ const [first, second] = await Promise.all([
85
+ file.evaluate("shop-de", "https://www.shop.de"),
86
+ file.evaluate("shop-de", "https://www.shop.de"),
87
+ ]);
88
+ const otherOrigin = await file.evaluate("shop-de", "https://m.shop.de");
89
+ expect(first).to.equal(second);
90
+ expect(otherOrigin).to.not.equal(first);
91
+ expect(otherOrigin.raw.host).to.equal("m.shop.de");
92
+ });
93
+ it("should forget what it read when the file changes", async () => {
94
+ const file = configFile(`window.speedKit = { split: 0.1 };`);
95
+ const before = await file.evaluate("shop-de", "https://www.shop.de");
96
+ file.setContent(`window.speedKit = { split: 0.5 };`);
97
+ const after = await file.evaluate("shop-de", "https://www.shop.de");
98
+ expect(before.split).to.equal(0.1);
99
+ expect(after.split).to.equal(0.5);
100
+ });
101
+ it("should read the deployed config the same way as the local file", async () => {
102
+ const source = `window.speedKit = { split: 0.5, appName: APP };`;
103
+ const remote = await VirtualSpeedKitConfigFile.createRemote(source);
104
+ const local = await evaluate(source);
105
+ const deployed = await remote.evaluate("shop-de", "https://www.shop.de");
106
+ expect(deployed).to.deep.equal(local);
107
+ expect(remote.getContent()).to.equal(source);
108
+ });
78
109
  });
@@ -0,0 +1,108 @@
1
+ import { expect } from "chai";
2
+ import { describe, it } from "mocha";
3
+ import { SpeedKitConfig } from "../struct/speed-kit-config.js";
4
+ describe("SpeedKitConfig", () => {
5
+ describe("ruleSetVersion", () => {
6
+ it("should default to version 1", () => {
7
+ expect(SpeedKitConfig.createFrom({}).ruleSetVersion).to.equal(1);
8
+ });
9
+ it("should read the declared version", () => {
10
+ const config = SpeedKitConfig.createFrom({
11
+ ruleSetConfig: { version: 2 },
12
+ });
13
+ expect(config.ruleSetVersion).to.equal(2);
14
+ });
15
+ it("should read a version given as a string", () => {
16
+ const config = SpeedKitConfig.createFrom({
17
+ ruleSetConfig: { version: "2" },
18
+ });
19
+ expect(config.ruleSetVersion).to.equal(2);
20
+ });
21
+ it("should fall back for an unusable version", () => {
22
+ const config = SpeedKitConfig.createFrom({
23
+ ruleSetConfig: { version: "x" },
24
+ });
25
+ expect(config.ruleSetVersion).to.equal(1);
26
+ });
27
+ });
28
+ describe("rule sets", () => {
29
+ it("should give every rule set for a config that declares none", () => {
30
+ const config = SpeedKitConfig.createFrom({});
31
+ expect(config.blacklist).to.deep.equal([]);
32
+ expect(config.disabledScopes).to.deep.equal([]);
33
+ expect(config.disabledSites).to.deep.equal([]);
34
+ expect(config.enabledSites).to.deep.equal([]);
35
+ expect(config.stripQueryParams).to.deep.equal([]);
36
+ expect(config.whitelist).to.deep.equal([]);
37
+ });
38
+ it("should ignore a rule set that is not an array", () => {
39
+ const config = SpeedKitConfig.createFrom({ blacklist: "everything" });
40
+ expect(config.blacklist).to.deep.equal([]);
41
+ });
42
+ it("should drop an entry of a rule set that is not a rule", () => {
43
+ const config = SpeedKitConfig.createFrom({
44
+ whitelist: [{ host: "a.de" }, "b.de", undefined, [{ host: "c.de" }]],
45
+ });
46
+ expect(config.whitelist).to.deep.equal([{ host: "a.de" }]);
47
+ });
48
+ it("should keep the rules of a rule set as they were written", () => {
49
+ const rule = { host: ["www.shop.de"], pathname: [/^\/p\//i] };
50
+ const config = SpeedKitConfig.createFrom({ enabledSites: [rule] });
51
+ expect(config.enabledSites[0]).to.deep.equal(rule);
52
+ // A plugin may only ever add to a copy, so the evaluated config a
53
+ // caller holds is never changed under it.
54
+ expect(config.raw.enabledSites[0]).to.equal(rule);
55
+ expect(config.enabledSites[0]).to.not.equal(rule);
56
+ });
57
+ });
58
+ describe("stripQueryParams", () => {
59
+ it("should wrap a bare condition in a list", () => {
60
+ const config = SpeedKitConfig.createFrom({ stripQueryParams: "gclid" });
61
+ expect(config.stripQueryParams).to.deep.equal(["gclid"]);
62
+ });
63
+ it("should keep a list as it is", () => {
64
+ const config = SpeedKitConfig.createFrom({
65
+ stripQueryParams: [{ params: ["size"] }],
66
+ });
67
+ expect(config.stripQueryParams).to.deep.equal([{ params: ["size"] }]);
68
+ });
69
+ it("should read a missing property as nothing to strip", () => {
70
+ expect(SpeedKitConfig.createFrom({}).stripQueryParams).to.deep.equal([]);
71
+ });
72
+ });
73
+ describe("the fields the pre-deploy checks read", () => {
74
+ it("should carry the split, its id, and the device detection", () => {
75
+ const detectDevice = () => "mobile";
76
+ const variationFunction = () => "b";
77
+ const config = SpeedKitConfig.createFrom({
78
+ customVariation: [{ variationFunction }],
79
+ detectDevice,
80
+ split: 0.5,
81
+ splitTestId: "2026-09-jeans",
82
+ });
83
+ expect(config.split).to.equal(0.5);
84
+ expect(config.splitTestId).to.equal("2026-09-jeans");
85
+ expect(config.detectDevice).to.equal(detectDevice);
86
+ expect(config.customVariation[0].variationFunction).to.equal(variationFunction);
87
+ });
88
+ it("should leave customVariation undefined when there is none", () => {
89
+ expect(SpeedKitConfig.createFrom({}).customVariation).to.equal(undefined);
90
+ });
91
+ });
92
+ it("should apply no plugin when a config registers none", () => {
93
+ const config = SpeedKitConfig.createFrom({ blacklist: [{ host: "a.de" }] });
94
+ expect(config.plugins).to.deep.equal([]);
95
+ expect(config.effective).to.deep.equal(config.raw);
96
+ });
97
+ it("should keep everything the config assigned", () => {
98
+ // A config carries far more than the CLI reads — appName, precache,
99
+ // loadHandler — and nothing may be lost on the way through here.
100
+ const evaluated = { appName: "shop-de", precache: ["/app.js"] };
101
+ expect(SpeedKitConfig.createFrom(evaluated).raw).to.equal(evaluated);
102
+ });
103
+ it("should survive a config that assigned no object at all", () => {
104
+ const config = SpeedKitConfig.createFrom(undefined);
105
+ expect(config.raw).to.deep.equal({});
106
+ expect(config.whitelist).to.deep.equal([]);
107
+ });
108
+ });
@@ -0,0 +1,41 @@
1
+ import type { EvaluatedSpeedKitConfig } from "./speed-kit-config.js";
2
+ /**
3
+ * Config plugins, the way the Speed Kit snippet applies them.
4
+ *
5
+ * A config registers plugins on `window.SpeedKit.configPlugins`, and in the
6
+ * browser it is `ConfigController` — part of the snippet, not of the config —
7
+ * that folds their hooks into `window.speedKit` before Speed Kit starts. A
8
+ * plugin can therefore contribute rules that appear nowhere in
9
+ * `config_SpeedKit.js`: `BlacklistPlugin` brings the whole cart/checkout/account
10
+ * blacklist, `StripQueryParamPlugin` the tracking parameters, `ShopifyPlugin`
11
+ * the Shopify scopes.
12
+ *
13
+ * The CLI evaluates the config in a vm, where the snippet never runs, so this
14
+ * is a port of `ConfigController.executePlugins` and its three modifiers —
15
+ * `speed-kit/snippet/ConfigController.ts` and
16
+ * `speed-kit/snippet/configplugins/`. It follows that code deliberately
17
+ * closely, quirks included, because a reading that differs from production is
18
+ * worse than no reading at all. Two things are on purpose *not* copied
19
+ * verbatim: every `instanceof RegExp` becomes `types.isRegExp`, since the
20
+ * config's regular expressions come out of the vm and belong to another realm,
21
+ * where `instanceof` is false and production's answer would be inverted.
22
+ *
23
+ * Keep this in step with the snippet. If a hook is added there, it is added
24
+ * here, or the query builder quietly goes back to reading half a config.
25
+ */
26
+ /** A plugin instance as the evaluated config registered it. */
27
+ export type ConfigPluginLike = Record<string, any>;
28
+ export interface AppliedConfigPlugins {
29
+ /** The config as Speed Kit runs it: what the file wrote plus every hook. */
30
+ readonly config: EvaluatedSpeedKitConfig;
31
+ /** The names of the plugins that contributed something, in order. */
32
+ readonly plugins: readonly string[];
33
+ }
34
+ /**
35
+ * Applies the plugins a config registered, leaving the evaluated config itself
36
+ * untouched.
37
+ *
38
+ * @param evaluated what the config assigned to `window.speedKit`
39
+ * @param plugins what it registered on `window.SpeedKit.configPlugins`
40
+ */
41
+ export declare function applyConfigPlugins(evaluated: EvaluatedSpeedKitConfig, plugins: readonly ConfigPluginLike[]): AppliedConfigPlugins;