@delmaredigital/payload-experiments 0.4.1 → 0.5.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 (290) hide show
  1. package/README.md +9 -5
  2. package/dist/admin/blocks/model.js +2 -0
  3. package/dist/admin/copy/blocks.d.ts +1 -0
  4. package/dist/admin/copy/blocks.js +1 -0
  5. package/dist/admin/copy/status.d.ts +3 -0
  6. package/dist/admin/copy/status.js +3 -0
  7. package/dist/admin/placementStatus.model.js +3 -0
  8. package/dist/admin/status.model.d.ts +3 -2
  9. package/dist/admin/status.model.js +9 -3
  10. package/dist/admin/surface/ResultsRoute.js +2 -1
  11. package/dist/admin/surface/TestsHome.d.ts +2 -1
  12. package/dist/admin/surface/TestsHome.js +31 -11
  13. package/dist/admin/surface/TestsSurface.js +4 -0
  14. package/dist/admin/surface/home.model.d.ts +11 -6
  15. package/dist/admin/surface/home.model.js +19 -8
  16. package/dist/admin/surface/newTest/PageWizard.js +13 -3
  17. package/dist/admin/surface/results/ResultsHeader.js +43 -9
  18. package/dist/admin/surface/test/TestPage.js +11 -1
  19. package/dist/admin/useLayerUsage.d.ts +1 -1
  20. package/dist/admin/views/buildSurfaceProps.js +3 -0
  21. package/dist/cohorts/build.d.ts +35 -0
  22. package/dist/cohorts/build.js +134 -0
  23. package/dist/cohorts/key.d.ts +12 -0
  24. package/dist/cohorts/key.js +15 -0
  25. package/dist/cohorts/maintenance.d.ts +10 -0
  26. package/dist/cohorts/maintenance.js +70 -0
  27. package/dist/cohorts/readiness.d.ts +30 -0
  28. package/dist/cohorts/readiness.js +54 -0
  29. package/dist/cohorts/readinessData.d.ts +29 -0
  30. package/dist/cohorts/readinessData.js +123 -0
  31. package/dist/cohorts/rule.d.ts +33 -0
  32. package/dist/cohorts/rule.js +50 -0
  33. package/dist/collections/cohortMembers.d.ts +4 -0
  34. package/dist/collections/cohortMembers.js +95 -0
  35. package/dist/collections/experiments/config.js +157 -7
  36. package/dist/collections/experiments/editability.js +10 -6
  37. package/dist/collections/experiments/lockHook.js +39 -20
  38. package/dist/collections/index.js +4 -1
  39. package/dist/collections/phases.js +4 -1
  40. package/dist/constants.d.ts +2 -0
  41. package/dist/constants.js +2 -0
  42. package/dist/db/adapter.d.ts +1 -1
  43. package/dist/db/adapter.js +14 -4
  44. package/dist/db/appends.js +1 -1
  45. package/dist/db/cohortMembership.d.ts +11 -0
  46. package/dist/db/cohortMembership.js +31 -0
  47. package/dist/db/cohortPool.d.ts +9 -0
  48. package/dist/db/cohortPool.js +53 -0
  49. package/dist/db/cohortTransactions.d.ts +3 -0
  50. package/dist/db/cohortTransactions.js +52 -0
  51. package/dist/db/cohorts.d.ts +72 -0
  52. package/dist/db/cohorts.js +280 -0
  53. package/dist/db/erase.d.ts +2 -0
  54. package/dist/db/erase.js +21 -2
  55. package/dist/db/events.d.ts +2 -0
  56. package/dist/db/events.js +8 -0
  57. package/dist/db/executor.d.ts +2 -0
  58. package/dist/db/executor.js +8 -0
  59. package/dist/db/locks.d.ts +7 -0
  60. package/dist/db/locks.js +13 -1
  61. package/dist/db/names.d.ts +95 -1
  62. package/dist/db/names.js +95 -1
  63. package/dist/db/occupancy.d.ts +21 -0
  64. package/dist/db/occupancy.js +46 -0
  65. package/dist/db/pageOccupancy.d.ts +4 -4
  66. package/dist/db/pageOccupancy.js +13 -10
  67. package/dist/db/pgExecutor.d.ts +4 -0
  68. package/dist/db/pgExecutor.js +1 -1
  69. package/dist/db/placements.js +1 -1
  70. package/dist/db/results.d.ts +5 -2
  71. package/dist/db/results.js +32 -5
  72. package/dist/db/slices.d.ts +9 -2
  73. package/dist/db/slices.js +15 -8
  74. package/dist/db/snapshots.d.ts +16 -1
  75. package/dist/db/snapshots.js +27 -2
  76. package/dist/db/sql.d.ts +2 -2
  77. package/dist/db/sql.js +1 -2
  78. package/dist/endpoints/cohortSources.d.ts +16 -0
  79. package/dist/endpoints/cohortSources.js +335 -0
  80. package/dist/endpoints/contentLocale.d.ts +1 -1
  81. package/dist/endpoints/events.js +82 -18
  82. package/dist/endpoints/index.js +45 -1
  83. package/dist/endpoints/pausedBy.d.ts +14 -0
  84. package/dist/endpoints/pausedBy.js +94 -0
  85. package/dist/endpoints/preflight.js +4 -1
  86. package/dist/endpoints/previewToken.js +2 -0
  87. package/dist/endpoints/results.d.ts +2 -2
  88. package/dist/endpoints/results.js +111 -3
  89. package/dist/endpoints/routeId.d.ts +2 -1
  90. package/dist/endpoints/routeId.js +6 -2
  91. package/dist/endpoints/summary.js +24 -1
  92. package/dist/endpoints/transitions.d.ts +7 -0
  93. package/dist/endpoints/transitions.js +50 -1
  94. package/dist/events/rows.d.ts +1 -1
  95. package/dist/events/rows.js +5 -3
  96. package/dist/exports/index.d.ts +2 -0
  97. package/dist/exports/index.js +1 -0
  98. package/dist/exports/runtimeProxy.d.ts +1 -0
  99. package/dist/exports/runtimeProxy.js +1 -0
  100. package/dist/hooks/blockRemovalRule.js +7 -4
  101. package/dist/hooks/pageGuard.js +1 -1
  102. package/dist/labels/status.d.ts +15 -0
  103. package/dist/labels/status.js +12 -0
  104. package/dist/manifest/compile.d.ts +15 -4
  105. package/dist/manifest/compile.js +91 -3
  106. package/dist/onInit.js +4 -1
  107. package/dist/options.d.ts +1 -0
  108. package/dist/options.js +10 -0
  109. package/dist/proxy/consentFallback.d.ts +1 -1
  110. package/dist/proxy/consentFallback.js +4 -4
  111. package/dist/proxy/control.d.ts +1 -1
  112. package/dist/proxy/control.js +2 -2
  113. package/dist/proxy/pageRewrite.d.ts +1 -1
  114. package/dist/proxy/pageRewrite.js +3 -2
  115. package/dist/proxy/resolve.js +76 -17
  116. package/dist/puck/ExperimentPicker.js +2 -1
  117. package/dist/puck/copy.d.ts +6 -5
  118. package/dist/puck/copy.js +3 -7
  119. package/dist/puck/editor/ExperimentSlotEditor.js +16 -5
  120. package/dist/puck/editor/QuickTestActions.js +7 -1
  121. package/dist/puck/editor/armWrites.js +6 -1
  122. package/dist/puck/editor/copy.d.ts +3 -0
  123. package/dist/puck/editor/copy.js +3 -0
  124. package/dist/puck/editor/editor.css +2 -1
  125. package/dist/puck/editor/editor.model.d.ts +2 -1
  126. package/dist/puck/editor/editor.model.js +2 -0
  127. package/dist/puck/editor/placementsCache.d.ts +1 -1
  128. package/dist/puck/editor/placementsCache.js +6 -3
  129. package/dist/results/analyse.d.ts +3 -1
  130. package/dist/results/analyse.js +20 -3
  131. package/dist/results/health.d.ts +11 -0
  132. package/dist/results/health.js +38 -0
  133. package/dist/results/power.d.ts +1 -1
  134. package/dist/results/power.js +1 -1
  135. package/dist/results/snapshot.d.ts +8 -2
  136. package/dist/results/snapshot.js +23 -1
  137. package/dist/results/unitValues.d.ts +48 -0
  138. package/dist/results/unitValues.js +166 -0
  139. package/dist/runtime/assign.d.ts +7 -3
  140. package/dist/runtime/assign.js +12 -4
  141. package/dist/runtime/cohortCookie.d.ts +15 -0
  142. package/dist/runtime/cohortCookie.js +72 -0
  143. package/dist/runtime/cohortLookup.d.ts +9 -0
  144. package/dist/runtime/cohortLookup.js +25 -0
  145. package/dist/runtime/eligibility.d.ts +11 -5
  146. package/dist/runtime/eligibility.js +33 -7
  147. package/dist/runtime/header.d.ts +1 -0
  148. package/dist/runtime/header.js +1 -1
  149. package/dist/runtime/proxyRuntime.js +5 -1
  150. package/dist/runtime/targeting.d.ts +3 -2
  151. package/dist/runtime/targeting.js +2 -1
  152. package/dist/sanitizeReadinessDetail.d.ts +2 -2
  153. package/dist/sanitizeReadinessDetail.js +41 -4
  154. package/dist/schedule/activeTime.d.ts +28 -0
  155. package/dist/schedule/activeTime.js +322 -0
  156. package/dist/schedule/ranges.d.ts +13 -0
  157. package/dist/schedule/ranges.js +11 -0
  158. package/dist/schedule/roundTime.d.ts +10 -0
  159. package/dist/schedule/roundTime.js +45 -0
  160. package/dist/schedule/types.d.ts +29 -0
  161. package/dist/schedule/types.js +1 -0
  162. package/dist/server/cohortLookup.d.ts +3 -0
  163. package/dist/server/cohortLookup.js +7 -0
  164. package/dist/server/eraseUser.d.ts +1 -0
  165. package/dist/server/getAssignments.js +63 -40
  166. package/dist/server/previewDraft.d.ts +1 -1
  167. package/dist/server/previewDraft.js +3 -2
  168. package/dist/server/recordExposures.js +4 -2
  169. package/dist/server/runResults.js +97 -150
  170. package/dist/testSurface/actions/LifecycleAction.d.ts +2 -1
  171. package/dist/testSurface/actions/LifecycleAction.js +15 -5
  172. package/dist/testSurface/actions/actions.model.d.ts +13 -2
  173. package/dist/testSurface/actions/actions.model.js +38 -5
  174. package/dist/testSurface/api.d.ts +13 -1
  175. package/dist/testSurface/api.js +19 -0
  176. package/dist/testSurface/copy/actions.d.ts +13 -0
  177. package/dist/testSurface/copy/actions.js +13 -0
  178. package/dist/testSurface/copy/cohorts.d.ts +68 -0
  179. package/dist/testSurface/copy/cohorts.js +81 -0
  180. package/dist/testSurface/copy/common.d.ts +2 -3
  181. package/dist/testSurface/copy/common.js +4 -4
  182. package/dist/testSurface/copy/inputs.d.ts +3 -0
  183. package/dist/testSurface/copy/inputs.js +3 -0
  184. package/dist/testSurface/copy/readiness.d.ts +20 -6
  185. package/dist/testSurface/copy/readiness.js +42 -9
  186. package/dist/testSurface/copy/results.d.ts +1 -0
  187. package/dist/testSurface/copy/results.js +1 -0
  188. package/dist/testSurface/copy/schedule.d.ts +58 -0
  189. package/dist/testSurface/copy/schedule.js +150 -0
  190. package/dist/testSurface/copy/surface.d.ts +2 -0
  191. package/dist/testSurface/copy/surface.js +2 -0
  192. package/dist/testSurface/copy/testPage.d.ts +12 -2
  193. package/dist/testSurface/copy/testPage.js +17 -2
  194. package/dist/testSurface/format/roundProgress.d.ts +13 -1
  195. package/dist/testSurface/format/roundProgress.js +34 -5
  196. package/dist/testSurface/format/scheduleStatus.d.ts +34 -0
  197. package/dist/testSurface/format/scheduleStatus.js +93 -0
  198. package/dist/testSurface/format/useScheduleClock.d.ts +3 -0
  199. package/dist/testSurface/format/useScheduleClock.js +20 -0
  200. package/dist/testSurface/labels.d.ts +4 -9
  201. package/dist/testSurface/labels.js +2 -7
  202. package/dist/testSurface/lockedNotice.model.d.ts +2 -2
  203. package/dist/testSurface/lockedNotice.model.js +16 -3
  204. package/dist/testSurface/readPreflight.d.ts +3 -0
  205. package/dist/testSurface/readPreflight.js +14 -0
  206. package/dist/testSurface/readinessRead.d.ts +2 -2
  207. package/dist/testSurface/results/DataQualityPanel.js +7 -7
  208. package/dist/testSurface/results/results.model.d.ts +1 -1
  209. package/dist/testSurface/results/results.model.js +34 -6
  210. package/dist/testSurface/review/cohortEstimate.d.ts +11 -0
  211. package/dist/testSurface/review/cohortEstimate.js +32 -0
  212. package/dist/testSurface/surface/contracts.d.ts +2 -1
  213. package/dist/testSurface/surface/contracts.js +1 -0
  214. package/dist/testSurface/surface/editorPlacement.d.ts +1 -0
  215. package/dist/testSurface/surface/readiness/ActionDialogs.d.ts +3 -2
  216. package/dist/testSurface/surface/readiness/ActionDialogs.js +121 -32
  217. package/dist/testSurface/surface/readiness/Banner.js +29 -11
  218. package/dist/testSurface/surface/readiness/readiness.model.d.ts +20 -2
  219. package/dist/testSurface/surface/readiness/readiness.model.js +35 -5
  220. package/dist/testSurface/surface/readiness/useTransition.js +17 -5
  221. package/dist/testSurface/surface/results/ResultsBody.js +44 -8
  222. package/dist/testSurface/surface/results/useResults.d.ts +16 -1
  223. package/dist/testSurface/surface/results/useResults.js +17 -6
  224. package/dist/testSurface/surface/test/CohortRuleEditor.d.ts +16 -0
  225. package/dist/testSurface/surface/test/CohortRuleEditor.js +492 -0
  226. package/dist/testSurface/surface/test/LengthSection.js +26 -6
  227. package/dist/testSurface/surface/test/ScheduleDateInput.d.ts +4 -0
  228. package/dist/testSurface/surface/test/ScheduleDateInput.js +15 -0
  229. package/dist/testSurface/surface/test/ScheduleSection.d.ts +2 -0
  230. package/dist/testSurface/surface/test/ScheduleSection.js +322 -0
  231. package/dist/testSurface/surface/test/Section.js +1 -1
  232. package/dist/testSurface/surface/test/TargetingSection.js +306 -182
  233. package/dist/testSurface/surface/test/TestSurface.js +9 -0
  234. package/dist/testSurface/surface/test/fields.d.ts +2 -1
  235. package/dist/testSurface/surface/test/fields.js +3 -3
  236. package/dist/testSurface/surface/test/jumpToSection.d.ts +1 -1
  237. package/dist/testSurface/surface/test/jumpToSection.js +4 -4
  238. package/dist/testSurface/surface/test/testPage.css +26 -0
  239. package/dist/testSurface/surface/testDocument.model.d.ts +12 -1
  240. package/dist/testSurface/surface/testDocument.model.js +2 -1
  241. package/dist/testSurface/surface/useTestDocument.js +11 -4
  242. package/dist/testSurface/targeting/targeting.model.d.ts +2 -1
  243. package/dist/testSurface/targeting/targeting.model.js +1 -1
  244. package/dist/testSurface/useApi.d.ts +12 -0
  245. package/dist/transitions/addMetric.js +5 -2
  246. package/dist/transitions/allocate.js +6 -1
  247. package/dist/transitions/assertNoOverlap.d.ts +9 -0
  248. package/dist/transitions/assertNoOverlap.js +24 -0
  249. package/dist/transitions/buildCohorts.d.ts +10 -0
  250. package/dist/transitions/buildCohorts.js +68 -0
  251. package/dist/transitions/cancelSchedule.d.ts +4 -0
  252. package/dist/transitions/cancelSchedule.js +42 -0
  253. package/dist/transitions/checkStart.d.ts +17 -3
  254. package/dist/transitions/checkStart.js +20 -7
  255. package/dist/transitions/decide.js +35 -11
  256. package/dist/transitions/kernel.d.ts +3 -1
  257. package/dist/transitions/kernel.js +5 -2
  258. package/dist/transitions/pause.d.ts +4 -0
  259. package/dist/transitions/pause.js +35 -0
  260. package/dist/transitions/phaseRecord.d.ts +19 -2
  261. package/dist/transitions/phaseRecord.js +50 -11
  262. package/dist/transitions/placementInteractions.js +1 -1
  263. package/dist/transitions/preflightData.d.ts +11 -3
  264. package/dist/transitions/preflightData.js +134 -81
  265. package/dist/transitions/reschedule.d.ts +4 -0
  266. package/dist/transitions/reschedule.js +58 -0
  267. package/dist/transitions/resume.d.ts +4 -0
  268. package/dist/transitions/resume.js +32 -0
  269. package/dist/transitions/scheduledRound.d.ts +18 -0
  270. package/dist/transitions/scheduledRound.js +42 -0
  271. package/dist/transitions/start.d.ts +4 -0
  272. package/dist/transitions/start.js +136 -55
  273. package/dist/transitions/startReads.d.ts +14 -0
  274. package/dist/transitions/startReads.js +27 -0
  275. package/dist/transitions/stop.d.ts +10 -0
  276. package/dist/transitions/stop.js +35 -22
  277. package/dist/types/admin.d.ts +1 -1
  278. package/dist/types/contracts.d.ts +7 -1
  279. package/dist/types/endpoints.d.ts +72 -3
  280. package/dist/types/manifest.d.ts +9 -0
  281. package/dist/types/runtime.d.ts +5 -0
  282. package/dist/types/snapshot.d.ts +10 -0
  283. package/dist/types/status.d.ts +1 -0
  284. package/dist/types/status.js +1 -0
  285. package/dist/types/surface.d.ts +1 -0
  286. package/dist/ui/Feedback.d.ts +9 -6
  287. package/dist/ui/Feedback.js +10 -9
  288. package/dist/ui/SelectControl.js +1 -1
  289. package/dist/ui/ui.css +2 -1
  290. package/package.json +2 -2
@@ -1,6 +1,6 @@
1
1
  import type { ReadinessCode } from './types/endpoints.js';
2
- /** Summary details contain identifiers only. Project known shapes rather than
3
- * recursively copying host objects: titles, messages and placement detail are text.
2
+ /** Summary details project known shapes rather than recursively copying host objects.
3
+ * Only manager readiness includes the named test and date for an occupancy conflict.
4
4
  * Other checks use authored client fallbacks and need no detail (including slot
5
5
  * counts and layer capacity, which are not identifiers). Empty slots carry only version keys.
6
6
  */
@@ -10,8 +10,24 @@ const arm = (value)=>typeof value === 'string' && [
10
10
  ].includes(value);
11
11
  const path = (value)=>typeof value === 'string' && value.length <= 128 && value.startsWith('/') && !value.includes('//') && !/[^A-Za-z0-9_/-]/.test(value);
12
12
  const failure = (value)=>value === 'placements-failed' || value === 'forbidden' || value === 'not-found';
13
- /** Summary details contain identifiers only. Project known shapes rather than
14
- * recursively copying host objects: titles, messages and placement detail are text.
13
+ const occupancy = (detail)=>{
14
+ const entry = object(detail) ? detail.occupancy : undefined;
15
+ if (!object(entry) || typeof entry.otherTestName !== 'string' || typeof entry.from !== 'string' || !Number.isFinite(Date.parse(entry.from)) || ![
16
+ 'page',
17
+ 'setting',
18
+ 'traffic group'
19
+ ].includes(String(entry.place))) return undefined;
20
+ return {
21
+ otherTestName: entry.otherTestName.slice(0, 128),
22
+ from: entry.from,
23
+ place: entry.place,
24
+ ...typeof entry.timeZone === 'string' && entry.timeZone.length <= 64 ? {
25
+ timeZone: entry.timeZone
26
+ } : {}
27
+ };
28
+ };
29
+ /** Summary details project known shapes rather than recursively copying host objects.
30
+ * Only manager readiness includes the named test and date for an occupancy conflict.
15
31
  * Other checks use authored client fallbacks and need no detail (including slot
16
32
  * counts and layer capacity, which are not identifiers). Empty slots carry only version keys.
17
33
  */ export function sanitizeReadinessDetail(code, detail, draftReadySlots = []) {
@@ -87,8 +103,20 @@ const failure = (value)=>value === 'placements-failed' || value === 'forbidden'
87
103
  {
88
104
  if (!object(detail) || !Array.isArray(detail.paths)) return undefined;
89
105
  const paths = detail.paths.filter(path);
90
- return paths.length ? {
91
- paths
106
+ const peer = occupancy(detail);
107
+ return paths.length || peer ? {
108
+ paths,
109
+ ...peer ? {
110
+ occupancy: peer
111
+ } : {}
112
+ } : undefined;
113
+ }
114
+ case 'flag-point-busy':
115
+ case 'layer-full':
116
+ {
117
+ const peer = occupancy(detail);
118
+ return peer ? {
119
+ occupancy: peer
92
120
  } : undefined;
93
121
  }
94
122
  case 'page-target-unpublished':
@@ -113,6 +141,15 @@ const failure = (value)=>value === 'placements-failed' || value === 'forbidden'
113
141
  }))
114
142
  };
115
143
  }
144
+ case 'cohort-source-invalid':
145
+ case 'cohort-cycle':
146
+ case 'cohort-source-open':
147
+ return typeof detail === 'object' && detail !== null && 'rule' in detail && Number.isSafeInteger(detail.rule) ? {
148
+ rule: detail.rule,
149
+ ...'reason' in detail && typeof detail.reason === 'string' ? {
150
+ reason: detail.reason
151
+ } : {}
152
+ } : undefined;
116
153
  case 'undeclared-targeting-key':
117
154
  case 'invalid-targeting-rule':
118
155
  return object(detail) && Array.isArray(detail.attributes) && detail.attributes.length && detail.attributes.every(key) ? {
@@ -0,0 +1,28 @@
1
+ import type { ActiveSpec, PauseInterval, ScheduleWindow } from './types.js';
2
+ type DateRange = {
3
+ from: Date;
4
+ to: Date;
5
+ };
6
+ type Pauses = readonly (PauseInterval | Pick<PauseInterval, 'from' | 'to'>)[];
7
+ export declare function isValidTimeZone(tz: string): boolean;
8
+ export declare function isActive(spec: ActiveSpec, pauses: PauseInterval[], now: Date): boolean;
9
+ export declare function inactiveIntervals(spec: ActiveSpec, pauses: Pauses, range: DateRange): DateRange[];
10
+ export declare function activeDays(spec: ActiveSpec, pauses: Pauses, range: DateRange): number;
11
+ /** Wall-clock input for an instant, independent of the browser's time zone. */
12
+ export declare function formatLocalDateTime(value: string | null | undefined, timeZone: string): string;
13
+ /** Choose the first occurrence of a repeated clock time and refuse nonexistent times. */
14
+ export declare function parseLocalDateTime(value: string, timeZone: string): string | null;
15
+ export declare function localDayStart(timeZone: string, now: Date): Date;
16
+ /** An instant on the final active date, for display in the schedule's zone. */
17
+ export declare function activeDayDate(spec: ActiveSpec, pauses: Pauses, range: DateRange, day: number): number | null;
18
+ /** Nominal recurring week in wall time, merging overlaps and overnight tails. */
19
+ export declare function activeWeek(windows: ScheduleWindow[]): {
20
+ hours: number;
21
+ days: number;
22
+ weekendsOnly: boolean;
23
+ };
24
+ export declare function nextActiveChange(spec: ActiveSpec, now: Date): {
25
+ at: Date;
26
+ active: boolean;
27
+ } | null;
28
+ export {};
@@ -0,0 +1,322 @@
1
+ const MINUTE = 60_000;
2
+ const DAY = 24 * 60 * MINUTE;
3
+ const WEEK = 7 * DAY;
4
+ const OFFSET_PROBE = 6 * 60 * MINUTE;
5
+ const WEEKDAYS = [
6
+ 'sun',
7
+ 'mon',
8
+ 'tue',
9
+ 'wed',
10
+ 'thu',
11
+ 'fri',
12
+ 'sat'
13
+ ];
14
+ const formatters = new Map();
15
+ export function isValidTimeZone(tz) {
16
+ if (typeof tz !== 'string' || !tz || /^[+-]/.test(tz)) return false;
17
+ try {
18
+ new Intl.DateTimeFormat('en-US', {
19
+ timeZone: tz
20
+ });
21
+ return true;
22
+ } catch {
23
+ return false;
24
+ }
25
+ }
26
+ function formatterFor(tz) {
27
+ let formatter = formatters.get(tz);
28
+ if (!formatter) {
29
+ formatter = new Intl.DateTimeFormat('en-US', {
30
+ timeZone: tz,
31
+ hourCycle: 'h23',
32
+ year: 'numeric',
33
+ month: 'numeric',
34
+ day: 'numeric',
35
+ hour: 'numeric',
36
+ minute: 'numeric',
37
+ second: 'numeric'
38
+ });
39
+ formatters.set(tz, formatter);
40
+ }
41
+ return formatter;
42
+ }
43
+ // Express the local clock as a UTC timestamp to get the offset at this instant.
44
+ function offsetAt(formatter, time) {
45
+ const parts = Object.fromEntries(formatter.formatToParts(time).map(({ type, value })=>[
46
+ type,
47
+ Number(value)
48
+ ]));
49
+ const local = new Date(0);
50
+ local.setUTCFullYear(parts.year, parts.month - 1, parts.day);
51
+ local.setUTCHours(parts.hour, parts.minute, parts.second, 0);
52
+ return local.getTime() - Math.floor(time / 1_000) * 1_000;
53
+ }
54
+ function clockTime(value) {
55
+ const [hours, minutes] = value.split(':').map(Number);
56
+ return (hours * 60 + minutes) * MINUTE;
57
+ }
58
+ function windowTimes(window) {
59
+ const from = clockTime(window.from);
60
+ const to = clockTime(window.to);
61
+ return {
62
+ from,
63
+ to: to <= from ? to + DAY : to
64
+ };
65
+ }
66
+ const startOfDay = (time)=>Math.floor(time / DAY) * DAY;
67
+ const weekday = (day)=>WEEKDAYS[new Date(day).getUTCDay()];
68
+ const datesToSpan = (range)=>({
69
+ from: range.from.getTime(),
70
+ to: range.to.getTime()
71
+ });
72
+ const parseSpan = ({ from, to })=>({
73
+ from: from === undefined ? -Infinity : Date.parse(from),
74
+ to: to === undefined ? Infinity : Date.parse(to)
75
+ });
76
+ const contains = ({ from, to }, time)=>from <= time && time < to;
77
+ const intersect = (a, b)=>({
78
+ from: Math.max(a.from, b.from),
79
+ to: Math.min(a.to, b.to)
80
+ });
81
+ export function isActive(spec, pauses, now) {
82
+ const time = now.getTime();
83
+ if (!contains(parseSpan(spec), time) || pauses.some((pause)=>contains(parseSpan(pause), time))) return false;
84
+ if (!spec.windows.length) return true;
85
+ const local = time + offsetAt(formatterFor(spec.tz), time);
86
+ const today = startOfDay(local);
87
+ const yesterday = today - DAY;
88
+ return spec.windows.some((window)=>{
89
+ const span = windowTimes(window);
90
+ return window.days.includes(weekday(today)) && contains(span, local - today) || window.days.includes(weekday(yesterday)) && contains(span, local - yesterday);
91
+ });
92
+ }
93
+ // Between offset transitions, every wall-clock edge has one UTC equivalent.
94
+ // Splitting at the transitions covers both copies of a repeated time and skips gaps.
95
+ function* offsetSpans(formatter, span) {
96
+ if (span.from >= span.to) return;
97
+ let from = span.from;
98
+ let before = from;
99
+ let offset = offsetAt(formatter, from);
100
+ while(before < span.to - 1){
101
+ let after = Math.min(before + OFFSET_PROBE, span.to - 1);
102
+ const nextOffset = offsetAt(formatter, after);
103
+ if (nextOffset === offset) {
104
+ before = after;
105
+ continue;
106
+ }
107
+ // Probe the actual transition to millisecond precision, including non-hour shifts.
108
+ while(after - before > 1){
109
+ const middle = before + Math.floor((after - before) / 2);
110
+ if (offsetAt(formatter, middle) === offset) before = middle;
111
+ else after = middle;
112
+ }
113
+ yield {
114
+ from,
115
+ to: after,
116
+ offset
117
+ };
118
+ from = before = after;
119
+ offset = nextOffset;
120
+ }
121
+ yield {
122
+ from,
123
+ to: span.to,
124
+ offset
125
+ };
126
+ }
127
+ function windowSpans(windows, span, offset) {
128
+ const localStart = startOfDay(span.from + offset);
129
+ const intervals = [];
130
+ for(let day = localStart - DAY; day < span.to + offset; day += DAY){
131
+ const startDay = weekday(day);
132
+ for (const window of windows){
133
+ if (!window.days.includes(startDay)) continue;
134
+ const { from, to } = windowTimes(window);
135
+ const interval = intersect(span, {
136
+ from: day + from - offset,
137
+ to: day + to - offset
138
+ });
139
+ if (interval.from < interval.to) intervals.push(interval);
140
+ }
141
+ }
142
+ return intervals;
143
+ }
144
+ function merge(spans) {
145
+ const merged = [];
146
+ for (const span of spans.sort((a, b)=>a.from - b.from)){
147
+ if (!(span.from < span.to)) continue;
148
+ const last = merged.at(-1);
149
+ if (last && span.from <= last.to) last.to = Math.max(last.to, span.to);
150
+ else merged.push({
151
+ ...span
152
+ });
153
+ }
154
+ return merged;
155
+ }
156
+ function complement(spans, range) {
157
+ const gaps = [];
158
+ let from = range.from;
159
+ for (const span of spans){
160
+ if (from < span.from) gaps.push({
161
+ from,
162
+ to: span.from
163
+ });
164
+ from = span.to;
165
+ }
166
+ if (from < range.to) gaps.push({
167
+ from,
168
+ to: range.to
169
+ });
170
+ return gaps;
171
+ }
172
+ function scheduledSpans(spec, span) {
173
+ if (!(span.from < span.to)) return [];
174
+ if (!spec.windows.length) return [
175
+ span
176
+ ];
177
+ const intervals = [];
178
+ for (const segment of offsetSpans(formatterFor(spec.tz), span)){
179
+ for (const interval of windowSpans(spec.windows, segment, segment.offset))intervals.push(interval);
180
+ }
181
+ return merge(intervals);
182
+ }
183
+ function inactiveSpans(spec, pauses, range) {
184
+ const scheduled = scheduledSpans(spec, intersect(range, parseSpan(spec)));
185
+ return merge([
186
+ ...complement(scheduled, range),
187
+ ...pauses.map((pause)=>intersect(range, parseSpan(pause)))
188
+ ]);
189
+ }
190
+ export function inactiveIntervals(spec, pauses, range) {
191
+ return inactiveSpans(spec, pauses, datesToSpan(range)).map(({ from, to })=>({
192
+ from: new Date(from),
193
+ to: new Date(to)
194
+ }));
195
+ }
196
+ function activeLocalDates(spec, pauses, range) {
197
+ const span = datesToSpan(range);
198
+ const active = complement(inactiveSpans(spec, pauses, span), span);
199
+ const days = [];
200
+ const formatter = formatterFor(spec.tz);
201
+ for (const interval of active){
202
+ for (const { from, to, offset } of offsetSpans(formatter, interval)){
203
+ days.push({
204
+ from: startOfDay(from + offset),
205
+ to: startOfDay(to - 1 + offset) + DAY
206
+ });
207
+ }
208
+ }
209
+ return merge(days);
210
+ }
211
+ export function activeDays(spec, pauses, range) {
212
+ return activeLocalDates(spec, pauses, range).reduce((count, { from, to })=>count + (to - from) / DAY, 0);
213
+ }
214
+ function localDateSpans(timeZone, date) {
215
+ return [
216
+ ...offsetSpans(formatterFor(timeZone), {
217
+ from: date - DAY,
218
+ to: date + 2 * DAY
219
+ })
220
+ ].map((segment)=>intersect(segment, {
221
+ from: date - segment.offset,
222
+ to: date + DAY - segment.offset
223
+ })).filter((span)=>span.from < span.to);
224
+ }
225
+ /** Wall-clock input for an instant, independent of the browser's time zone. */ export function formatLocalDateTime(value, timeZone) {
226
+ const instant = Date.parse(value ?? '');
227
+ if (!Number.isFinite(instant)) return '';
228
+ return new Date(instant + offsetAt(formatterFor(timeZone), instant)).toISOString().slice(0, -1);
229
+ }
230
+ /** Choose the first occurrence of a repeated clock time and refuse nonexistent times. */ export function parseLocalDateTime(value, timeZone) {
231
+ const clock = Date.parse(`${value}Z`);
232
+ if (!Number.isFinite(clock)) return null;
233
+ for (const segment of offsetSpans(formatterFor(timeZone), {
234
+ from: clock - DAY,
235
+ to: clock + DAY
236
+ })){
237
+ const instant = clock - segment.offset;
238
+ if (contains(segment, instant)) return new Date(instant).toISOString();
239
+ }
240
+ return null;
241
+ }
242
+ export function localDayStart(timeZone, now) {
243
+ const date = startOfDay(+now + offsetAt(formatterFor(timeZone), +now));
244
+ return new Date(localDateSpans(timeZone, date)[0].from);
245
+ }
246
+ /** An instant on the final active date, for display in the schedule's zone. */ export function activeDayDate(spec, pauses, range, day) {
247
+ for (const { from, to } of activeLocalDates(spec, pauses, range)){
248
+ const days = (to - from) / DAY;
249
+ if (day <= days) return localDateSpans(spec.tz, from + (day - 1) * DAY)[0].from;
250
+ day -= days;
251
+ }
252
+ return null;
253
+ }
254
+ /** Nominal recurring week in wall time, merging overlaps and overnight tails. */ export function activeWeek(windows) {
255
+ if (!windows.length) return {
256
+ hours: 168,
257
+ days: 7,
258
+ weekendsOnly: false
259
+ };
260
+ const monday = 4 * DAY;
261
+ const week = {
262
+ from: monday,
263
+ to: monday + WEEK
264
+ };
265
+ const spans = merge(windowSpans(windows, week, 0));
266
+ return {
267
+ hours: spans.reduce((total, span)=>total + (span.to - span.from) / (60 * MINUTE), 0),
268
+ days: merge(spans.map(({ from, to })=>({
269
+ from: startOfDay(from),
270
+ to: startOfDay(to - 1) + DAY
271
+ }))).reduce((total, span)=>total + (span.to - span.from) / DAY, 0),
272
+ weekendsOnly: spans.length > 0 && spans.every((span)=>span.from >= monday + 5 * DAY)
273
+ };
274
+ }
275
+ export function nextActiveChange(spec, now) {
276
+ const time = now.getTime();
277
+ const bounds = parseSpan(spec);
278
+ const start = Math.max(time, bounds.from);
279
+ if (!(start < bounds.to)) return null;
280
+ // Check one week in wall time. A constant schedule stays constant across DST.
281
+ const week = {
282
+ from: 0,
283
+ to: WEEK
284
+ };
285
+ const pattern = merge(windowSpans(spec.windows, week, 0));
286
+ if (!spec.windows.length || pattern[0]?.from === week.from && pattern[0].to === week.to) {
287
+ if (start > time) return {
288
+ at: new Date(start),
289
+ active: true
290
+ };
291
+ return Number.isFinite(bounds.to) ? {
292
+ at: new Date(bounds.to),
293
+ active: false
294
+ } : null;
295
+ }
296
+ if (!pattern.length) return null;
297
+ const active = isActive(spec, [], now);
298
+ // Search in weekly chunks so a skipped window can advance to its next occurrence.
299
+ for(let from = start; from < bounds.to;){
300
+ const to = Math.min(from + WEEK, bounds.to);
301
+ const [first] = scheduledSpans(spec, {
302
+ from,
303
+ to
304
+ });
305
+ if (!active && first) return {
306
+ at: new Date(first.from),
307
+ active: true
308
+ };
309
+ if (active) {
310
+ if (first?.from !== from) return {
311
+ at: new Date(from),
312
+ active: false
313
+ };
314
+ if (first.to < to || to === bounds.to) return {
315
+ at: new Date(first.to),
316
+ active: false
317
+ };
318
+ }
319
+ from = to;
320
+ }
321
+ return null;
322
+ }
@@ -0,0 +1,13 @@
1
+ export type Claim = {
2
+ from: Date;
3
+ to: Date | null;
4
+ };
5
+ /** Occupancy uses the outer bounds of a round, including when it has recurring windows. */
6
+ export declare function claimRange(phase: {
7
+ startedAt: string | Date;
8
+ schedule?: {
9
+ startAt?: string | null;
10
+ endAt?: string | null;
11
+ } | null;
12
+ }): Claim;
13
+ export declare function rangesOverlap(a: Claim, b: Claim): boolean;
@@ -0,0 +1,11 @@
1
+ /** Occupancy uses the outer bounds of a round, including when it has recurring windows. */ export function claimRange(phase) {
2
+ return {
3
+ from: new Date(phase.schedule?.startAt ?? phase.startedAt),
4
+ to: phase.schedule?.endAt == null ? null : new Date(phase.schedule.endAt)
5
+ };
6
+ }
7
+ export function rangesOverlap(a, b) {
8
+ const aFrom = a.from.getTime(), bFrom = b.from.getTime();
9
+ const aTo = a.to?.getTime() ?? Infinity, bTo = b.to?.getTime() ?? Infinity;
10
+ return Math.max(aFrom, bFrom) < Math.min(aTo, bTo);
11
+ }
@@ -0,0 +1,10 @@
1
+ import type { ActiveSpec, RoundTiming } from './types.js';
2
+ export declare const DAY_MS = 86400000;
3
+ export declare function roundActiveSpec(timing: RoundTiming): ActiveSpec;
4
+ export declare function hasRoundTiming(timing?: RoundTiming | null): timing is RoundTiming;
5
+ /** Results readers need boundaries only. Pause authors and reasons stay on manager surfaces. */
6
+ export declare function publicRoundTiming({ schedule, pauses }: RoundTiming): RoundTiming;
7
+ export declare function roundDays(timing: RoundTiming, from: Date, to: Date): {
8
+ days: number;
9
+ completed: number;
10
+ };
@@ -0,0 +1,45 @@
1
+ import { activeDays, localDayStart } from './activeTime.js';
2
+ export const DAY_MS = 86_400_000;
3
+ export function roundActiveSpec(timing) {
4
+ const { schedule } = timing;
5
+ return {
6
+ from: schedule?.startAt,
7
+ to: schedule?.endAt,
8
+ tz: schedule?.timeZone ?? 'UTC',
9
+ windows: schedule?.windows ?? []
10
+ };
11
+ }
12
+ export function hasRoundTiming(timing) {
13
+ return Boolean(timing?.schedule || timing?.pauses?.length);
14
+ }
15
+ /** Results readers need boundaries only. Pause authors and reasons stay on manager surfaces. */ export function publicRoundTiming({ schedule, pauses }) {
16
+ return {
17
+ ...schedule ? {
18
+ schedule
19
+ } : {},
20
+ ...pauses?.length ? {
21
+ pauses: pauses.map(({ from, to })=>({
22
+ from,
23
+ to
24
+ }))
25
+ } : {}
26
+ };
27
+ }
28
+ export function roundDays(timing, from, to) {
29
+ const spec = roundActiveSpec(timing);
30
+ const pauses = timing.pauses ?? [];
31
+ const openPause = pauses.find((pause)=>!pause.to && Date.parse(pause.from) <= +to);
32
+ const asOf = openPause ? new Date(openPause.from) : to;
33
+ const days = activeDays(spec, pauses, {
34
+ from,
35
+ to: asOf
36
+ });
37
+ const completed = activeDays(spec, pauses, {
38
+ from,
39
+ to: localDayStart(spec.tz, asOf)
40
+ });
41
+ return {
42
+ days,
43
+ completed
44
+ };
45
+ }
@@ -0,0 +1,29 @@
1
+ export type Weekday = 'mon' | 'tue' | 'wed' | 'thu' | 'fri' | 'sat' | 'sun';
2
+ export type ScheduleWindow = {
3
+ days: Weekday[];
4
+ from: string;
5
+ to: string;
6
+ };
7
+ export type ActiveSpec = {
8
+ from?: string;
9
+ to?: string;
10
+ tz: string;
11
+ windows: ScheduleWindow[];
12
+ };
13
+ export type PauseInterval = {
14
+ from: string;
15
+ to?: string;
16
+ by: string;
17
+ reason?: string;
18
+ };
19
+ export type ExperimentSchedule = {
20
+ startAt?: string;
21
+ endAt?: string;
22
+ timeZone: string;
23
+ windows: ScheduleWindow[];
24
+ };
25
+ /** The part of the frozen phase record needed to calculate time and progress. */
26
+ export type RoundTiming = {
27
+ schedule?: ExperimentSchedule | null;
28
+ pauses?: Array<Pick<PauseInterval, 'from' | 'to'>> | null;
29
+ };
@@ -0,0 +1 @@
1
+ /** The part of the frozen phase record needed to calculate time and progress. */ export { };
@@ -0,0 +1,3 @@
1
+ import type { Payload } from 'payload';
2
+ import type { CohortLookupArgs } from '../db/cohortMembership.js';
3
+ export declare function resolveServerCohorts(payload: Payload | undefined, args: CohortLookupArgs): Promise<Map<string, boolean> | undefined> | undefined;
@@ -0,0 +1,7 @@
1
+ import { payloadCohortPool } from '../db/cohortPool.js';
2
+ import { createPostgresCohortLookup, resolveCohorts } from '../runtime/cohortLookup.js';
3
+ export function resolveServerCohorts(payload, args) {
4
+ if (!args.keys.length) return undefined;
5
+ const options = payload && payloadCohortPool(payload);
6
+ return options ? resolveCohorts(createPostgresCohortLookup(options), args) : undefined;
7
+ }
@@ -4,4 +4,5 @@ export declare function eraseUser(payload: Payload, opts: {
4
4
  }): Promise<{
5
5
  identities: number;
6
6
  events: number;
7
+ cohortMembers: number;
7
8
  }>;