-
Notifications
You must be signed in to change notification settings - Fork 1
feat(offline): add manual connectivity verification #104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rdlabo
wants to merge
2
commits into
main
Choose a base branch
from
feat/offline-connectivity-verifier
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
projects/kit/offline/src/lib/offline-network-verification.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import { provideHttpClient } from '@angular/common/http'; | ||
| import { HttpTestingController, provideHttpClientTesting } from '@angular/common/http/testing'; | ||
| import { TestBed } from '@angular/core/testing'; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
| import { OfflineNetworkService } from './offline-network.service'; | ||
| import { OFFLINE_BYPASS, OFFLINE_IGNORE_TRANSPORT_FAILURE } from './offline-request-policy'; | ||
|
|
||
| describe('OfflineNetworkService connection verification', () => { | ||
| let http: HttpTestingController; | ||
| let service: OfflineNetworkService; | ||
|
|
||
| beforeEach(() => { | ||
| TestBed.configureTestingModule({ | ||
| providers: [OfflineNetworkService, provideHttpClient(), provideHttpClientTesting()], | ||
| }); | ||
| http = TestBed.inject(HttpTestingController); | ||
| service = TestBed.inject(OfflineNetworkService); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| http.verify(); | ||
| vi.useRealTimers(); | ||
| }); | ||
|
|
||
| it('bypasses local fallback and records a successful API check', async () => { | ||
| const markApiSuccess = vi.spyOn(service, 'markApiSuccess'); | ||
| const result = service.verifyConnection('/status'); | ||
| expect(service.checkingConnection()).toBe(true); | ||
| const request = http.expectOne('/status'); | ||
| expect(request.request.context.get(OFFLINE_BYPASS)).toBe(true); | ||
| expect(request.request.context.get(OFFLINE_IGNORE_TRANSPORT_FAILURE)).toBe(true); | ||
| request.flush({}); | ||
|
|
||
| await expect(result).resolves.toBe(true); | ||
| expect(markApiSuccess).toHaveBeenCalledOnce(); | ||
| expect(service.checkingConnection()).toBe(false); | ||
| }); | ||
|
|
||
| it('shares the service-wide in-flight check and permits another check after it settles', async () => { | ||
| const first = service.verifyConnection('/status'); | ||
| const second = service.verifyConnection('/another-status'); | ||
| expect(second).toBe(first); | ||
| http.expectOne('/status').flush({}); | ||
| http.expectNone('/another-status'); | ||
| await Promise.all([first, second]); | ||
|
|
||
| const third = service.verifyConnection('/status'); | ||
| http.expectOne('/status').flush({}); | ||
| await expect(third).resolves.toBe(true); | ||
| }); | ||
|
|
||
| it('does not classify an HTTP error as a disconnected transport', async () => { | ||
| const markApiFailure = vi.spyOn(service, 'markApiFailure'); | ||
| const result = service.verifyConnection('/status'); | ||
| http.expectOne('/status').flush({}, { status: 500, statusText: 'Server Error' }); | ||
|
|
||
| await expect(result).resolves.toBe(false); | ||
| expect(markApiFailure).not.toHaveBeenCalled(); | ||
| expect(service.checkingConnection()).toBe(false); | ||
| }); | ||
|
|
||
| it('records a status-zero failure when no newer API observation exists and permits retry', async () => { | ||
| const markApiFailure = vi.spyOn(service, 'markApiFailure'); | ||
| const first = service.verifyConnection('/status'); | ||
| http.expectOne('/status').error(new ProgressEvent('error')); | ||
| await expect(first).resolves.toBe(false); | ||
| expect(markApiFailure).toHaveBeenCalledOnce(); | ||
| expect(service.state()).toBe('offline'); | ||
|
|
||
| const second = service.verifyConnection('/status'); | ||
| http.expectOne('/status').flush({}); | ||
| await expect(second).resolves.toBe(true); | ||
| }); | ||
|
|
||
| it('does not let an older probe failure overwrite a newer successful API observation', async () => { | ||
| const markApiFailure = vi.spyOn(service, 'markApiFailure'); | ||
| const verification = service.verifyConnection('/status'); | ||
| service.markApiSuccess(); | ||
| http.expectOne('/status').error(new ProgressEvent('error')); | ||
|
|
||
| await expect(verification).resolves.toBe(false); | ||
| expect(markApiFailure).not.toHaveBeenCalled(); | ||
| expect(service.state()).toBe('unverified'); | ||
| }); | ||
|
|
||
| it('does not overwrite reachability when a stalled check times out and permits retry', async () => { | ||
| vi.useFakeTimers(); | ||
| const markApiFailure = vi.spyOn(service, 'markApiFailure'); | ||
| const first = service.verifyConnection('/status', 10); | ||
| http.expectOne('/status'); | ||
| await vi.advanceTimersByTimeAsync(10); | ||
|
|
||
| await expect(first).resolves.toBe(false); | ||
| expect(markApiFailure).not.toHaveBeenCalled(); | ||
| expect(service.checkingConnection()).toBe(false); | ||
|
|
||
| const second = service.verifyConnection('/status', 10); | ||
| http.expectOne('/status').flush({}); | ||
| await expect(second).resolves.toBe(true); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.