@coda-console/plugin-check 0.8.23 → 0.8.25

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,65 @@
1
+ import assert from 'node:assert/strict';
2
+ import { readFileSync } from 'node:fs';
3
+ import { test } from 'node:test';
4
+ import { createTestHost } from '@coda-console/plugin-check/test-host';
5
+ import { activate } from '../main.mjs';
6
+
7
+ const { contributes, uses } = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8'));
8
+ const NOTE = 'notes/note@1';
9
+
10
+ async function started() {
11
+ const host = createTestHost({ plugin: { protocols: contributes.protocols, protocolImplementations: contributes.protocolImplementations, uses } });
12
+ await activate(host.coda);
13
+ return host;
14
+ }
15
+ const note = { kind: NOTE, id: 'any' };
16
+
17
+ test('find lists the welcome note the module wrote on activation', async () => {
18
+ const host = await started();
19
+ assert.deepEqual(await host.call(note, 'find'), { seen: 0, notes: [{ key: '欢迎', title: '欢迎' }] });
20
+ assert.deepEqual(await host.call(note, 'find', { query: '没有' }), { seen: 0, notes: [] });
21
+ await host.close();
22
+ });
23
+
24
+ test('activating over notes already there leaves them as they are', async () => {
25
+ const host = createTestHost({ plugin: { protocols: contributes.protocols, protocolImplementations: contributes.protocolImplementations, uses } });
26
+ await host.coda.put({ kind: NOTE, key: '旧的' }, { spec: { title: '旧的', body: '先前记的' } });
27
+ await activate(host.coda);
28
+ assert.deepEqual((await host.call(note, 'find')).notes, [{ key: '旧的', title: '旧的' }]);
29
+ await host.close();
30
+ });
31
+
32
+ test('add makes the note and publishes note_notes.added each time; the module counts the events it is given', async () => {
33
+ const host = await started();
34
+ const input = { title: '买牛奶', body: '两盒', project: 'p1' };
35
+ assert.deepEqual(await host.call(note, 'add', input), { key: '买牛奶', notified: true });
36
+ await host.call(note, 'add', input);
37
+ assert.equal(host.emitted.length, 2);
38
+ const [event] = host.emitted;
39
+ assert.equal(event.name, 'note_notes.added');
40
+ assert.equal(event.kind, NOTE);
41
+ assert.deepEqual(event.data, { key: '买牛奶', title: '买牛奶', body: '两盒' });
42
+ assert.deepEqual((await host.call(note, 'find', { query: '牛奶' })).notes, [{ key: '买牛奶', title: '买牛奶' }]);
43
+ await host.emit({ ...event, publisher: 'notes' });
44
+ assert.equal((await host.call(note, 'find')).seen, 1);
45
+ await host.close();
46
+ });
47
+
48
+ test('an input over the limits is refused before anything is written or published', async () => {
49
+ const host = await started();
50
+ await assert.rejects(host.call(note, 'add', { title: '长', body: 'x'.repeat(10001), project: 'p1' }), { code: 'invalid_input' });
51
+ assert.deepEqual((await host.call(note, 'find')).notes.map(entry => entry.key), ['欢迎']);
52
+ assert.deepEqual(host.emitted, []);
53
+ await host.close();
54
+ });
55
+
56
+ test('add that cannot publish still saves the note, and says it did not tell anyone', async () => {
57
+ const host = createTestHost({ plugin: { protocols: contributes.protocols, protocolImplementations: contributes.protocolImplementations, uses } });
58
+ const coda = { ...host.coda, emit: async () => { throw host.coda.error('invalid_input', '项目 nope 不存在'); } };
59
+ await activate(coda);
60
+ const added = await host.call(note, 'add', { title: '买牛奶', body: '两盒', project: 'nope' });
61
+ assert.deepEqual(added, { key: '买牛奶', notified: false, reason: added.reason });
62
+ assert.match(String(added.reason), /项目 nope 不存在/);
63
+ assert.deepEqual((await host.call(note, 'find')).notes.map(entry => entry.key).sort(), ['买牛奶', '欢迎']);
64
+ await host.close();
65
+ });
@@ -0,0 +1,22 @@
1
+ {
2
+ "name": "uptime",
3
+ "displayName": "运行时间(模板)",
4
+ "description": "最小的资源插件模板",
5
+ "version": "1.0.0",
6
+ "contributes": {
7
+ "resources": [
8
+ {
9
+ "id": "uptime",
10
+ "title": "运行时间",
11
+ "command": "node \"$CODA_PLUGIN_PATH/scripts/resources.mjs\"",
12
+ "maxAge": "PT1M",
13
+ "actions": {
14
+ "mark": {
15
+ "command": "node \"$CODA_PLUGIN_PATH/scripts/mark.mjs\"",
16
+ "confirm": "记下此刻?\n之后显示距这次记下过了多久。"
17
+ }
18
+ }
19
+ }
20
+ ]
21
+ }
22
+ }
@@ -0,0 +1,6 @@
1
+ // 「记下此刻」按钮的动作:Coda 在点按钮(并确认)后运行它,退出码 0 就算成功,随后重读这个类型。
2
+ // 按钮带的 args 在 CODA_PANEL_ARGS 里(JSON),这个按钮没带。
3
+ import { writeFileSync } from 'node:fs';
4
+ import { join } from 'node:path';
5
+
6
+ writeFileSync(join(process.env.CODA_PLUGIN_STATE, 'mark.json'), JSON.stringify({ at: new Date().toISOString() }));
@@ -0,0 +1,35 @@
1
+ // 「运行时间」资源类型的取数命令:Coda 在打开资源页或点「刷新」时用 `sh -c` 运行它,标准输出就是资源的 JSON
2
+ // (docs/plugins.md「资源」)。`node` 是 Coda 后端自己的 Node;只用 Node 自带的模块。
3
+ // 读不懂的键会让这次读取失败;格式里没有说明性的字段,每个资源只有名字、读数、一行错误和按钮。
4
+ import { execFileSync } from 'node:child_process';
5
+ import { readFileSync } from 'node:fs';
6
+ import { cpus, loadavg, uptime } from 'node:os';
7
+ import { join } from 'node:path';
8
+
9
+ // `uptime` 的负载在 macOS 写作「load averages: 1.2 1.3 1.4」,在 Linux 写作「load average: 1.2, 1.3, 1.4」;读不到就用 Node 的。
10
+ function loads() {
11
+ try {
12
+ const line = execFileSync('uptime', { encoding: 'utf8', timeout: 5000 });
13
+ const found = /load averages?:\s*([\d.]+),?\s+([\d.]+),?\s+([\d.]+)/.exec(line);
14
+ if (found) return found.slice(1, 4).map(Number);
15
+ } catch {}
16
+ return loadavg();
17
+ }
18
+ const since = seconds => {
19
+ const days = Math.floor(seconds / 86400), hours = Math.floor(seconds % 86400 / 3600), minutes = Math.floor(seconds % 3600 / 60);
20
+ return days ? `${days} 天 ${hours} 小时` : hours ? `${hours} 小时 ${minutes} 分钟` : `${minutes} 分钟`;
21
+ };
22
+ // 按钮动作记下的时刻,存在插件的状态目录里(CODA_PLUGIN_STATE,跨读取、跨版本保留)。
23
+ function marked() {
24
+ try { return JSON.parse(readFileSync(join(process.env.CODA_PLUGIN_STATE, 'mark.json'), 'utf8')).at; } catch { return null; }
25
+ }
26
+
27
+ const [one] = loads(), cores = cpus().length, up = uptime(), mark = marked();
28
+ // 这台机器的东西只有一个资源,不写名字,也不写身份(identity 留给账号这类在几台机器上是同一个的)。
29
+ // 条一律画「还剩多少」:这里是 CPU 按 1 分钟负载算的余量;不知道比例时不写 fraction。读数带的时刻(time)写在数值后面。
30
+ const values = [
31
+ { label: 'CPU 余量', value: `${one.toFixed(2)} / ${cores} 核`, fraction: Math.round(Math.max(0, Math.min(1, 1 - one / cores)) * 1000) / 1000 },
32
+ { label: '已开机', value: since(up), time: new Date(Date.now() - up * 1000).toISOString() },
33
+ ...(mark ? [{ label: '距上次记下', value: since((Date.now() - Date.parse(mark)) / 1000) }] : []),
34
+ ];
35
+ process.stdout.write(JSON.stringify({ resources: [{ values, actions: [{ label: '记下此刻', action: 'mark' }] }] }));