@homeflare/alchemy 0.31.2 → 0.32.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 (140) hide show
  1. package/README.md +39 -3
  2. package/dist/linux/container-form.d.ts +129 -0
  3. package/dist/linux/container-form.d.ts.map +1 -0
  4. package/dist/linux/container-generator.d.ts +74 -0
  5. package/dist/linux/container-generator.d.ts.map +1 -0
  6. package/dist/linux/container-handlers.d.ts +15 -0
  7. package/dist/linux/container-handlers.d.ts.map +1 -0
  8. package/dist/linux/container-lifecycle.d.ts +37 -0
  9. package/dist/linux/container-lifecycle.d.ts.map +1 -0
  10. package/dist/linux/container-preflight.d.ts +34 -0
  11. package/dist/linux/container-preflight.d.ts.map +1 -0
  12. package/dist/linux/container-secrets.d.ts +37 -0
  13. package/dist/linux/container-secrets.d.ts.map +1 -0
  14. package/dist/linux/container-settle.d.ts +34 -0
  15. package/dist/linux/container-settle.d.ts.map +1 -0
  16. package/dist/linux/container.d.ts +30 -0
  17. package/dist/linux/container.d.ts.map +1 -0
  18. package/dist/linux/fake-quadlet.d.ts +25 -0
  19. package/dist/linux/fake-quadlet.d.ts.map +1 -0
  20. package/dist/linux/index.d.ts +6 -3
  21. package/dist/linux/index.d.ts.map +1 -1
  22. package/dist/linux/index.js +641 -268
  23. package/dist/linux/index.js.map +15 -7
  24. package/dist/linux/providers.d.ts +1 -1
  25. package/dist/linux/providers.d.ts.map +1 -1
  26. package/dist/linux/systemctl.d.ts +7 -1
  27. package/dist/linux/systemctl.d.ts.map +1 -1
  28. package/dist/opnsense/alias-form.d.ts +29 -0
  29. package/dist/opnsense/alias-form.d.ts.map +1 -0
  30. package/dist/opnsense/alias.d.ts +81 -0
  31. package/dist/opnsense/alias.d.ts.map +1 -0
  32. package/dist/opnsense/category-form.d.ts +10 -0
  33. package/dist/opnsense/category-form.d.ts.map +1 -0
  34. package/dist/opnsense/category.d.ts +58 -0
  35. package/dist/opnsense/category.d.ts.map +1 -0
  36. package/dist/opnsense/credentials.d.ts +36 -0
  37. package/dist/opnsense/credentials.d.ts.map +1 -0
  38. package/dist/opnsense/fake-opnsense.d.ts +51 -0
  39. package/dist/opnsense/fake-opnsense.d.ts.map +1 -0
  40. package/dist/opnsense/group-form.d.ts +11 -0
  41. package/dist/opnsense/group-form.d.ts.map +1 -0
  42. package/dist/opnsense/group.d.ts +62 -0
  43. package/dist/opnsense/group.d.ts.map +1 -0
  44. package/dist/opnsense/index.d.ts +16 -0
  45. package/dist/opnsense/index.d.ts.map +1 -0
  46. package/dist/opnsense/index.js +211 -0
  47. package/dist/opnsense/index.js.map +20 -0
  48. package/dist/opnsense/policy.d.ts +20 -0
  49. package/dist/opnsense/policy.d.ts.map +1 -0
  50. package/dist/opnsense/providers.d.ts +13 -0
  51. package/dist/opnsense/providers.d.ts.map +1 -0
  52. package/dist/opnsense/resource.d.ts +74 -0
  53. package/dist/opnsense/resource.d.ts.map +1 -0
  54. package/dist/opnsense/wire.d.ts +39 -0
  55. package/dist/opnsense/wire.d.ts.map +1 -0
  56. package/dist/proxmox/index.js +106 -47
  57. package/dist/proxmox/index.js.map +6 -4
  58. package/dist/proxmox/storage-form.d.ts +54 -0
  59. package/dist/proxmox/storage-form.d.ts.map +1 -0
  60. package/dist/proxmox/storage-wire.d.ts +56 -0
  61. package/dist/proxmox/storage-wire.d.ts.map +1 -0
  62. package/dist/proxmox/storage.d.ts +55 -60
  63. package/dist/proxmox/storage.d.ts.map +1 -1
  64. package/dist/unifi/fake-unifi.d.ts +48 -0
  65. package/dist/unifi/fake-unifi.d.ts.map +1 -0
  66. package/dist/unifi/firewall-zone-form.d.ts +40 -0
  67. package/dist/unifi/firewall-zone-form.d.ts.map +1 -0
  68. package/dist/unifi/firewall-zone.d.ts +50 -0
  69. package/dist/unifi/firewall-zone.d.ts.map +1 -0
  70. package/dist/unifi/index.d.ts +13 -0
  71. package/dist/unifi/index.d.ts.map +1 -0
  72. package/dist/unifi/index.js +190 -0
  73. package/dist/unifi/index.js.map +16 -0
  74. package/dist/unifi/network-form.d.ts +67 -0
  75. package/dist/unifi/network-form.d.ts.map +1 -0
  76. package/dist/unifi/network.d.ts +67 -0
  77. package/dist/unifi/network.d.ts.map +1 -0
  78. package/dist/unifi/policy.d.ts +20 -0
  79. package/dist/unifi/policy.d.ts.map +1 -0
  80. package/dist/unifi/providers.d.ts +13 -0
  81. package/dist/unifi/providers.d.ts.map +1 -0
  82. package/dist/unifi/resource.d.ts +64 -0
  83. package/dist/unifi/resource.d.ts.map +1 -0
  84. package/docs/opnsense.md +146 -0
  85. package/docs/provider-standard.md +1 -1
  86. package/docs/quadlet-container.md +132 -0
  87. package/docs/unifi.md +93 -0
  88. package/package.json +12 -2
  89. package/src/linux/container-fixture.test.ts +92 -0
  90. package/src/linux/container-form.ts +243 -0
  91. package/src/linux/container-generator.test.ts +195 -0
  92. package/src/linux/container-generator.ts +113 -0
  93. package/src/linux/container-handlers.ts +73 -0
  94. package/src/linux/container-lifecycle.test.ts +209 -0
  95. package/src/linux/container-lifecycle.ts +217 -0
  96. package/src/linux/container-preflight.ts +97 -0
  97. package/src/linux/container-secrets.test.ts +171 -0
  98. package/src/linux/container-secrets.ts +112 -0
  99. package/src/linux/container-settle.ts +97 -0
  100. package/src/linux/container.ts +60 -0
  101. package/src/linux/fake-quadlet.ts +127 -0
  102. package/src/linux/index.ts +11 -3
  103. package/src/linux/providers.ts +2 -0
  104. package/src/linux/systemctl.ts +9 -0
  105. package/src/opnsense/alias-form.test.ts +89 -0
  106. package/src/opnsense/alias-form.ts +53 -0
  107. package/src/opnsense/alias.test.ts +203 -0
  108. package/src/opnsense/alias.ts +117 -0
  109. package/src/opnsense/category-form.test.ts +54 -0
  110. package/src/opnsense/category-form.ts +20 -0
  111. package/src/opnsense/category.test.ts +138 -0
  112. package/src/opnsense/category.ts +82 -0
  113. package/src/opnsense/credentials.ts +41 -0
  114. package/src/opnsense/fake-opnsense.ts +79 -0
  115. package/src/opnsense/group-form.test.ts +75 -0
  116. package/src/opnsense/group-form.ts +25 -0
  117. package/src/opnsense/group.test.ts +141 -0
  118. package/src/opnsense/group.ts +78 -0
  119. package/src/opnsense/index.ts +45 -0
  120. package/src/opnsense/policy.ts +49 -0
  121. package/src/opnsense/providers.ts +21 -0
  122. package/src/opnsense/resource.ts +146 -0
  123. package/src/opnsense/wire.ts +56 -0
  124. package/src/opnsense/write-refusal.test.ts +148 -0
  125. package/src/proxmox/storage-form.ts +144 -0
  126. package/src/proxmox/storage-read-failure.test.ts +214 -0
  127. package/src/proxmox/storage-wire.ts +124 -0
  128. package/src/proxmox/storage.ts +165 -175
  129. package/src/unifi/fake-unifi.ts +60 -0
  130. package/src/unifi/firewall-zone-form.ts +65 -0
  131. package/src/unifi/firewall-zone.test.ts +88 -0
  132. package/src/unifi/firewall-zone.ts +66 -0
  133. package/src/unifi/index.ts +26 -0
  134. package/src/unifi/network-form.ts +207 -0
  135. package/src/unifi/network.test.ts +209 -0
  136. package/src/unifi/network.ts +83 -0
  137. package/src/unifi/policy.ts +37 -0
  138. package/src/unifi/providers.ts +15 -0
  139. package/src/unifi/resource.test.ts +133 -0
  140. package/src/unifi/resource.ts +131 -0
@@ -0,0 +1,133 @@
1
+ /**
2
+ * `unifiOperations` in isolation, against a synthetic spec — no real UniFi SDK, no HTTP. Proves
3
+ * the read-only engine's own contract: `Unowned` on a cold read (H1), `noop`/`update` diff,
4
+ * `reconcile` succeeding with ZERO write calls on an exact match (the H6 forced post-adoption
5
+ * call this engine has to survive without writing), and a typed `UnifiWriteRefused` — never a
6
+ * silent success, never a raw SDK write — every other time `reconcile` or `delete` runs.
7
+ *
8
+ * `network.test.ts`/`firewall-zone.test.ts` prove the same contract again against the REAL SDK
9
+ * and a fake HTTP server, and additionally prove that no non-`GET` request is ever sent.
10
+ */
11
+ import { describe, expect, test } from 'bun:test';
12
+ import { Unowned } from 'alchemy/AdoptPolicy';
13
+ import type { UnifiNetworkOpContext } from '@distilled.cloud/unifi-network/Protocol';
14
+ import * as Effect from 'effect/Effect';
15
+ import { fakeUnifiLayer } from './fake-unifi.ts';
16
+ import { UnifiWriteRefused } from './policy.ts';
17
+ import { type UnifiSpec, unifiOperations } from './resource.ts';
18
+
19
+ interface WidgetProps {
20
+ id: string;
21
+ value: number;
22
+ }
23
+ type WidgetLive = WidgetProps;
24
+ type WidgetAttrs = WidgetProps;
25
+
26
+ /** `live: undefined` models a declared object nothing on the site matches. */
27
+ const specFor = (
28
+ live: WidgetLive | undefined,
29
+ ): UnifiSpec<WidgetProps, WidgetLive, WidgetAttrs, never> => ({
30
+ type: 'Test.Widget',
31
+ describe: (props) => `widgets/${props.id}`,
32
+ fetchLive: () => Effect.succeed(live),
33
+ attributes: (l) => l,
34
+ matches: (attrs, props) => attrs.value === props.value,
35
+ });
36
+
37
+ const MATCH: WidgetProps = { id: 'w1', value: 1 };
38
+ const DRIFTED_LIVE: WidgetLive = { id: 'w1', value: 99 };
39
+
40
+ /**
41
+ * `UnifiSpec`'s `fetchLive` is typed against the real `UnifiNetworkOpContext` (`resource.ts`'s
42
+ * own header explains why the engine is not generic over it) even though `specFor` above never
43
+ * touches HTTP — so every run here still needs A layer satisfying that context. This one fails
44
+ * the test outright if anything ever calls it, which is itself part of the write-refusal proof:
45
+ * the synthetic spec has no operation that could, but if one ever did, this is not a silent pass.
46
+ */
47
+ const unreachableFetch = (() => {
48
+ throw new Error('resource.test.ts: unexpected HTTP call from a spec with no HTTP operations');
49
+ }) as unknown as typeof globalThis.fetch;
50
+
51
+ const run = <A, E>(effect: Effect.Effect<A, E, UnifiNetworkOpContext>) =>
52
+ Effect.runPromise(effect.pipe(Effect.provide(fakeUnifiLayer(unreachableFetch))));
53
+
54
+ const refusal = async (
55
+ effect: Effect.Effect<unknown, UnifiWriteRefused, UnifiNetworkOpContext>,
56
+ ) => {
57
+ const failure = await run(Effect.flip(effect));
58
+ expect(failure).toBeInstanceOf(UnifiWriteRefused);
59
+ return failure;
60
+ };
61
+
62
+ describe('unifiOperations.readHandler', () => {
63
+ test('cold read (no persisted output) of a match answers Unowned', async () => {
64
+ const attrs = await run(
65
+ unifiOperations(specFor(MATCH)).readHandler({ olds: MATCH, output: undefined }),
66
+ );
67
+ expect(Unowned.is(attrs)).toBe(true);
68
+ expect(attrs).toEqual(MATCH);
69
+ });
70
+
71
+ test('warm read (persisted output says ours already) answers plain attributes', async () => {
72
+ const attrs = await run(
73
+ unifiOperations(specFor(MATCH)).readHandler({ olds: MATCH, output: MATCH }),
74
+ );
75
+ expect(Unowned.is(attrs)).toBe(false);
76
+ expect(attrs).toEqual(MATCH);
77
+ });
78
+
79
+ test('a genuinely absent object answers undefined either way', async () => {
80
+ const attrs = await run(
81
+ unifiOperations(specFor(undefined)).readHandler({ olds: MATCH, output: MATCH }),
82
+ );
83
+ expect(attrs).toBeUndefined();
84
+ });
85
+ });
86
+
87
+ describe('unifiOperations.diff', () => {
88
+ test('no persisted output defers to the engine default (undefined)', async () => {
89
+ const diff = await run(unifiOperations(specFor(MATCH)).diff(MATCH, undefined));
90
+ expect(diff).toBeUndefined();
91
+ });
92
+
93
+ test('declared props match live -- noop', async () => {
94
+ const diff = await run(unifiOperations(specFor(MATCH)).diff(MATCH, MATCH));
95
+ expect(diff).toEqual({ action: 'noop' });
96
+ });
97
+
98
+ test('live object drifted from the declaration -- update, never silently absorbed', async () => {
99
+ const diff = await run(unifiOperations(specFor(DRIFTED_LIVE)).diff(MATCH, MATCH));
100
+ expect(diff).toEqual({ action: 'update' });
101
+ });
102
+
103
+ test('live object vanished since it was adopted -- update, never a quiet noop', async () => {
104
+ const diff = await run(unifiOperations(specFor(undefined)).diff(MATCH, MATCH));
105
+ expect(diff).toEqual({ action: 'update' });
106
+ });
107
+ });
108
+
109
+ describe('unifiOperations.reconcile', () => {
110
+ test('exact match: zero write calls (the spec has none), returns live attributes -- H6', async () => {
111
+ const attrs = await run(unifiOperations(specFor(MATCH)).reconcile(MATCH));
112
+ expect(attrs).toEqual(MATCH);
113
+ });
114
+
115
+ test('missing live object refuses "create", never fabricates one', async () => {
116
+ const failure = await refusal(unifiOperations(specFor(undefined)).reconcile(MATCH));
117
+ expect(failure.action).toBe('create');
118
+ expect(failure.type).toBe('Test.Widget');
119
+ expect(failure.identity).toBe('widgets/w1');
120
+ });
121
+
122
+ test('drifted live object refuses "update", never converges by writing', async () => {
123
+ const failure = await refusal(unifiOperations(specFor(DRIFTED_LIVE)).reconcile(MATCH));
124
+ expect(failure.action).toBe('update');
125
+ });
126
+ });
127
+
128
+ describe('unifiOperations.destroy', () => {
129
+ test('always refuses "delete" -- the spec has no destroy operation at all', async () => {
130
+ const failure = await refusal(unifiOperations(specFor(MATCH)).destroy(MATCH));
131
+ expect(failure.action).toBe('delete');
132
+ });
133
+ });
@@ -0,0 +1,131 @@
1
+ /**
2
+ * One shape for every UniFi Network read-only object — `Unifi.Network`, `Unifi.FirewallZone`,
3
+ * and whatever import adds next.
4
+ *
5
+ * ★ MIRRORS `../discord/resource.ts` (S7-S10, H1, H5), NOT `../netbox/resource.ts`. Both are
6
+ * "marker-less API" families (S8): UniFi Network objects carry no tag or metadata field this
7
+ * stack could stamp as ownership, so a cold read of a match is `Unowned(attrs)` exactly like
8
+ * `Cloudflare/Snippets/Snippet.ts@v2.0.0-beta.79#read`, the reference implementation S8 and H1
9
+ * both cite. `adopt(true)`, piped on by each resource's convenience constructor (H5), turns
10
+ * that into a silent one-time bind instead of an `OwnedBySomeoneElse` refusal.
11
+ *
12
+ * ⛔ THE DIFFERENCE FROM EVERY OTHER FAMILY HERE: THERE IS NO `create`, `update` OR `destroy` IN
13
+ * THIS SPEC AT ALL. Tim's rule, 2026-09-24 (`policy.ts`): UniFi and OPNsense are read-only. So
14
+ * `reconcile` below can only ever refuse or report an exact match — it has no write path to
15
+ * fall into by accident, and no resource file wired through this engine can reach one either.
16
+ *
17
+ * ⚠️ RECONCILE STILL HAS TO SUCCEED ON AN EXACT-MATCH ADOPTION, NOT JUST REFUSE UNCONDITIONALLY.
18
+ * Measured against `Plan.ts@v2.0.0-beta.79` (`forceUpdateAfterAdoption`, H6): beta.79 forces
19
+ * ONE reconcile call after every cold adoption, whether or not the diff said noop — so a
20
+ * resource whose `reconcile` always failed would break the ordinary "adopt an unchanged
21
+ * object" deploy, not just a real write. The fix, exactly like `discordOperations.reconcile`:
22
+ * read the live object again, and refuse ONLY when it is missing (would need a create) or
23
+ * drifted (would need a write to converge). An exact match returns the live attributes and
24
+ * calls nothing — S10's "zero write calls when there is no drift" holds even under the forced
25
+ * post-adoption call.
26
+ *
27
+ * ⚠️ `NotFound` IS FOLDED TO "ABSENT" INSIDE EACH RESOURCE FILE'S OWN `fetchLive`, NOT HERE —
28
+ * same split netbox's `resource.ts` uses and for the same reason: `Effect.catchTag`'s
29
+ * tag-literal inference needs a concrete error union to resolve `"NotFound"` against, and a
30
+ * spec's `E` is only concrete at each resource file's own call site. Every OTHER failure (a
31
+ * real 5xx, a network error, `Forbidden`) is NOT folded anywhere — it fails the plan loudly,
32
+ * per the task's own rule: only a genuine not-found may mean absent.
33
+ */
34
+ import { Unowned } from 'alchemy/AdoptPolicy';
35
+ import { isResolved } from 'alchemy/Diff';
36
+ import type { Input } from 'alchemy/Input';
37
+ import { CredentialsFromEnv } from '@distilled.cloud/unifi-network/Credentials';
38
+ import type { UnifiNetworkOpContext } from '@distilled.cloud/unifi-network/Protocol';
39
+ import * as Effect from 'effect/Effect';
40
+ import type * as HttpClient from 'effect/unstable/http/HttpClient';
41
+ import { refuseWrite } from './policy.ts';
42
+
43
+ /** What every handler needs from the caller's runtime once `CredentialsFromEnv` is baked in. */
44
+ export type UnifiRequirements = HttpClient.HttpClient;
45
+
46
+ /**
47
+ * One UniFi Network object's read. `Live` is whatever the SDK decodes (a `NetworkDetails`, a
48
+ * `FirewallZone`, …); `E` is left to each resource file to declare (its own `fetchLive`'s error
49
+ * union, with `NotFound` already folded away) so the precise per-operation error union flows
50
+ * through instead of being widened by hand here.
51
+ */
52
+ export type UnifiSpec<Props extends object, Live, Attributes extends object, E> = {
53
+ /** The vendor type string, e.g. `Unifi.Network` — for the refusal message only. */
54
+ readonly type: string;
55
+ /** Already folds a genuine not-found to `undefined`; nothing else is folded (see header). */
56
+ readonly fetchLive: (props: Props) => Effect.Effect<Live | undefined, E, UnifiNetworkOpContext>;
57
+ readonly attributes: (live: Live, props: Props) => Attributes;
58
+ readonly matches: (attributes: Attributes, props: Props) => boolean;
59
+ /** For the refusal message and read-back failures. */
60
+ readonly describe: (props: Props) => string;
61
+ };
62
+
63
+ export const unifiOperations = <Props extends object, Live, Attributes extends object, E>(
64
+ spec: UnifiSpec<Props, Live, Attributes, E>,
65
+ ) => {
66
+ const read = (props: Props) =>
67
+ spec
68
+ .fetchLive(props)
69
+ .pipe(Effect.map((live) => (live === undefined ? undefined : spec.attributes(live, props))));
70
+
71
+ return {
72
+ read,
73
+
74
+ readHandler: ({ olds, output }: { olds: Props; output: Attributes | undefined }) =>
75
+ Effect.gen(function* () {
76
+ const attrs = yield* read(olds);
77
+ if (attrs === undefined) return undefined;
78
+ // Warm read (persisted `output` says a prior apply already adopted this exact object):
79
+ // plain attributes. Cold read (fresh store, or state lost): no ownership marker on this
80
+ // vendor exists to tell "ours" from "someone else's" apart, so Unowned until adopt(true).
81
+ return output !== undefined ? attrs : Unowned(attrs);
82
+ }),
83
+
84
+ diff: (news: Input<Props>, output: Attributes | undefined) =>
85
+ Effect.gen(function* () {
86
+ if (output === undefined || !isResolved(news)) return undefined;
87
+ const live = yield* read(news);
88
+ if (live === undefined) return { action: 'update' } as const;
89
+ return spec.matches(live, news)
90
+ ? ({ action: 'noop' } as const)
91
+ : ({ action: 'update' } as const);
92
+ }),
93
+
94
+ reconcile: (news: Props) =>
95
+ Effect.gen(function* () {
96
+ const live = yield* read(news);
97
+ if (live === undefined) return yield* refuseWrite(spec.type, spec.describe(news), 'create');
98
+ if (!spec.matches(live, news)) {
99
+ return yield* refuseWrite(spec.type, spec.describe(news), 'update');
100
+ }
101
+ // Exact match: the object already is what is declared. Zero write calls (S10), correct
102
+ // whether this is a routine deploy or beta.79's forced post-adoption reconcile (H6).
103
+ return live;
104
+ }),
105
+
106
+ destroy: (olds: Props) => refuseWrite(spec.type, spec.describe(olds), 'delete'),
107
+ };
108
+ };
109
+
110
+ /**
111
+ * The four provider handlers for a spec'd read-only UniFi object, wired once.
112
+ *
113
+ * ⛔ `list` ANSWERS EMPTY. `GET /v1/sites/{siteId}/networks` answers every network on the site;
114
+ * adoption stays explicit, the same reasoning `Proxmox.User`'s and NetBox's `list` give.
115
+ */
116
+ export const unifiHandlers = <Props extends object, Live, Attributes extends object, E>(
117
+ spec: UnifiSpec<Props, Live, Attributes, E>,
118
+ ) => {
119
+ const ops = unifiOperations(spec);
120
+ const withCredentials = <A, Err>(effect: Effect.Effect<A, Err, UnifiNetworkOpContext>) =>
121
+ Effect.provide(effect, CredentialsFromEnv);
122
+ return {
123
+ list: () => Effect.succeed([]),
124
+ read: (args: { olds: Props; output: Attributes | undefined }) =>
125
+ withCredentials(ops.readHandler(args)),
126
+ diff: (args: { news: Input<Props>; output: Attributes | undefined }) =>
127
+ withCredentials(ops.diff(args.news, args.output)),
128
+ reconcile: (args: { news: Props }) => withCredentials(ops.reconcile(args.news)),
129
+ delete: (args: { olds: Props }) => withCredentials(ops.destroy(args.olds)),
130
+ };
131
+ };