Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,9 @@ func (s *HTTPd) RegisterSystemHandler(site *core.Site, valueChan chan<- util.Par
"deletedevice": {"DELETE", "/devices/{class:[a-z]+}/{id:[0-9.]+}", deleteDeviceHandler(site)},
"testconfig": {"POST", "/test/{class:[a-z]+}", testConfigHandler},
"testmerged": {"POST", "/test/{class:[a-z]+}/merge/{id:[0-9.]+}", testConfigHandler},
"interval": {"POST", "/interval/{value:[0-9.]+}", settingsSetDurationHandler(keys.Interval)},
"updatesponsortoken": {"POST", "/sponsortoken", updateSponsortokenHandler},
"deletesponsortoken": {"DELETE", "/sponsortoken", deleteSponsorTokenHandler},
"interval": {"POST", "/interval/{value:[0-9.]+}", settingsSetDurationHandler(keys.Interval, valueChan)},
"updatesponsortoken": {"POST", "/sponsortoken", updateSponsortokenHandler(valueChan)},
"deletesponsortoken": {"DELETE", "/sponsortoken", deleteSponsorTokenHandler(valueChan)},
}

// yaml handlers
Expand Down
54 changes: 33 additions & 21 deletions server/http_config_site_other_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,48 @@ import (

"github.com/evcc-io/evcc/core/keys"
"github.com/evcc-io/evcc/server/db/settings"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/sponsor"
)

func updateSponsortokenHandler(w http.ResponseWriter, r *http.Request) {
var req struct {
Token string `json:"token"`
}

if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
jsonError(w, http.StatusBadRequest, err)
return
}
func updateSponsortokenHandler(valueChan chan<- util.Param) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req struct {
Token string `json:"token"`
}

if req.Token != "" {
if err := sponsor.ConfigureSponsorship(req.Token); err != nil {
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
jsonError(w, http.StatusBadRequest, err)
return
}
sponsor.SetFromYaml(false)
}

// TODO find better place
settings.SetString(keys.SponsorToken, req.Token)
setConfigDirty()
if req.Token != "" {
if err := sponsor.ConfigureSponsorship(req.Token); err != nil {
jsonError(w, http.StatusBadRequest, err)
return
}
sponsor.SetFromYaml(false)
}

// TODO find better place
settings.SetString(keys.SponsorToken, req.Token)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imho we should move this and the channel handling into a sponsor function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can refactor this into a helper function. Would you prefer a function in the sponsor package that handles both the database save and websocket publish, or a local helper in the handler file?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot move this saving and publishing logic to the sponsor package. Pass uiChan to package on init.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 5aaa62d. Moved sponsor token saving and publishing logic to the sponsor package:

  • Added sponsor.Init(valueChan) called in RegisterSystemHandler
  • Created sponsor.SaveToken() and sponsor.DeleteToken() functions that handle database persistence and websocket publishing
  • Updated handlers to use these new functions
  • Removed valueChan parameter from sponsor handlers

setConfigDirty()

status := sponsor.Status()
valueChan <- util.Param{Key: keys.Sponsor, Val: status}

jsonWrite(w, sponsor.Status())
jsonWrite(w, status)
}
}

func deleteSponsorTokenHandler(w http.ResponseWriter, r *http.Request) {
settings.SetString(keys.SponsorToken, "")
setConfigDirty()
jsonWrite(w, true)
func deleteSponsorTokenHandler(valueChan chan<- util.Param) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
settings.SetString(keys.SponsorToken, "")
setConfigDirty()

status := sponsor.Status()
valueChan <- util.Param{Key: keys.Sponsor, Val: status}

jsonWrite(w, true)
}
}
4 changes: 3 additions & 1 deletion server/http_global_settings_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func settingsDeleteHandler(key string) http.HandlerFunc {
}
}

func settingsSetDurationHandler(key string) http.HandlerFunc {
func settingsSetDurationHandler(key string, valueChan chan<- util.Param) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)

Expand All @@ -41,6 +41,8 @@ func settingsSetDurationHandler(key string) http.HandlerFunc {
settings.SetInt(key, int64(time.Second*time.Duration(val)))
setConfigDirty()

valueChan <- util.Param{Key: key, Val: val}

jsonWrite(w, val)
}
}
Expand Down
79 changes: 79 additions & 0 deletions tests/config-control.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { test, expect } from "@playwright/test";
import { start, stop, baseUrl } from "./evcc";
import { enableExperimental, expectModalVisible, expectModalHidden } from "./utils";

test.use({ baseURL: baseUrl() });

test.afterEach(async () => {
await stop();
});

test.describe("control settings (interval)", () => {
test("interval is immediately visible after save without restart", async ({ page }) => {
await start();
await page.goto("/#/config");
await enableExperimental(page);

// Initially, control entry should show default interval (30s)
const controlEntry = page.getByTestId("generalconfig-control");
await expect(controlEntry).toContainText("30");

Check failure on line 19 in tests/config-control.spec.ts

View workflow job for this annotation

GitHub Actions / Integration

[chromium] › tests/config-control.spec.ts:12:3 › control settings (interval) › interval is immediately visible after save without restart

1) [chromium] › tests/config-control.spec.ts:12:3 › control settings (interval) › interval is immediately visible after save without restart Retry #4 ─────────────────────────────────────────────────────────────────────────────────────── Error: expect(locator).toContainText(expected) failed Locator: getByTestId('generalconfig-control') Expected substring: "30" Received string: "Control behavior 🧪 10 s" Timeout: 5000ms Call log: - Expect "toContainText" with timeout 5000ms - waiting for getByTestId('generalconfig-control') 9 × locator resolved to <div data-v-9ec5b3b6="" data-testid="generalconfig-control" class="d-flex justify-content-between flex-wrap align-items-center gap-2">…</div> - unexpected value "Control behavior 🧪 10 s" 17 | // Initially, control entry should show default interval (30s) 18 | const controlEntry = page.getByTestId("generalconfig-control"); > 19 | await expect(controlEntry).toContainText("30"); | ^ 20 | 21 | // Open control modal 22 | await controlEntry.getByRole("button", { name: "Edit" }).click(); at /home/runner/work/evcc/evcc/tests/config-control.spec.ts:19:32

Check failure on line 19 in tests/config-control.spec.ts

View workflow job for this annotation

GitHub Actions / Integration

[chromium] › tests/config-control.spec.ts:12:3 › control settings (interval) › interval is immediately visible after save without restart

1) [chromium] › tests/config-control.spec.ts:12:3 › control settings (interval) › interval is immediately visible after save without restart Retry #3 ─────────────────────────────────────────────────────────────────────────────────────── Error: expect(locator).toContainText(expected) failed Locator: getByTestId('generalconfig-control') Expected substring: "30" Received string: "Control behavior 🧪 10 s" Timeout: 5000ms Call log: - Expect "toContainText" with timeout 5000ms - waiting for getByTestId('generalconfig-control') 9 × locator resolved to <div data-v-9ec5b3b6="" data-testid="generalconfig-control" class="d-flex justify-content-between flex-wrap align-items-center gap-2">…</div> - unexpected value "Control behavior 🧪 10 s" 17 | // Initially, control entry should show default interval (30s) 18 | const controlEntry = page.getByTestId("generalconfig-control"); > 19 | await expect(controlEntry).toContainText("30"); | ^ 20 | 21 | // Open control modal 22 | await controlEntry.getByRole("button", { name: "Edit" }).click(); at /home/runner/work/evcc/evcc/tests/config-control.spec.ts:19:32

Check failure on line 19 in tests/config-control.spec.ts

View workflow job for this annotation

GitHub Actions / Integration

[chromium] › tests/config-control.spec.ts:12:3 › control settings (interval) › interval is immediately visible after save without restart

1) [chromium] › tests/config-control.spec.ts:12:3 › control settings (interval) › interval is immediately visible after save without restart Retry #2 ─────────────────────────────────────────────────────────────────────────────────────── Error: expect(locator).toContainText(expected) failed Locator: getByTestId('generalconfig-control') Expected substring: "30" Received string: "Control behavior 🧪 10 s" Timeout: 5000ms Call log: - Expect "toContainText" with timeout 5000ms - waiting for getByTestId('generalconfig-control') 9 × locator resolved to <div data-v-9ec5b3b6="" data-testid="generalconfig-control" class="d-flex justify-content-between flex-wrap align-items-center gap-2">…</div> - unexpected value "Control behavior 🧪 10 s" 17 | // Initially, control entry should show default interval (30s) 18 | const controlEntry = page.getByTestId("generalconfig-control"); > 19 | await expect(controlEntry).toContainText("30"); | ^ 20 | 21 | // Open control modal 22 | await controlEntry.getByRole("button", { name: "Edit" }).click(); at /home/runner/work/evcc/evcc/tests/config-control.spec.ts:19:32

Check failure on line 19 in tests/config-control.spec.ts

View workflow job for this annotation

GitHub Actions / Integration

[chromium] › tests/config-control.spec.ts:12:3 › control settings (interval) › interval is immediately visible after save without restart

1) [chromium] › tests/config-control.spec.ts:12:3 › control settings (interval) › interval is immediately visible after save without restart Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── Error: expect(locator).toContainText(expected) failed Locator: getByTestId('generalconfig-control') Expected substring: "30" Received string: "Control behavior 🧪 10 s" Timeout: 5000ms Call log: - Expect "toContainText" with timeout 5000ms - waiting for getByTestId('generalconfig-control') 9 × locator resolved to <div data-v-9ec5b3b6="" data-testid="generalconfig-control" class="d-flex justify-content-between flex-wrap align-items-center gap-2">…</div> - unexpected value "Control behavior 🧪 10 s" 17 | // Initially, control entry should show default interval (30s) 18 | const controlEntry = page.getByTestId("generalconfig-control"); > 19 | await expect(controlEntry).toContainText("30"); | ^ 20 | 21 | // Open control modal 22 | await controlEntry.getByRole("button", { name: "Edit" }).click(); at /home/runner/work/evcc/evcc/tests/config-control.spec.ts:19:32

Check failure on line 19 in tests/config-control.spec.ts

View workflow job for this annotation

GitHub Actions / Integration

[chromium] › tests/config-control.spec.ts:12:3 › control settings (interval) › interval is immediately visible after save without restart

1) [chromium] › tests/config-control.spec.ts:12:3 › control settings (interval) › interval is immediately visible after save without restart Error: expect(locator).toContainText(expected) failed Locator: getByTestId('generalconfig-control') Expected substring: "30" Received string: "Control behavior 🧪 10 s" Timeout: 5000ms Call log: - Expect "toContainText" with timeout 5000ms - waiting for getByTestId('generalconfig-control') 9 × locator resolved to <div data-v-9ec5b3b6="" data-testid="generalconfig-control" class="d-flex justify-content-between flex-wrap align-items-center gap-2">…</div> - unexpected value "Control behavior 🧪 10 s" 17 | // Initially, control entry should show default interval (30s) 18 | const controlEntry = page.getByTestId("generalconfig-control"); > 19 | await expect(controlEntry).toContainText("30"); | ^ 20 | 21 | // Open control modal 22 | await controlEntry.getByRole("button", { name: "Edit" }).click(); at /home/runner/work/evcc/evcc/tests/config-control.spec.ts:19:32

// Open control modal
await controlEntry.getByRole("button", { name: "Edit" }).click();

const modal = page.locator("#controlModal");
await expectModalVisible(modal);

// Change interval to 60 seconds
const intervalInput = modal.getByLabel("Interval");
await expect(intervalInput).toHaveValue("30");
await intervalInput.fill("60");

// Save the changes
await modal.getByRole("button", { name: "Save" }).click();

// Modal should close
await expectModalHidden(modal);

// The control entry should now show the new interval value (60s) without restart
await expect(controlEntry).toContainText("60");
await expect(controlEntry).not.toContainText("30");

// Verify by opening the modal again
await controlEntry.getByRole("button", { name: "Edit" }).click();
await expectModalVisible(modal);
await expect(intervalInput).toHaveValue("60");
});

test("residual power is immediately visible after save", async ({ page }) => {
await start();
await page.goto("/#/config");
await enableExperimental(page);

// Open control modal
const controlEntry = page.getByTestId("generalconfig-control");
await controlEntry.getByRole("button", { name: "Edit" }).click();

const modal = page.locator("#controlModal");
await expectModalVisible(modal);

// Get initial residual power value
const residualPowerInput = modal.getByLabel("Residual power");
const initialValue = await residualPowerInput.inputValue();

// Change residual power to 200W
await residualPowerInput.fill("200");

// Save the changes
await modal.getByRole("button", { name: "Save" }).click();

// Modal should close
await expectModalHidden(modal);

// Verify by opening the modal again
await controlEntry.getByRole("button", { name: "Edit" }).click();
await expectModalVisible(modal);
await expect(residualPowerInput).toHaveValue("200");
await expect(residualPowerInput).not.toHaveValue(initialValue);
});
});
31 changes: 31 additions & 0 deletions tests/sponsor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,35 @@
await modal.getByRole("button", { name: "Save" }).click();
await expect(modal).toContainText("token is expired");
});

test("token is immediately visible after save without restart", async ({ page }) => {
await start();
await page.goto("/#/config");
await enableExperimental(page);

// Initially, sponsoring entry should show "---" (no sponsor)
const sponsorEntry = page.getByTestId("generalconfig-sponsoring");
await expect(sponsorEntry).toContainText("---");

// Open sponsor modal and enter expired token (will be saved despite being invalid)
await sponsorEntry.getByRole("button", { name: "Edit" }).click();

const modal = page.getByTestId("sponsor-modal");
const textarea = modal.getByRole("textbox", { name: "Enter your token" });

await textarea.fill(EXPIRED_TOKEN);
await modal.getByRole("button", { name: "Save" }).click();

// After save, the modal should close and show error in the entry (NOT "---")
// The token should be immediately visible in the UI without restart
await expect(modal).not.toBeVisible();

Check failure on line 101 in tests/sponsor.spec.ts

View workflow job for this annotation

GitHub Actions / Integration

[chromium] › tests/sponsor.spec.ts:81:3 › sponsor token › token is immediately visible after save without restart

2) [chromium] › tests/sponsor.spec.ts:81:3 › sponsor token › token is immediately visible after save without restart Retry #4 ─────────────────────────────────────────────────────────────────────────────────────── Error: expect(locator).not.toBeVisible() failed Locator: getByTestId('sponsor-modal') Expected: not visible Received: visible Timeout: 5000ms Call log: - Expect "not toBeVisible" with timeout 5000ms - waiting for getByTestId('sponsor-modal') 9 × locator resolved to <div tabindex="-1" role="dialog" id="sponsorModal" aria-modal="true" aria-hidden="false" data-bs-backdrop="true" data-bs-keyboard="true" data-testid="sponsor-modal" class="modal fade text-dark show modal-lg">…</div> - unexpected value "visible" 99 | // After save, the modal should close and show error in the entry (NOT "---") 100 | // The token should be immediately visible in the UI without restart > 101 | await expect(modal).not.toBeVisible(); | ^ 102 | 103 | // The sponsor entry should now show "invalid" instead of "---" 104 | await expect(sponsorEntry).toContainText("invalid"); at /home/runner/work/evcc/evcc/tests/sponsor.spec.ts:101:29

Check failure on line 101 in tests/sponsor.spec.ts

View workflow job for this annotation

GitHub Actions / Integration

[chromium] › tests/sponsor.spec.ts:81:3 › sponsor token › token is immediately visible after save without restart

2) [chromium] › tests/sponsor.spec.ts:81:3 › sponsor token › token is immediately visible after save without restart Retry #3 ─────────────────────────────────────────────────────────────────────────────────────── Error: expect(locator).not.toBeVisible() failed Locator: getByTestId('sponsor-modal') Expected: not visible Received: visible Timeout: 5000ms Call log: - Expect "not toBeVisible" with timeout 5000ms - waiting for getByTestId('sponsor-modal') 9 × locator resolved to <div tabindex="-1" role="dialog" id="sponsorModal" aria-modal="true" aria-hidden="false" data-bs-backdrop="true" data-bs-keyboard="true" data-testid="sponsor-modal" class="modal fade text-dark show modal-lg">…</div> - unexpected value "visible" 99 | // After save, the modal should close and show error in the entry (NOT "---") 100 | // The token should be immediately visible in the UI without restart > 101 | await expect(modal).not.toBeVisible(); | ^ 102 | 103 | // The sponsor entry should now show "invalid" instead of "---" 104 | await expect(sponsorEntry).toContainText("invalid"); at /home/runner/work/evcc/evcc/tests/sponsor.spec.ts:101:29

Check failure on line 101 in tests/sponsor.spec.ts

View workflow job for this annotation

GitHub Actions / Integration

[chromium] › tests/sponsor.spec.ts:81:3 › sponsor token › token is immediately visible after save without restart

2) [chromium] › tests/sponsor.spec.ts:81:3 › sponsor token › token is immediately visible after save without restart Retry #2 ─────────────────────────────────────────────────────────────────────────────────────── Error: expect(locator).not.toBeVisible() failed Locator: getByTestId('sponsor-modal') Expected: not visible Received: visible Timeout: 5000ms Call log: - Expect "not toBeVisible" with timeout 5000ms - waiting for getByTestId('sponsor-modal') 9 × locator resolved to <div tabindex="-1" role="dialog" id="sponsorModal" aria-modal="true" aria-hidden="false" data-bs-backdrop="true" data-bs-keyboard="true" data-testid="sponsor-modal" class="modal fade text-dark show modal-lg">…</div> - unexpected value "visible" 99 | // After save, the modal should close and show error in the entry (NOT "---") 100 | // The token should be immediately visible in the UI without restart > 101 | await expect(modal).not.toBeVisible(); | ^ 102 | 103 | // The sponsor entry should now show "invalid" instead of "---" 104 | await expect(sponsorEntry).toContainText("invalid"); at /home/runner/work/evcc/evcc/tests/sponsor.spec.ts:101:29

Check failure on line 101 in tests/sponsor.spec.ts

View workflow job for this annotation

GitHub Actions / Integration

[chromium] › tests/sponsor.spec.ts:81:3 › sponsor token › token is immediately visible after save without restart

2) [chromium] › tests/sponsor.spec.ts:81:3 › sponsor token › token is immediately visible after save without restart Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── Error: expect(locator).not.toBeVisible() failed Locator: getByTestId('sponsor-modal') Expected: not visible Received: visible Timeout: 5000ms Call log: - Expect "not toBeVisible" with timeout 5000ms - waiting for getByTestId('sponsor-modal') 9 × locator resolved to <div tabindex="-1" role="dialog" id="sponsorModal" aria-modal="true" aria-hidden="false" data-bs-backdrop="true" data-bs-keyboard="true" data-testid="sponsor-modal" class="modal fade text-dark show modal-lg">…</div> - unexpected value "visible" 99 | // After save, the modal should close and show error in the entry (NOT "---") 100 | // The token should be immediately visible in the UI without restart > 101 | await expect(modal).not.toBeVisible(); | ^ 102 | 103 | // The sponsor entry should now show "invalid" instead of "---" 104 | await expect(sponsorEntry).toContainText("invalid"); at /home/runner/work/evcc/evcc/tests/sponsor.spec.ts:101:29

Check failure on line 101 in tests/sponsor.spec.ts

View workflow job for this annotation

GitHub Actions / Integration

[chromium] › tests/sponsor.spec.ts:81:3 › sponsor token › token is immediately visible after save without restart

2) [chromium] › tests/sponsor.spec.ts:81:3 › sponsor token › token is immediately visible after save without restart Error: expect(locator).not.toBeVisible() failed Locator: getByTestId('sponsor-modal') Expected: not visible Received: visible Timeout: 5000ms Call log: - Expect "not toBeVisible" with timeout 5000ms - waiting for getByTestId('sponsor-modal') 9 × locator resolved to <div tabindex="-1" role="dialog" id="sponsorModal" aria-modal="true" aria-hidden="false" data-bs-backdrop="true" data-bs-keyboard="true" data-testid="sponsor-modal" class="modal fade text-dark show modal-lg">…</div> - unexpected value "visible" 99 | // After save, the modal should close and show error in the entry (NOT "---") 100 | // The token should be immediately visible in the UI without restart > 101 | await expect(modal).not.toBeVisible(); | ^ 102 | 103 | // The sponsor entry should now show "invalid" instead of "---" 104 | await expect(sponsorEntry).toContainText("invalid"); at /home/runner/work/evcc/evcc/tests/sponsor.spec.ts:101:29

// The sponsor entry should now show "invalid" instead of "---"
await expect(sponsorEntry).toContainText("invalid");
await expect(sponsorEntry).not.toContainText("---");

// Verify by opening the modal again
await sponsorEntry.getByRole("button", { name: "Edit" }).click();
await expect(modal.getByRole("textbox", { name: "Your token" })).toHaveValue(SHORT_TOKEN);
});
});
Loading