From 572f793061af0c6d3b36686a7537b260d76990a8 Mon Sep 17 00:00:00 2001 From: abearixong Date: Mon, 17 Nov 2025 20:01:44 +0800 Subject: [PATCH] fix: add test --- agent/ddns/cloudflare/index.ts | 8 ++++++++ agent/task.ts | 2 +- agent/test/common.ts | 13 +++++++++++++ agent/test/get-cf-list.ts | 10 ++++++++++ 4 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 agent/test/common.ts create mode 100644 agent/test/get-cf-list.ts diff --git a/agent/ddns/cloudflare/index.ts b/agent/ddns/cloudflare/index.ts index a1c7dff..405831a 100644 --- a/agent/ddns/cloudflare/index.ts +++ b/agent/ddns/cloudflare/index.ts @@ -32,6 +32,14 @@ export class CloudflareDDNS { if(!result.success) { throw new Error(`更新失败: ${JSON.stringify(result.errors)}`); } + console.log(`更新成功: ${domain} -> ${new_ip}`); return result; } + async getList(zone_id: string, api_token: string) { + const url = `https://api.cloudflare.com/client/v4/zones/${zone_id}/dns_records`; + return fetch(url, { + method: 'GET', + headers: this.makeHeader(api_token), + }).then(res => res.json()); + } } \ No newline at end of file diff --git a/agent/task.ts b/agent/task.ts index 2c13033..f03558f 100644 --- a/agent/task.ts +++ b/agent/task.ts @@ -8,7 +8,7 @@ const storage = createStorage({ }), }); -type CloudflareConfig = { +export type CloudflareConfig = { // Cloudflare 访问地址 website: string; domain: string; diff --git a/agent/test/common.ts b/agent/test/common.ts new file mode 100644 index 0000000..19d8093 --- /dev/null +++ b/agent/test/common.ts @@ -0,0 +1,13 @@ +import { createStorage } from "unstorage"; +import fsDriver from "unstorage/drivers/fs"; +import { CloudflareDDNS } from "@agent/ddns/cloudflare/index.ts"; +import { CloudflareConfig } from "@agent/task.ts"; +const storage = createStorage({ + driver: fsDriver({ + base: process.cwd() + '/storage/ddns-agent' + }), +}); + + +export const config = await storage.getItem('cloudflare.json'); + diff --git a/agent/test/get-cf-list.ts b/agent/test/get-cf-list.ts new file mode 100644 index 0000000..6fbdb34 --- /dev/null +++ b/agent/test/get-cf-list.ts @@ -0,0 +1,10 @@ +import { CloudflareDDNS } from "@agent/ddns/cloudflare/index.ts"; +import { config } from "./common.ts"; + +const cf = new CloudflareDDNS(); +if (config) { + const res = await cf.getList(config.zone_id, config.api_token); + console.log('Cloudflare DNS Records List:', res); +} else { + console.log('No configuration found.'); +} \ No newline at end of file