@remnic/core 9.10.0 → 9.11.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.
@@ -7,10 +7,11 @@
7
7
  * correction actually blocks future extraction of matching content.
8
8
  */
9
9
  import { strict as assert } from "node:assert";
10
- import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
10
+ import { mkdir, writeFile } from "node:fs/promises";
11
11
  import path from "node:path";
12
- import { tmpdir } from "node:os";
13
12
  import { test } from "node:test";
13
+
14
+ import { withTempDir as managedWithTempDir } from "./testing/tmp-dir.js";
14
15
  import {
15
16
  REDACTION_RULES_SUBDIR,
16
17
  compileRedactionPattern,
@@ -20,14 +21,8 @@ import {
20
21
  } from "./extraction-redaction-rules.js";
21
22
  import { validateRedactionPattern } from "./correction/correction-contract.js";
22
23
 
23
- async function withTempDir<T>(fn: (dir: string) => Promise<T>): Promise<T> {
24
- const dir = await mkdtemp(path.join(tmpdir(), "remnic-redact-"));
25
- try {
26
- return await fn(dir);
27
- } finally {
28
- await rm(dir, { recursive: true, force: true });
29
- }
30
- }
24
+ const withTempDir = <T>(fn: (dir: string) => Promise<T>): Promise<T> =>
25
+ managedWithTempDir(fn, "remnic-redact-");
31
26
 
32
27
  async function writeRule(stateDir: string, pattern: string): Promise<void> {
33
28
  const dir = path.join(stateDir, REDACTION_RULES_SUBDIR);
@@ -3,10 +3,11 @@
3
3
  */
4
4
 
5
5
  import assert from "node:assert/strict";
6
- import os from "node:os";
7
6
  import path from "node:path";
8
7
  import test from "node:test";
9
- import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
8
+ import { mkdir, readFile, writeFile } from "node:fs/promises";
9
+
10
+ import { withTempDir as managedWithTempDir } from "../testing/tmp-dir.js";
10
11
 
11
12
  import { appendEdge, graphFilePath, graphsDir, type GraphEdge } from "../graph.js";
12
13
  import {
@@ -85,14 +86,8 @@ async function readEdgesFile(memoryDir: string): Promise<GraphEdge[]> {
85
86
  .map((l) => JSON.parse(l) as GraphEdge);
86
87
  }
87
88
 
88
- async function withTempDir(fn: (dir: string) => Promise<void>): Promise<void> {
89
- const dir = await mkdtemp(path.join(os.tmpdir(), "remnic-graph-decay-test-"));
90
- try {
91
- await fn(dir);
92
- } finally {
93
- await rm(dir, { recursive: true, force: true });
94
- }
95
- }
89
+ const withTempDir = (fn: (dir: string) => Promise<void>): Promise<void> =>
90
+ managedWithTempDir(fn, "remnic-graph-decay-test-");
96
91
 
97
92
  test("runs end-to-end on a fixture graph with fresh, mid-decay, stale edges", async () => {
98
93
  await withTempDir(async (memoryDir) => {
@@ -6,10 +6,11 @@
6
6
 
7
7
  import assert from "node:assert/strict";
8
8
  import { promises as fs } from "node:fs";
9
- import os from "node:os";
10
9
  import path from "node:path";
11
10
  import test from "node:test";
12
11
 
12
+ import { makeTempDir as managedMakeTempDir } from "../testing/tmp-dir.js";
13
+
13
14
  import { readPeer } from "./storage.js";
14
15
  import { migrateFromIdentityAnchor } from "./migrate-from-identity-anchor.js";
15
16
 
@@ -17,9 +18,7 @@ import { migrateFromIdentityAnchor } from "./migrate-from-identity-anchor.js";
17
18
  // Helpers
18
19
  // ──────────────────────────────────────────────────────────────────────────────
19
20
 
20
- async function makeTempDir(): Promise<string> {
21
- return fs.mkdtemp(path.join(os.tmpdir(), "peer-migrate-test-"));
22
- }
21
+ const makeTempDir = (): Promise<string> => managedMakeTempDir("peer-migrate-test-");
23
22
 
24
23
  /** Write a file, creating intermediate directories as needed. */
25
24
  async function writeFixture(filePath: string, content: string): Promise<void> {
@@ -7,10 +7,11 @@
7
7
 
8
8
  import assert from "node:assert/strict";
9
9
  import { promises as fs } from "node:fs";
10
- import os from "node:os";
11
10
  import path from "node:path";
12
11
  import test from "node:test";
13
12
 
13
+ import { makeTempDir as managedMakeTempDir } from "../testing/tmp-dir.js";
14
+
14
15
  import {
15
16
  appendInteractionLog,
16
17
  assertValidPeerId,
@@ -33,9 +34,7 @@ import {
33
34
  // Fixtures
34
35
  // ──────────────────────────────────────────────────────────────────────
35
36
 
36
- async function makeTempDir(): Promise<string> {
37
- return await fs.mkdtemp(path.join(os.tmpdir(), "peers-test-"));
38
- }
37
+ const makeTempDir = (): Promise<string> => managedMakeTempDir("peers-test-");
39
38
 
40
39
  function samplePeer(overrides: Partial<Peer> = {}): Peer {
41
40
  return {
@@ -4,16 +4,12 @@ import os from "node:os";
4
4
  import path from "node:path";
5
5
  import test from "node:test";
6
6
 
7
+ import { withTempDir as managedWithTempDir } from "../testing/tmp-dir.js";
8
+
7
9
  import { collectLocalSessionSummaries, compileRedactionRules, runLocalSessionSummaryCliCommand } from "./index.js";
8
10
 
9
- async function withTempDir<T>(fn: (dir: string) => Promise<T>): Promise<T> {
10
- const dir = await mkdtemp(path.join(os.tmpdir(), "remnic-session-summary-"));
11
- try {
12
- return await fn(dir);
13
- } finally {
14
- await rm(dir, { recursive: true, force: true });
15
- }
16
- }
11
+ const withTempDir = <T>(fn: (dir: string) => Promise<T>): Promise<T> =>
12
+ managedWithTempDir(fn, "remnic-session-summary-");
17
13
 
18
14
  test("collectLocalSessionSummaries produces metadata-only drafts without local paths or raw session keys", async () => {
19
15
  await withTempDir(async (dir) => {
@@ -0,0 +1,68 @@
1
+ import { after } from "node:test";
2
+ import { mkdtemp, rm } from "node:fs/promises";
3
+ import { mkdtempSync, rmSync } from "node:fs";
4
+ import { tmpdir } from "node:os";
5
+ import { join } from "node:path";
6
+
7
+ /**
8
+ * Shared temp-dir lifecycle for `@remnic/core` tests (#2083). The single source
9
+ * of truth within this package, so its tests are self-cleaning rather than
10
+ * relying solely on the run-level TMPDIR sandbox (Tier 1).
11
+ *
12
+ * `makeTempDir` / `makeTempDirSync` register the created directory for cleanup
13
+ * via a single file-scoped `after()` hook (registered at import time, before
14
+ * any test runs), so a bare `const dir = await makeTempDir()` call site needs
15
+ * no per-test teardown wiring. `withTempDir` scopes the directory to a callback
16
+ * and removes it in `finally`.
17
+ */
18
+ const pendingCleanup = new Set<string>();
19
+
20
+ // Registered at module load — importing this module from a test file installs
21
+ // one file-scoped after-all hook that removes every managed temp dir. Doing it
22
+ // here (not lazily inside a test) guarantees the hook binds to the file root
23
+ // rather than whichever test happened to allocate the first dir.
24
+ after(async () => {
25
+ const dirs = [...pendingCleanup];
26
+ pendingCleanup.clear();
27
+ // Surface cleanup failures (do not swallow): a dir that cannot be removed is
28
+ // a real signal. `force: true` already ignores a missing dir, so a rejection
29
+ // here means an actual teardown problem and should fail the file loudly.
30
+ await Promise.all(dirs.map((dir) => rm(dir, { recursive: true, force: true })));
31
+ });
32
+
33
+ /** Create a temp dir that is removed automatically after the file's tests. */
34
+ export async function makeTempDir(prefix = "remnic-test-"): Promise<string> {
35
+ const dir = await mkdtemp(join(tmpdir(), prefix));
36
+ pendingCleanup.add(dir);
37
+ return dir;
38
+ }
39
+
40
+ /** Synchronous {@link makeTempDir}. */
41
+ export function makeTempDirSync(prefix = "remnic-test-"): string {
42
+ const dir = mkdtempSync(join(tmpdir(), prefix));
43
+ pendingCleanup.add(dir);
44
+ return dir;
45
+ }
46
+
47
+ /** Run `fn` with a fresh temp dir, removing it in `finally`. */
48
+ export async function withTempDir<T>(
49
+ fn: (dir: string) => T | Promise<T>,
50
+ prefix = "remnic-test-",
51
+ ): Promise<T> {
52
+ const dir = await mkdtemp(join(tmpdir(), prefix));
53
+ try {
54
+ return await fn(dir);
55
+ } finally {
56
+ await rm(dir, { recursive: true, force: true });
57
+ }
58
+ }
59
+
60
+ /** Synchronous {@link withTempDir}. */
61
+ export function withTempDirSync<T>(fn: (dir: string) => T, prefix = "remnic-test-"): T {
62
+ const dir = mkdtempSync(join(tmpdir(), prefix));
63
+ try {
64
+ return fn(dir);
65
+ } finally {
66
+ rmSync(dir, { recursive: true, force: true });
67
+ }
68
+ }
@@ -22,11 +22,12 @@
22
22
 
23
23
  import assert from "node:assert/strict";
24
24
  import test from "node:test";
25
- import { mkdtemp, rm, writeFile, readFile, mkdir, stat, readdir } from "node:fs/promises";
25
+ import { rm, writeFile, readFile, mkdir, stat, readdir } from "node:fs/promises";
26
26
  import path from "node:path";
27
- import { tmpdir } from "node:os";
28
27
  import { gzipSync, gunzipSync } from "node:zlib";
29
28
 
29
+ import { makeTempDir as managedMakeTempDir } from "../testing/tmp-dir.js";
30
+
30
31
  import { exportCapsule } from "./capsule-export.js";
31
32
  import { importCapsule } from "./capsule-import.js";
32
33
  import { backupMemoryDir } from "./backup.js";
@@ -53,9 +54,7 @@ const FAST_SCRYPT: ScryptParams = {
53
54
 
54
55
  const TEST_PASSPHRASE = "hunter2-test-passphrase";
55
56
 
56
- async function makeTempDir(): Promise<string> {
57
- return mkdtemp(path.join(tmpdir(), "capsule-enc-test-"));
58
- }
57
+ const makeTempDir = (): Promise<string> => managedMakeTempDir("capsule-enc-test-");
59
58
 
60
59
  /**
61
60
  * Initialize a secure-store header in `memoryDir` and unlock the keyring.
@@ -4,16 +4,12 @@ import { tmpdir } from "node:os";
4
4
  import path from "node:path";
5
5
  import test from "node:test";
6
6
 
7
+ import { withTempDir as managedWithTempDir } from "./testing/tmp-dir.js";
8
+
7
9
  import { WriteQuarantineStore } from "./write-quarantine.js";
8
10
 
9
- async function withTempDir(fn: (dir: string) => Promise<void>): Promise<void> {
10
- const dir = await mkdtemp(path.join(tmpdir(), "remnic-quarantine-"));
11
- try {
12
- await fn(dir);
13
- } finally {
14
- await rm(dir, { recursive: true, force: true });
15
- }
16
- }
11
+ const withTempDir = (fn: (dir: string) => Promise<void>): Promise<void> =>
12
+ managedWithTempDir(fn, "remnic-quarantine-");
17
13
 
18
14
  function isInside(root: string, child: string): boolean {
19
15
  const rel = path.relative(path.resolve(root), path.resolve(child));