dsh-offwork 1.1.2 → 1.2.2

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.
@@ -0,0 +1,130 @@
1
+ import test from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import { mkdtemp, writeFile, readFile, stat, rm } from 'node:fs/promises';
4
+ import { tmpdir } from 'node:os';
5
+ import { join } from 'node:path';
6
+ import { createServer } from 'node:http';
7
+ import { createOperations, DEFAULT_SETTINGS, installOperations } from '../index.js';
8
+ import { createSettingsAccess, createSettingsHandler } from '../settings-api.js';
9
+ async function fixture(t) {
10
+ const dir = await mkdtemp(join(tmpdir(), 'offwork-settings-api-'));
11
+ t.after(() => rm(dir, { recursive: true, force: true }));
12
+ return dir;
13
+ }
14
+ test('stable migration preserves original bytes and never overwrites an existing target', async (t) => {
15
+ const dir = await fixture(t),
16
+ legacy = join(dir, 'legacy.json'),
17
+ target = join(dir, 'storage', 'settings.json');
18
+ const bytes = JSON.stringify({ ...DEFAULT_SETTINGS, monthlySalary: 12345 });
19
+ await writeFile(legacy, bytes);
20
+ const operate = createOperations(target, { legacyFile: legacy });
21
+ const result = await operate('get');
22
+ assert.equal(result.ok, true);
23
+ assert.equal(result.settings.monthlySalary, 12345);
24
+ assert.equal(await readFile(legacy, 'utf8'), bytes);
25
+ assert.equal((await stat(target)).mode & 0o777, 0o600);
26
+ await operate('set', { monthlySalary: 444 });
27
+ await writeFile(legacy, JSON.stringify(DEFAULT_SETTINGS));
28
+ assert.equal(
29
+ (await createOperations(target, { legacyFile: legacy })('get')).settings.monthlySalary,
30
+ 444,
31
+ );
32
+ });
33
+ test('corrupt legacy data fails closed without creating a defaults file', async (t) => {
34
+ const dir = await fixture(t),
35
+ legacy = join(dir, 'legacy.json'),
36
+ target = join(dir, 'storage', 'settings.json');
37
+ await writeFile(legacy, '{broken');
38
+ const result = await createOperations(target, { legacyFile: legacy })('get');
39
+ assert.equal(result.ok, false);
40
+ await assert.rejects(readFile(target), { code: 'ENOENT' });
41
+ assert.equal(await readFile(legacy, 'utf8'), '{broken');
42
+ });
43
+ test('UI settings API rejects cross-origin, missing header and non-JSON writes', async (t) => {
44
+ const dir = await fixture(t),
45
+ operate = createOperations(join(dir, 'settings.json')),
46
+ access = createSettingsAccess(dir);
47
+ const server = createServer(createSettingsHandler(operate, access, () => undefined));
48
+ await new Promise((r) => server.listen(0, '127.0.0.1', r));
49
+ t.after(() => new Promise((r) => server.close(r)));
50
+ const url = `http://127.0.0.1:${server.address().port}/offwork-clock/settings`;
51
+ assert.equal((await fetch(url)).status, 403);
52
+ assert.equal(
53
+ (await fetch(url, { headers: { 'x-offwork-client': '1', 'sec-fetch-site': 'cross-site' } }))
54
+ .status,
55
+ 403,
56
+ );
57
+ assert.equal(
58
+ (await fetch(url, { headers: { 'x-offwork-client': '1', origin: 'https://evil.example' } }))
59
+ .status,
60
+ 403,
61
+ );
62
+ const headers = { 'x-offwork-client': '1', 'content-type': 'application/json' };
63
+ assert.equal((await (await fetch(url, { headers })).json()).agentAccess, false);
64
+ assert.equal(
65
+ (await fetch(url, { method: 'POST', headers: { 'x-offwork-client': '1' }, body: '{}' })).status,
66
+ 415,
67
+ );
68
+ const saved = await (
69
+ await fetch(url, {
70
+ method: 'POST',
71
+ headers,
72
+ body: JSON.stringify({ action: 'set', settings: { dailySalary: 234 } }),
73
+ })
74
+ ).json();
75
+ assert.equal(saved.settings.dailySalary, 234);
76
+ await fetch(url, {
77
+ method: 'POST',
78
+ headers,
79
+ body: JSON.stringify({ action: 'access', allowed: true }),
80
+ });
81
+ assert.equal(await access.get(), true);
82
+ assert.equal(
83
+ (await fetch(url, { method: 'POST', headers, body: 'x'.repeat(17000) })).status,
84
+ 413,
85
+ );
86
+ });
87
+ test('private settings default to unauthenticated denial before reading data', async (t) => {
88
+ const dir = await fixture(t);
89
+ let reads = 0;
90
+ const server = createServer(
91
+ createSettingsHandler(async () => {
92
+ reads++;
93
+ return { ok: true };
94
+ }, createSettingsAccess(dir)),
95
+ );
96
+ await new Promise((r) => server.listen(0, '127.0.0.1', r));
97
+ t.after(() => new Promise((r) => server.close(r)));
98
+ const response = await fetch(`http://127.0.0.1:${server.address().port}/offwork-clock/settings`, {
99
+ headers: { 'x-offwork-client': '1' },
100
+ });
101
+ assert.equal(response.status, 401);
102
+ assert.equal(reads, 0);
103
+ });
104
+
105
+ test('agent salary access defaults off, can be explicitly enabled and revoked', async (t) => {
106
+ const dir = await fixture(t),
107
+ operate = createOperations(join(dir, 'settings.json')),
108
+ access = createSettingsAccess(dir);
109
+ let tool;
110
+ installOperations(
111
+ {
112
+ effect: (fn) => fn(),
113
+ commands: { register: () => () => {} },
114
+ tools: {
115
+ register: (t) => {
116
+ tool = t;
117
+ return () => {};
118
+ },
119
+ },
120
+ },
121
+ operate,
122
+ access,
123
+ );
124
+ assert.equal((await tool.execute({ action: 'get' })).ok, false);
125
+ await access.set(true);
126
+ assert.equal((await tool.execute({ action: 'get' })).ok, true);
127
+ await access.set(false);
128
+ assert.equal((await tool.execute({ action: 'set', settings: { monthlySalary: 777 } })).ok, false);
129
+ assert.equal((await operate('get')).configured, false);
130
+ });
package/tests/ui.test.mjs CHANGED
@@ -125,7 +125,10 @@ test('Client uses the exact tested engine and a single surface', async () => {
125
125
  assert.match(source, /removeEventListener\('storage'/);
126
126
  assert.match(source, /ro\?\.disconnect/);
127
127
  assert.match(source, /generation\.current/);
128
- assert.doesNotMatch(source, /document\.body|window\.open|fetch\(/);
128
+ assert.doesNotMatch(source, /document\.body|window\.open/);
129
+ assert.match(source, /offwork-clock\/settings/);
130
+ assert.match(source, /credentials: 'same-origin'/);
131
+ assert.doesNotMatch(source, /remote\.commands\.execute/);
129
132
  assert.match(source, /money\(safeState\.earned\)/);
130
133
  assert.match(source, /owc-glass/);
131
134
  assert.match(source, /--owc-alpha/);
package/tools/build.mjs CHANGED
@@ -16,6 +16,9 @@ const moduleNames = [
16
16
  'hydration.js',
17
17
  'hydration-store.js',
18
18
  'countdown.js',
19
+ 'companion-state.js',
20
+ 'companion-image.js',
21
+ 'companion-app.js',
19
22
  ];
20
23
  const [entry, ...modules] = await Promise.all(
21
24
  moduleNames.map((name) => readFile(new URL(name, root), 'utf8')),
@@ -30,7 +33,10 @@ if (entry.split(marker).length !== 2) {
30
33
  const pure = modules
31
34
  .map((source) =>
32
35
  source
33
- .replace(/^import\s+\{[^}]+\}\s+from '\.\/(?:hydration|clock)\.js';\n/gm, '')
36
+ .replace(
37
+ /^import\s+\{[^}]+\}\s+from '\.\/(?:hydration|clock|countdown|ui-utils)\.js';\n/gm,
38
+ '',
39
+ )
34
40
  .replace(/^export /gm, ''),
35
41
  )
36
42
  .join('\n');
@@ -71,6 +71,10 @@ assert.equal(packed.version, pkg.version);
71
71
  for (const path of [
72
72
  'package.json',
73
73
  'index.js',
74
+ 'settings-api.js',
75
+ 'companion-state.js',
76
+ 'companion-image.js',
77
+ 'companion-app.js',
74
78
  'media-server.js',
75
79
  'media-manifest.js',
76
80
  'client.js',